1.理论基础
测试图片(test image)与训练图片(training image)每个对应相同位置像素值之差的绝对值,然后求和。具体如下图所示:
2.代码
""" In this code, the parameter "X" in function "train" is different from "X" in function "predict" """
import numpy as np
class NearestNeighbor:
def __init__(self):
pass
def train(self,X,y):
""" X is N*D where each row is an example. Y is 1-dimension of size N"""
# the nearest neighbor classifier simply remembers all the training data
self.Xtr = X
self.ytr = y
def predict(self,X):
""" X is N*D where each row is an example we wish to predict label for """
num_test = X.shape[0]
# lets make sure that the output type matches the input type
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 i'th test image
# using the L1 distance (sum of absolute value differences)
distances = np.sum(np.abs(self.Xtr - X[i,:]),axis=1) # get the index with smallest distance
min_index = np.argmin(distances)
Ypred = self.ytr[min_index] # predict the label of the nearest example
return Ypred