rdkit.ML.KNN.KNNModel

Package rdkit :: Package ML :: Package KNN :: Module KNNModel |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.ML.KNN.KNNModel

     1  # $Id$
     2  #
     3  #  Copyright (C) 2003 Rational Discovery LLC
     4  #      All Rights Reserved
     5  #
     6  """ Define the class _KNNModel_, used to represent a k-nearest neighbhors model
     7
     8  """
     9  from rdkit.DataStructs.TopNContainer import TopNContainer
    10
    11
    12 -class KNNModel(object):
    13    """ This is a base class used by KNNClassificationModel
    14    and KNNRegressionModel to represent a k-nearest neighbor predictor. In general
    15    one of this child classes needs to be instantiated.
    16
    17    _KNNModel_s can save the following pieces of internal state, accessible via
    18      standard setter/getter functions - the child object store additional stuff:
    19
    20      1) _Examples_: a list of examples which have been predicted (either classified
    21                      or values predicted)
    22
    23      2) _TrainingExamples_: List of training examples (since this is a KNN model these examples
    24                             along with the value _k_ below define the model)
    25
    26      3) _TestExamples_: the list of examples used to test the model
    27
    28      4) _k_: the number of closest neighbors used for prediction
    29
    30    """
    31
    32 -  def __init__(self, k, attrs, dfunc, radius=None):
    33      self._setup(k, attrs, dfunc, radius)
    34
    35 -  def _setup(self, k, attrs, dfunc, radius):
    36      self._examples = []
    37      self._trainingExamples = []
    38      self._testExamples = []
    39      self._k = k
    40      self._attrs = attrs
    41      self._dfunc = dfunc
    42      self._name = ""
    43      self._radius = radius
    44
    45 -  def GetName(self):
    46      return self._name
    47
    48 -  def SetName(self, name):
    49      self._name = name
    50
    51 -  def GetExamples(self):
    52      return self._examples
    53
    54 -  def SetExamples(self, examples):
    55      self._examples = examples
    56
    57 -  def GetTrainingExamples(self):
    58      return self._trainingExamples
    59
    60 -  def SetTrainingExamples(self, examples):
    61      self._trainingExamples = examples
    62
    63 -  def GetTestExamples(self):
    64      return self._testExamples
    65
    66 -  def SetTestExamples(self, examples):
    67      self._testExamples = examples
    68
    69 -  def GetNeighbors(self, example):
    70      """ Returns the k nearest neighbors of the example
    71
    72      """
    73      nbrs = TopNContainer(self._k)
    74      for trex in self._trainingExamples:
    75        dist = self._dfunc(trex, example, self._attrs)
    76        if self._radius is None or dist < self._radius:
    77          nbrs.Insert(-dist, trex)
    78      nbrs.reverse()
    79      return [x for x in nbrs]
    80