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