1
2
3
4
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
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 = []
24
26 return "Classification Model"
27
29 self._badExamples = examples
30
32 return self._badExamples
33
36
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
59 knnLst = self.GetNeighbors(example)
60
61
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
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