-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknn.py
More file actions
387 lines (327 loc) · 13.3 KB
/
knn.py
File metadata and controls
387 lines (327 loc) · 13.3 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin
from scipy import stats
class KNNClassifier(BaseEstimator,ClassifierMixin):
def __init__(self,label_type="class",weight_type='no_weight', k=3, c_mask=None, n_mask=None): ## add parameters here
"""
Args:
columntype for each column tells you if continues[real] or if nominal.
weight_type: inverse_distance voting or if non distance weighting. Options = ["no_weight","inverse_distance"]
"""
self.label_type = label_type
self.weight_type = weight_type
self.k = k
self.data = None
self.labels = None
self.norm = "mixed" if c_mask is not None else "2"
self.c_mask = c_mask
self.n_mask = n_mask
def fit(self,data,labels):
""" Fit the data; run the algorithm (for this lab really just saves the data :D)
Args:
X (array-like): A 2D numpy array with the training data, excluding targets
y (array-like): A 2D numpy array with the training targets
Returns:
self: this allows this to be chained, e.g. model.fit(X,y).predict(X_test)
"""
self.X = data
self.y = labels
return self
def predict(self,X):
""" Predict all classes for a dataset X
Args:
X (array-like): A 2D numpy array with the training data, excluding targets
Returns:
array, shape (n_samples,)
Predicted target values per element in X.
"""
if self.label_type == "class":
return np.array([self._pred_class(x) for x in X])
else:
return np.array([self._pred_regress(x) for x in X])
def _get_distances(self, x):
"""
Gets the distance based on self.norm
:param x:
:return:
"""
# sorted points by distance
if self.norm == "2":
return np.linalg.norm(self.X - x, axis=1)
elif self.norm == "mixed":
# get continuous distances
cont_dist = np.linalg.norm(self.X[:, self.c_mask] - x[self.c_mask], axis=1)
# get nominal distances
nominal_dist = np.sum(self.X[:,self.n_mask] == x[self.n_mask], axis=1)
# get missing value distances
unknown_dist = np.sum(~(np.isnan(self.X) & np.isnan(x)), axis=1)
return cont_dist + nominal_dist + unknown_dist
def _get_closest(self, x, k):
"""
:param x: new data point to classify
:return:
"""
distances = self._get_distances(x)
srtd_idx = np.argsort(distances)
# get sorted distances
sorted_dist = distances[srtd_idx][:k]
# get inidices of top k closest data points
pnts = srtd_idx[:k]
# get the k nearest labels
nearest_labels = self.y[pnts]
return nearest_labels, sorted_dist
def _pred_class(self, x):
"""
Prediction algorithm for classification
:param data:
:param labels:
:return:
"""
# Gets the distance from each vector, find the closest k values and returns the mode of their labels
nearest_labels, sorted_dist = self._get_closest(x, self.k)
# get counts
labels, cnts = np.unique(nearest_labels, return_counts=True)
labels = labels[np.argsort(cnts[::-1])]
if self.weight_type == 'inverse_distance':
max_val = 0
max_l = 0
for l in labels:
m = np.sum(1/(sorted_dist[nearest_labels == l])**2)
if m > max_val:
max_val = m
max_l = l
return max_l
return labels[0]
def _pred_regress(self, x):
"""
Prediction Algorithm for regression data
:param data:
:param labels:
:return:
"""
# Gets the distance from each vector, find the closest k values and returns the mode of their labels
nearest_labels, sorted_dist = self._get_closest(x, self.k)
if self.weight_type == 'inverse_distance':
weights = (1/sorted_dist**2)
return np.sum(weights*nearest_labels) / np.sum(weights)
else:
return np.mean(nearest_labels)
#Returns the Mean score given input data and labels
def score(self, X, y):
""" Return accuracy of model on a given dataset. Must implement own score function.
Args:
X (array-like): A 2D numpy array with data, excluding targets
y (array-like): A 2D numpy array with targets
Returns:
score : float
Mean accuracy of self.predict(X) wrt. y.
"""
y_ = self.predict(X)
y.flatten()
n = len(y)
if self.label_type == "class":
return sum(y_ == y) / n
else:
return np.sum((y_ - y)**2) / n
# ------------------ KD TREE Implementation ------------------
class KDTNode:
def __init__(self, x):
if(type(x)is not np.ndarray):
raise TypeError("X must be an np.array")
self.value = x
self.left = None
self.right = None
self.pivot = None
def has_no_children(self):
"""
Checks to see if node has children or not
:return: bool True if has NO children
"""
if(self.left is None and self.right is None):
return True
else:
return False
def has_left(self):
"""
Checks for left child
:return: bool True if has left child
"""
return True if self.left is not None else False
def has_right(self):
"""
Check for right child
:return: bool: True if has right child
"""
return True if self.right is not None else False
def __str__(self):
"""
Overrride to string function
:return: String of Node
"""
return "Array: " + str([x for x in self.value]) + " Pivot:" + str(self.pivot)
class KDTree:
"""A k-dimensional binary tree for solving the nearest neighbor problem.
Attributes:
root (KDTNode): the root node of the tree. Like all other nodes in
the tree, the root has a NumPy array of shape (k,) as its value.
k (int): the dimension of the data in the tree.
"""
def __init__(self):
"""Initialize the root and k attributes."""
self.root = None
self.k = None
def find(self, data):
"""Return the node containing the data. If there is no such node in
the tree, or if the tree is empty, raise a ValueError.
"""
def _step(current):
"""Recursively step through the tree until finding the node
containing the data. If there is no such node, raise a ValueError.
"""
if current is None: # Base case 1: dead end.
raise ValueError(str(data) + " is not in the tree")
elif np.allclose(data, current.value):
return current # Base case 2: data found!
elif data[current.pivot] < current.value[current.pivot]:
return _step(current.left) # Recursively search left.
else:
return _step(current.right) # Recursively search right.
# Start the recursive search at the root of the tree.
return _step(self.root)
def insert(self, data):
"""Insert a new node containing the specified data.
Parameters:
data ((k,) ndarray): a k-dimensional point to insert into the tree.
Raises:
ValueError: if data does not have the same dimensions as other
values in the tree.
"""
if(self.root is None):
# If the tree is empty assign root and k value
new_node = KDTNode(data)
new_node.pivot = 0
self.k = len(data)
self.root = new_node
else:
if (len(data) != self.k):
# The data dimensions needs to match the tree dimensions
raise ValueError("Data must be the same dimension as", self.k)
# Find the parent of the node to insert
parent_node = self.find_insert_parent(self. root, data)
# Create new node and give it the right pivot value
new_node = KDTNode(data)
new_node.pivot = (parent_node.pivot + 1) % self.k
# Place the new node to the left or right of the parent node depending
if(parent_node.value[parent_node.pivot] > new_node.value[parent_node.pivot]):
parent_node.left = new_node
else:
parent_node.right = new_node
return
def find_insert_parent(self, cur_node, data):
"""
Resursive Function to find the correct parent node to attach a new node to
:param cur_node: KDTNode starts with root
:param data: data of new node ot insert
:return: KDTNode: the appropriate parent node to attach the new node to
"""
if(set(cur_node.value) == set(data)):
raise ValueError("Value already in the Tree")
if(cur_node.has_no_children()):
#Base Case
return cur_node
if(cur_node.has_right() and cur_node.value[cur_node.pivot] < data[cur_node.pivot]):
# Go right
return self.find_insert_parent(cur_node.right, data)
if (cur_node.has_left() and cur_node.value[cur_node.pivot] > data[cur_node.pivot]):
#Go left
return self.find_insert_parent(cur_node.left, data)
else:
return cur_node
def query(self, z):
"""Find the value in the tree that is nearest to z.
Parameters:
z ((k,) ndarray): a k-dimensional target point.
Returns:
((k,) ndarray) the value in the tree that is nearest to z.
(float) The Euclidean distance from the nearest neighbor to z.
"""
cur_node = self.root
def closest_euclid(cur_node, nearest, d):
"""
Recursive function to find the closest node by the euclidean distance
:param cur_node: KDTNode: Root
:param nearest: KDTNode: Current closest node
:param d: float: euclid distance of the current node
:return: (KDTNode, float): The node closest to z and its distance from z
"""
if cur_node is None:
return nearest, d
x = cur_node.value
i = cur_node.pivot
norm = la.norm(x - z)
if(norm < d):
nearest = cur_node
d = norm
if(z[i] < x[i]):
# Go left based on pivot
nearest, d = closest_euclid(cur_node.left, nearest, d)
if (z[i] + d > x[i]):
nearest, d = closest_euclid(cur_node.right, nearest, d)
else:
# Go right based on pivot
nearest, d = closest_euclid(cur_node.right, nearest, d)
if (z[i] - d <= x[i]):
nearest, d = closest_euclid(cur_node.left, nearest, d)
return nearest, d
nearest, d = closest_euclid(cur_node, cur_node, la.norm(cur_node.value - z))
return nearest.value, d
def __str__(self):
"""String representation: a hierarchical list of nodes and their axes.
Example: 'KDT(k=2)
[5,5] [5 5] pivot = 0
/ \ [3 2] pivot = 1
[3,2] [8,4] [8 4] pivot = 1
\ \ [2 6] pivot = 0
[2,6] [7,5] [7 5] pivot = 0'
"""
if self.root is None:
return "Empty KDT"
nodes, strs = [self.root], []
while nodes:
current = nodes.pop(0)
strs.append("{}\tpivot = {}".format(current.value, current.pivot))
for child in [current.left, current.right]:
if child:
nodes.append(child)
return "KDT(k={})\n".format(self.k) + "\n".join(strs)
class KNeighborsClassifier:
def __init__(self, n_neighbors):
if(type(n_neighbors) is not int):
raise TypeError("Needs to be an integer")
self.n_neighbors = n_neighbors
self.tree = None
self.labels = None
def fit(self, X, y):
"""
Takes in data and labels and saves them as attributes
:param X: ((m,k) ndarray): a training set of m k-dimensional points.
:param y: ((k, ) ndarray): labels.
:return:
"""
#Initialize a tree
self.tree = KDTree(X)
self.labels = y
return
def predict(self, z):
"""
Returns the most common label of the closest neighbors to z
:param z: (k) ndarray: target point to find the closest points to
:return: float, most common label among the k closest nodes
"""
distances, indices = self.tree.query(z, self.n_neighbors)
# Gets the labels for each of the closest nodes and returns the most frequent
if(self.n_neighbors == 1):
return self.labels[indices]
else:
mode = stats.mode([self.labels[i] for i in indices])
return mode.mode[0]