ML.KNN.KNNClassificationModel

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

# Source Code for Module ML.KNN.KNNClassificationModel

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