1.python2和python3的版本不兼容,有一些语法不同,需要改
2.关于导入cifar-10数据库,我是在http://www.cs.toronto.edu/~kriz/cifar.html下载的数据集,直接把数据集拖到代码存储的根目录的位置就可以了
import pickle as p
import matplotlib.pyplot as plt
import numpy as np
# NearestNeighbor class
class NearestNeighbor(object):
def __init__(self):
pass
def train(self, X, y):
""" X is N x 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 x 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 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

本文介绍了如何在CIFAR-10数据集上实现KNN算法。过程中需要注意Python2与Python3的语法差异,并详细说明了如何导入并处理CIFAR-10数据集。
最低0.47元/天 解锁文章

2168

被折叠的 条评论
为什么被折叠?



