1. First classifier:Nearest Neighbor Classifier

数据集:CIFAR-10,提供10个label,50,000 个训练样本,10,000个测试样本。
K-NN 中最重要的一个部分就是 距离的计算方法,下面是曼哈顿距离的计算方法。
import numpy as np
class NearestNeighbor:
def __init__(self):
pass
def train(self,X,y):
'''X is NxD where each row is an example. Y is 1-dimension of size N'''
# the nearest neighbor classifier simple remembers all the training data
self.Xtr = X
self.ytr = y
def predict(self,X):
'''X is NxD 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 range(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
另一种距离计算方法,欧氏距离


验证方法—K折交叉验证

K-NN 在图片上并不常用

2. linear classification
DeepLearning CS231n CS231n ImageClassification
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!