mirror of
https://github.com/autistic-symposium/tensorflow-for-deep-learning-py.git
synced 2025-05-11 03:04:59 -04:00
clean up
This commit is contained in:
parent
da1b22cf4a
commit
db89b720d8
13 changed files with 2873 additions and 0 deletions
23
Numpy/NeareastNeighbor.py
Normal file
23
Numpy/NeareastNeighbor.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
import numpy as np
|
||||
|
||||
class NearestNeighbor(object):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def train(self, X, y):
|
||||
self.Xtr = X
|
||||
self.ytr = y
|
||||
|
||||
def predict(self, X):
|
||||
num_test = X.shape[0]
|
||||
Ypred = np.zeros(num_test, dtype = self.ytr.dtype)
|
||||
|
||||
# loop over all test rows
|
||||
for i in xrange(num_test):
|
||||
# find the nearest training image to the i'th test image
|
||||
# using the L1 distance (sum of absolute value differences)
|
||||
distances = np.sum(np.abs(self.Xtr - X[i,:]), axis = 1)
|
||||
min_index = np.argmin(distances) # get the index with smallest distance
|
||||
Ypred[i] = self.ytr[min_index] # predict the label of the nearest example
|
||||
|
||||
return Ypred
|
Loading…
Add table
Add a link
Reference in a new issue