1
2
3
4
5
6
7 """ Define the class _KNNRegressionModel_, used to represent a k-nearest neighbhors
8 regression model
9
10 Inherits from _KNNModel_
11 """
12
13 from rdkit.ML.KNN import KNNModel
14 from rdkit.ML.KNN import DistFunctions
15 import math
16
17
19 """ This is used to represent a k-nearest neighbor classifier
20
21 """
22
24 self._setup(k, attrs, dfunc)
25
26 self._badExamples = []
27
29 return "Regression Model"
30
32 self._badExamples = examples
33
35 return self._badExamples
36
39
40 - def PredictExample(self, example, appendExamples=0, weightedAverage=0) :
41 """ Generates a prediction for an example by looking at its closest 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 **Returns**
50
51 - the classification of _example_
52
53 """
54 if appendExamples:
55 self._examples.append(example)
56
57
58 knnLst = self.GetNeighbors(example)
59
60 accum = 0.0
61 for knn in knnLst:
62 if not weightedAverage:
63 accum += knn[1][-1]
64 if not weightedAverage:
65 accum /= len(knnLst)
66
67 return accum
68