-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnearest_neighbour_classifiers.py
47 lines (34 loc) · 1.24 KB
/
nearest_neighbour_classifiers.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
import numpy as np
from distance_metrics import euclidean_distance
def mean(feature_vectors):
size = len(feature_vectors)
cumulative = np.zeros(feature_vectors[0].shape)
for feature_vector in feature_vectors:
cumulative += feature_vector
return cumulative / size
def nearest_neighbours(dataset, point, metric):
point = np.array(point)
for feature_vector, label in dataset:
similarity = metric(np.array(feature_vector), point)
print(feature_vector, label, similarity)
def nearest_mean(dataset, point, metric):
clusters = {}
for feature_vector, label in dataset:
if label not in clusters:
clusters[label] = []
clusters[label].append(feature_vector)
grouped_dataset = []
for label, feature_vectors in clusters.items():
grouped_dataset.append((mean(feature_vectors), label))
return nearest_neighbours(grouped_dataset, point, metric)
def main():
dataset = [
(np.array([[7, 7]]), "A"),
(np.array([[7, 4]]), "A"),
(np.array([[3, 4]]), "B"),
(np.array([[1, 4]]), "B"),
]
nearest_neighbours(dataset, (3, 7), euclidean_distance)
nearest_mean(dataset, (3, 7), euclidean_distance)
if __name__ == "__main__":
main()