- Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathknn.py
71 lines (47 loc) · 2.19 KB
/
knn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# coding:utf-8
fromcollectionsimportCounter
importnumpyasnp
fromscipy.spatial.distanceimporteuclidean
frommla.baseimportBaseEstimator
classKNNBase(BaseEstimator):
def__init__(self, k=5, distance_func=euclidean):
"""Base class for Nearest neighbors classifier and regressor.
Parameters
----------
k : int, default 5
The number of neighbors to take into account. If 0, all the
training examples are used.
distance_func : function, default euclidean distance
A distance function taking two arguments. Any function from
scipy.spatial.distance will do.
"""
self.k=Noneifk==0elsek# l[:None] returns the whole list
self.distance_func=distance_func
defaggregate(self, neighbors_targets):
raiseNotImplementedError()
def_predict(self, X=None):
predictions= [self._predict_x(x) forxinX]
returnnp.array(predictions)
def_predict_x(self, x):
"""Predict the label of a single instance x."""
# compute distances between x and all examples in the training set.
distances= (self.distance_func(x, example) forexampleinself.X)
# Sort all examples by their distance to x and keep their target value.
neighbors=sorted(((dist, target) for (dist, target) inzip(distances, self.y)), key=lambdax: x[0])
# Get targets of the k-nn and aggregate them (most common one or
# average).
neighbors_targets= [targetfor (_, target) inneighbors[: self.k]]
returnself.aggregate(neighbors_targets)
classKNNClassifier(KNNBase):
"""Nearest neighbors classifier.
Note: if there is a tie for the most common label among the neighbors, then
the predicted label is arbitrary."""
defaggregate(self, neighbors_targets):
"""Return the most common target label."""
most_common_label=Counter(neighbors_targets).most_common(1)[0][0]
returnmost_common_label
classKNNRegressor(KNNBase):
"""Nearest neighbors regressor."""
defaggregate(self, neighbors_targets):
"""Return the mean of all targets."""
returnnp.mean(neighbors_targets)