-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkNN.py
More file actions
165 lines (150 loc) · 5.44 KB
/
Copy pathkNN.py
File metadata and controls
165 lines (150 loc) · 5.44 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import csv
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.colors import ListedColormap
from sklearn import neighbors
def readData(path):
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
data = []
for row in csv_reader:
data.append([float(row[1]), float(row[2]), int(row[0])])
return data
def kNN(k, trainData, testData, bound):
# bound = True -> Operates for plotting the decision boundaries
correctEstimates = 0
estimates = []
for testRow in testData:
distances = []
for trainRow in trainData:
dist = np.math.sqrt(((trainRow[0] - testRow[0]) ** 2) + ((trainRow[1] - testRow[1]) ** 2))
distances.append([dist, trainRow[2]])
distances.sort()
nearest = distances[:k] # Get the k nearest points
votes = [0, 0, 0]
for c in nearest:
if c[0] < 0.01: # Handle points that are at the same spot or too near
c[0] = 0.01
votes[c[1] - 1] += 1 / c[0] # Calculate the votes for every class
estimatedClass = 1 + votes.index(max(votes)) # Get the class with the highest vote
if bound:
estimates.append(estimatedClass)
elif testRow[2] == estimatedClass:
correctEstimates += 1
if not bound:
print("k =", k, " ", "{:.2f}".format(correctEstimates / len(testData) * 100), " ",
len(testData) - correctEstimates, "/", len(testData))
else:
return estimates
def scikitAccuracy(k, trainData, testData):
X = []
y = []
test = []
testResult = []
for trainRow in trainData:
X.append([trainRow[0], trainRow[1]])
y.append(trainRow[2])
for testRow in testData:
test.append([testRow[0], testRow[1]])
testResult.append(testRow[2])
clf = neighbors.KNeighborsClassifier(k, weights='distance')
clf.fit(X, y)
predictions = clf.predict(test)
correctEstimates = 0
for index, t in enumerate(testResult):
if t == predictions[index]:
correctEstimates += 1
print("k =", k, " ", "{:.2f}".format(correctEstimates / len(testData) * 100), " ",
len(testData) - correctEstimates, "/", len(testData))
def decisionBoundaries(k, trainData):
# Draws the decision boundaries using sci-kit learn's k-NN algorithm
X = []
y = []
x_min = 1000
x_max = 0
y_min = 1000
y_max = 0
for trainRow in trainData:
if trainRow[0] < x_min:
x_min = trainRow[0]
elif trainRow[0] > x_max:
x_max = trainRow[0]
if trainRow[1] < y_min:
y_min = trainRow[1]
elif trainRow[1] > y_max:
y_max = trainRow[1]
X.append([trainRow[0], trainRow[1]])
y.append(trainRow[2])
h = .01 # step size in the mesh
cmap_light = ListedColormap(['#61ff5e', '#fc6d91', '#2beaff'])
cmap_bold = ['#3da13b', '#a3465f', '#1c8f9c']
clf = neighbors.KNeighborsClassifier(k, weights='distance')
clf.fit(X, y)
xx, yy = np.meshgrid(np.arange(x_min - 0.5, x_max + 0.5, h), np.arange(y_min - 0.5, y_max + 0.5, h))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure(figsize=(8, 6))
plt.contourf(xx, yy, Z, cmap=cmap_light)
sns.scatterplot(x=list(zip(*X))[0], y=list(zip(*X))[1], hue=y, palette=cmap_bold, alpha=1.0, edgecolor="black")
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("Decision Boundaries k=%i" % k)
plt.xlabel("x coordinates")
plt.ylabel("y coordinates")
plt.show()
def customDecisionBoundaries(k, trainData):
# Draws the decision boundaries using my own k-NN algorithm
X = []
y = []
x_min = 1000
x_max = 0
y_min = 1000
y_max = 0
for trainRow in trainData:
if trainRow[0] < x_min:
x_min = trainRow[0]
elif trainRow[0] > x_max:
x_max = trainRow[0]
if trainRow[1] < y_min:
y_min = trainRow[1]
elif trainRow[1] > y_max:
y_max = trainRow[1]
X.append([trainRow[0], trainRow[1]])
y.append(trainRow[2])
h = .01 # step size in the mesh
cmap_light = ListedColormap(['#61ff5e', '#fc6d91', '#2beaff'])
cmap_bold = ['#3da13b', '#a3465f', '#1c8f9c']
xx, yy = np.meshgrid(np.arange(x_min - 0.5, x_max + 0.5, h), np.arange(y_min - 0.5, y_max + 0.5, h))
Z = np.c_[xx.ravel(), yy.ravel()]
Z = kNN(k, trainData, Z, True) # Apply the k-NN algorithm to get predictions for all points in the mesh
Z = np.array(Z).reshape(xx.shape)
plt.figure(figsize=(8, 6))
plt.contourf(xx, yy, Z, cmap=cmap_light)
sns.scatterplot(x=list(zip(*X))[0], y=list(zip(*X))[1], hue=y, palette=cmap_bold, alpha=1.0, edgecolor="black")
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("Decision Boundaries k=%i" % k)
plt.xlabel("x coordinates")
plt.ylabel("y coordinates")
plt.show()
train = readData("data_training.csv")
test = readData("data_test.csv")
print(" Accuracy (%) Error Count")
i = 1
while i < 15:
if i == 13:
i = 15
kNN(i, train, test, False)
i += 2
print()
print("Sci-kit Accuracy (%) Error Count")
i = 1
while i < 15:
if i == 13:
i = 15
scikitAccuracy(i, train, test)
i += 2
# Change the k values to test with different values
#decisionBoundaries(25, train)
customDecisionBoundaries(25, train)