原因是下面这段代码调用数据集的时候出错,
cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'因为该文件夹下没有数据集文件。 解决方法是去百度网盘下载CIFAR数据集,提取码为qd4v 然后直接用绝对地址调用数据集即可。
# Load the raw CIFAR-10 data. cifar10_dir = 'F:\CIFAR10' X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir) # As a sanity check, we print out the size of the training and test data. print('Training data shape: ', X_train.shape) print('Training labels shape: ', y_train.shape) print('Test data shape: ', X_test.shape) print('Test labels shape: ', y_test.shape)idxs = np.flatnonzero(y_train == y)第一次循环y=0,np.flatnonzero的作用是将矩阵转成扁平非0形式,这句主要是将训练集中类别为plane的所有图片的位置找到。 然后idxs = np.random.choice(idxs, samples_per_class, replace=False)是从中随机选出7张,np.random.choice()括号后面的三个参数分别表示从idxs中随机选出samples_per_class个,replace=False表示抽出后不放回,保证选出的图片不会重复。 第一个for循环主要是选出每个类别的7张图片,第二个for循环将每个类别的图片按列显示出来。
其中X_train = np.reshape(X_train, (a,b))表示将X_train数组变成a行b列的形式,若a,b中出现了-1,数组新的shape属性应该要与原来的配套,如果出现-1的话,那么Numpy会根据剩下的维度计算出数组的另外一个shape属性值。 比如此处,X_train.shape[0]得到X_train数组的第一个维度的值,因为X_train本身是一维的,-1后不会再有其他列出现,这样就可以将X_train数组整形为一行。
建立一个4×2的矩阵c, c.shape[1] 为第一维的长度,c.shape[0] 为第二维的长度
>>>c = array([[1,1],[1,2],[1,3],[1,4]]) >>>c.shape (4, 2) >>>c.shape[0] 4 >>>c.shape[1] 2numpy.argmin(a, axis=None, out=None)
给出axis方向最小值的下标
>>> a = np.arange(6).reshape(2,3) >>> a array([[0, 1, 2], [3, 4, 5]]) >>> np.argmin(a) 0 >>> np.argmin(a, axis=0) array([0, 0, 0]) >>> np.argmin(a, axis=1) array([0, 0])argsort函数返回的是数组值从小到大的索引值
One dimensional array:一维数组 >>> x = np.array([3, 1, 2]) >>> np.argsort(x) array([1, 2, 0]) Two-dimensional array:二维数组 >>> x = np.array([[0, 3], [2, 2]]) >>> x array([[0, 3], [2, 2]]) >>> np.argsort(x, axis=0) #按列排序 array([[0, 1], [1, 0]]) >>> np.argsort(x, axis=1) #按行排序 array([[0, 1], [0, 1]])10.python numpy.linalg.norm函数的用法 1、linalg = linear(线性)+ algebra(代数),norm则表示范数。
difference = np.linalg.norm(dists - dists_one, ord='fro') print('Difference was: %f' % (difference, )) if difference < 0.001: print('Good! The distance matrices are the same') else: print('Uh-oh! The distance matrices are different')其中,ord='fro'表示使用Frobenius范数,求的是两个矩阵所有元素的差值的均方根,即它们之间的欧氏距离(L2)。
numpy.reshape( )用法总结 python: numpy–函数 shape用法 CS231n 第一次作业KNN中本地CIFAR10数据集的载入 numpy——hsplit函数、vsplit函数、array_split函数 Python-辨析type/dtype/astype用法 numpy.argmin使用
