cs231n assignment1_Q1_KNN Classifier

本文记录了作者学习cs231n课程并完成k-最近邻(kNN)分类器作业的过程。内容包括环境配置、数据集下载、Jupyter Notebook的使用以及kNN分类器的实现和性能评估。通过实验,作者观察了不同k值对分类准确率的影响。
摘要由CSDN通过智能技术生成

今天开始将进行学习cs231n课程并完成相关的作业,在此记录。

配置环境
首先在做作业之前,需要配置作业相关的环境才能进行,我的步骤如下:

  1. 下载并安装anaconda:https://www.anaconda.com/download/ ,这是一个Python的科学包,它集成了很多机器学习、深度学习要用的相关库,也自带了一个Python。
  2. 安装Jupyter notebook,直接可以在命令行安装,pip install jupyter
  3. 下载数据集CIFAR10,网址:http://www.cs.toronto.edu/~kriz/cifar.html
  4. 在有Pycharm的前提下,在Pycharm上使用Jupyter notebook进行编写。
    Jupyter这个工具可以及时在工作界面上看到运行的结果。

做作业

题目:

k-最近邻(kNN)练习

完成并将完成的工作表(包括其输出和工作表之外的任何支持代码)与您的作业提交一起提交。 有关详细信息,请参阅课程网站上的作业页面。

kNN分类器包括两个阶段:

  • 在训练期间,分类器获取训练数据并简单地记住它
  • 在测试期间,kNN通过与所有训练图像进行比较并且转移k个最相似训练示例的标签来对每个测试图像进行分类
  • k的值是交叉验证的

在本练习中,您将实现这些步骤并理解基本的图像分类管道,交叉验证,并获得编写高效矢量化代码的熟练程度。

步骤

  1. 到官网下载作业包,网址:https://github.com/cs231n/cs231n.github.io/blob/master/assignments/2017/assignment1.md
    然后解压。
  2. 进入pycharm,新建项目到之前步骤1解压的目录下,将下载的CIFAR10压缩包解压到cs231n\datasets下。
  3. 在pycharm中打开assignment1/knn.ipynb进行编写。

下面贴上代码

knn.ipynb

import random
import numpy as np
from cs231n.data_utils import load_CIFAR10
import matplotlib.pyplot as plt


# This is a bit of magic to make matplotlib figures appear inline in the notebook
# rather than in a new window.
%matplotlib inline 

plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'


# Some more magic so that the notebook will reload external python modules;
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
# Load the raw CIFAR-10 data.
cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'
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)

Training data shape: (50000, 32, 32, 3)
Training labels shape: (50000,)
Test data shape: (10000, 32, 32, 3)
Test labels shape: (10000,)

# Visualize some examples from the dataset.
# We show a few examples of training images from each class.
classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
num_classes = len(classes)
samples_per_class = 7

#############
#
# enumerate函数示例:
# seasons = ['Spring', 'Summer', 'Fall', 'Winter']
# >>> list(enumerate(seasons))
# [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
#
#############

for y, cls in enumerate(classes):  
    idxs = np.flatnonzero(y_train == y) #此方法返回了训练集中有相同标签的索引值
    idxs = np.random.choice(idxs, samples_per_class, replace=False) #产生一个随机采样
    for i, idx in enumerate(idxs):
        plt_idx = i * num_classes + y + 1
        plt.subplot(samples_per_class, num_classes, plt_idx) #行数,列数,每行的第几个图像
        plt.imshow(X_train[idx].astype('uint8'))
        plt.axis('off')
        if i == 0:
            plt.title(cls)
plt.show()

在这里插入图片描述

# Subsample the data for more efficient code execution in this exercise
#为了后面减少计算量,随机采样数据集,训练集采样5000张,测试集采样500张
num_training = 5000
mask = list(range(num_training))
X_train = X_train[mask]
y_train = y_train[mask]

num_test = 500
mask = list(range(num_test))
X_test = X_test[mask]
y_test = y_test[mask]
# Reshape the image data into rows
#将32*32*3的图片reshape成一行
X_train = np.reshape(X_train, (X_train.shape[0]</
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值