-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.py
51 lines (41 loc) · 1.88 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
from supervisedlearner import SupervisedLearner
import numpy as np
class KNNClassifier(SupervisedLearner):
def __init__(self, feature_funcs, k):
super(KNNClassifier, self).__init__(feature_funcs)
self.k = k
def train(self, anchor_points, anchor_labels):
"""
:param anchor_points: a 2D numpy array, in which each row is
a datapoint, without its label, to be used
for one of the anchor points
:param anchor_labels: a list in which the i'th element is the correct label
of the i'th datapoint in anchor_points
Does not return anything; simply stores anchor_labels and the
_features_ of anchor_points.
"""
pass
def predict(self, x):
"""
Given a single data point, x, represented as a 1D numpy array,
predicts the class of x by taking a plurality vote among its k
nearest neighbors in feature space. Resolves ties arbitrarily.
The K nearest neighbors are determined based on Euclidean distance
in _feature_ space (so be sure to compute the features of x).
Returns the label of the class to which x is predicted to belong.
"""
# A list containing the Euclidean distance of x from another point y,
# each element of which is in the form (distance, y index)
# Get the k closest points to x and their labels
# Note: max(set(x), key=x.count) returns the mode of a list x.
pass
def evaluate(self, datapoints, labels):
"""
:param datapoints: a 2D numpy array, in which each row is a datapoint.
:param labels: a 1D numpy array, in which the i'th element is the
correct label of the i'th datapoint.
Returns the fraction (between 0 and 1) of the given datapoints to which
predict(.) assigns the correct label
"""
# Count the number of correct predictions and find the model accuracy
pass