mirror of
https://github.com/autistic-symposium/tensorflow-for-deep-learning-py.git
synced 2025-05-12 11:42:14 -04:00
Clean up this scratch space
This commit is contained in:
parent
1de1667900
commit
ed0cead015
62 changed files with 39650 additions and 13 deletions
23
numpy_examples/NeareastNeighbor.py
Normal file
23
numpy_examples/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