-
Notifications
You must be signed in to change notification settings - Fork 0
/
E16 nearest neighbour.py
54 lines (39 loc) · 1.82 KB
/
E16 nearest neighbour.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
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
# create random data with two classes
X, Y = make_blobs(n_samples=16, n_features=2, centers=2, center_box=(-2, 2))
# scale the data so that all values are between 0.0 and 1.0
X = MinMaxScaler().fit_transform(X)
# split two data points from the data as test data and
# use the remaining n-2 points as the training data
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=2)
# place-holder for the predicted classes
y_predict = np.empty(len(y_test), dtype=np.int64)
# produce line segments that connect the test data points
# to the nearest neighbors for drawing the chart
lines = []
# distance function
def dist(a, b):
sum = 0
for ai, bi in zip(a, b):
sum = sum + (ai - bi)**2
return np.sqrt(sum)
def main(X_train, X_test, y_train, y_test):
global y_predict
global lines
k = 3 # classify our test items based on the classes of 3 nearest neighbors
# process each of the test data points
for i, test_item in enumerate(X_test):
# calculate the distances to all training points
distances = [dist(train_item, test_item) for train_item in X_train]
# add your code here
nearest = np.argsort(distances) # this just finds the nearest neighbour (so k=1)
nearest_labels = y_train[nearest[:k]]
# create a line connecting the points for the chart
# you may change this to do the same for all the k nearest neigbhors if you like
# but it will not be checked in the tests
y_predict[i] = np.round(np.mean(nearest_labels)) # this just classifies everything as 0
print(y_predict)
main(X_train, X_test, y_train, y_test)