rdkit.ML.KNN.KNNRegressionModel

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

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

     1  # $Id: KNNRegressionModel.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 _KNNRegressionModel_, used to represent a k-nearest neighbhors
     8  regression model
     9
    10      Inherits from _KNNModel_
    11  """
    12
    13  from rdkit.ML.KNN import KNNModel
    14  from rdkit.ML.KNN import DistFunctions
    15  import math
    16
    17
    18 -class KNNRegressionModel(KNNModel.KNNModel) :
    19    """ This is used to represent a k-nearest neighbor classifier
    20
    21    """
    22
    23 -  def __init__(self, k, attrs, dfunc) :
    24      self._setup(k, attrs, dfunc)
    25
    26      self._badExamples = [] # list of examples incorrectly classified
    27
    28 -  def type(self):
    29      return "Regression Model"
    30
    31 -  def SetBadExamples(self, examples) :
    32      self._badExamples = examples
    33
    34 -  def GetBadExamples(self) :
    35      return self._badExamples
    36
    37 -  def NameModel(self, varNames) :
    38      self.SetName(self.type())
    39
    40 -  def PredictExample(self, example, appendExamples=0, weightedAverage=0) :
    41      """ Generates a prediction for an example by looking at its closest neighbors
    42
    43      **Arguments**
    44
    45        - examples: the example to be classified
    46
    47        - appendExamples: if this is nonzero then the example will be stored on this model
    48
    49      **Returns**
    50
    51        - the classification of _example_
    52
    53      """
    54      if appendExamples:
    55        self._examples.append(example)
    56
    57      # first find the k-closest examples in the traning set
    58      knnLst = self.GetNeighbors(example)
    59
    60      accum = 0.0
    61      for knn in knnLst:
    62        if not weightedAverage:
    63          accum += knn[1][-1]
    64      if not weightedAverage:
    65        accum /= len(knnLst)
    66
    67      return accum
    68