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: KNNModel.py 997 2009-02-25 06:12:43Z glandrum $
     2  #
     3  #  Copyright (C) 2003 Rational Discovery LLC
     4  #      All Rights Reserved
     5  #
     6
     7  """ Define the class _KNNModel_, used to represent a k-nearest neighbhors model
     8
     9  """
    10  from rdkit.ML.KNN import DistFunctions
    11  import bisect
    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 -  def __init__(self, k, attrs, dfunc) :
    32      self._setup(k, attrs, dfunc)
    33
    34 -  def _setup(self, k, attrs, dfunc) :
    35      self._examples = []
    36      self._trainingExamples = []
    37      self._testExamples = []
    38      self._k = k
    39      self._attrs = attrs
    40      self._dfunc = dfunc
    41      self._name = ""
    42
    43 -  def GetName(self) :
    44      return self_name
    45
    46 -  def SetName(self, name) :
    47      self._name = name
    48
    49 -  def GetExamples(self) :
    50      return self._examples
    51
    52 -  def SetExamples(self, examples):
    53      self._examples = examples
    54
    55 -  def GetTrainingExamples(self):
    56      return self._trainingExamples
    57
    58 -  def SetTrainingExamples(self,examples):
    59      self._trainingExamples = examples
    60
    61 -  def GetTestExamples(self) :
    62      return self._testExamples
    63
    64 -  def SetTestExamples(self, examples) :
    65      self._testExamples = examples
    66
    67 -  def GetNeighbors(self,example):
    68      """ Returns the k nearest neighbors of the example
    69
    70      """
    71      # we'll maintain a sorted list of nearest neighbors in res:
    72      res = [(1e8,0)]*self._k
    73      for trex in self._trainingExamples:
    74        dist = self._dfunc(trex, example, self._attrs)
    75        knn = (dist, trex)
    76        if dist < res[-1][0]:
    77          loc = bisect.bisect(res,knn)
    78          res.insert(loc,knn)
    79          res.pop()
    80      return res
    81