1
2
3
4
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
18 """ This is used to represent a k-nearest neighbor classifier
19
20 """
21
23 self._setup(k, attrs, dfunc)
24
25 self._badExamples = []
26
28 return "Classification Model"
29
31 self._badExamples = examples
33 return self._badExamples
34
37
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
58 knnLst = self.GetNeighbors(example)
59
60
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
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