rdkit.ML.KNN.KNNClassificationModel

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

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

     1  # $Id$
     2  #
     3  #  Copyright (C) 2003 Rational Discovery LLC
     4  #      All Rights Reserved
     5  #
     6  """ Define the class _KNNClassificationModel_, used to represent a k-nearest neighbhors
     7      classification model
     8
     9      Inherits from _KNNModel_
    10  """
    11
    12  from rdkit.ML.KNN import KNNModel
    13
    14
    15 -class KNNClassificationModel(KNNModel.KNNModel):
    16    """ This is used to represent a k-nearest neighbor classifier
    17
    18    """
    19
    20 -  def __init__(self, k, attrs, dfunc, radius=None):
    21      self._setup(k, attrs, dfunc, radius)
    22
    23      self._badExamples = []  # list of examples incorrectly classified
    24
    25 -  def type(self):
    26      return "Classification Model"
    27
    28 -  def SetBadExamples(self, examples):
    29      self._badExamples = examples
    30
    31 -  def GetBadExamples(self):
    32      return self._badExamples
    33
    34 -  def NameModel(self, varNames):
    35      self.SetName(self.type())
    36
    37 -  def ClassifyExample(self, example, appendExamples=0, neighborList=None):
    38      """ Classify a an example by looking at its closest neighbors
    39
    40      The class assigned to this example is same as the class for the mojority of its
    41      _k 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      - neighborList: if provided, will be used to return the list of neighbors
    50
    51      **Returns**
    52
    53        - the classification of _example_
    54      """
    55      if appendExamples:
    56        self._examples.append(example)
    57
    58      # first find the k-closest examples in the traning set
    59      knnLst = self.GetNeighbors(example)
    60
    61      # find out how many of the neighbors belong to each of the classes
    62      clsCnt = {}
    63      for knn in knnLst:
    64        cls = knn[1][-1]
    65        if (cls in clsCnt):
    66          clsCnt[cls] += 1
    67        else:
    68          clsCnt[cls] = 1
    69      if neighborList is not None:
    70        neighborList.extend(knnLst)
    71
    72      # now return the class with the maximum count
    73      mkey = -1
    74      mcnt = -1
    75      for key in clsCnt.keys():
    76        if (mcnt < clsCnt[key]):
    77          mkey = key
    78          mcnt = clsCnt[key]
    79
    80      return mkey
    81