1
2
3
4
5
6
7 """ Define the class _KNNModel_, used to represent a k-nearest neighbhors model
8
9 """
10 from ML.KNN import DistFunctions
11 import bisect
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 """
32 self._setup(k, attrs, dfunc)
33
34 - def _setup(self, k, attrs, dfunc) :
35 self._examples = []
36 self._trainingExamples = []
37 self._testExamples = []
38 self._k = k
39 self._attrs = attrs
40 self._dfunc = dfunc
41 self._name = ""
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
72 res = [(1e8,0)]*self._k
73 for trex in self._trainingExamples:
74 dist = self._dfunc(trex, example, self._attrs)
75 knn = (dist, trex)
76 if dist < res[-1][0]:
77 loc = bisect.bisect(res,knn)
78 res.insert(loc,knn)
79 res.pop()
80 return res
81