1
2
3
4
5
6
7 """ Define the class _KNNModel_, used to represent a k-nearest neighbhors model
8
9 """
10 from rdkit.DataStructs.TopNContainer import TopNContainer
12 """ This is a base class used by KNNClassificationModel
13 and KNNRegressionModel to represent a k-nearest neighbor predictor. In general
14 one of this child classes needs to be instantiated.
15
16 _KNNModel_s can save the following pieces of internal state, accessible via
17 standard setter/getter functions - the child object store additional stuff:
18
19 1) _Examples_: a list of examples which have been predicted (either classified
20 or values predicted)
21
22 2) _TrainingExamples_: List of training examples (since this is a KNN model these examples
23 along with the value _k_ below define the model)
24
25 3) _TestExamples_: the list of examples used to test the model
26
27 4) _k_: the number of closest neighbors used for prediction
28
29 """
30 - def __init__(self, k, attrs, dfunc, radius=None) :
32
33 - def _setup(self, k, attrs, dfunc, radius) :
34 self._examples = []
35 self._trainingExamples = []
36 self._testExamples = []
37 self._k = k
38 self._attrs = attrs
39 self._dfunc = dfunc
40 self._name = ""
41 self._radius = radius
42
45
48
51
53 self._examples = examples
54
56 return self._trainingExamples
57
59 self._trainingExamples = examples
60
62 return self._testExamples
63
65 self._testExamples = examples
66
68 """ Returns the k nearest neighbors of the example
69
70 """
71 nbrs = TopNContainer(self._k)
72 for trex in self._trainingExamples:
73 dist = self._dfunc(trex, example, self._attrs)
74 if self._radius is None or dist<self._radius:
75 nbrs.Insert(-dist,trex)
76 nbrs.reverse()
77 return [x for x in nbrs]
78