MINST数据集
数据概况
一般在进行模型训练之前,都要做一个数据集分析的任务。这个在英文中一般缩写为EDA,也就是Exploring Data Analysis(好像是这个)。
import pandas as pd
import torch
from torchvision.utils import make_grid
import matplotlib.pyplot as plt
train_df = pd.read_csv('/Users/xiaoshunv/Desktop/MNIST_csv/train.csv')
n_train = len(train_df)
n_pixels = len(train_df.columns) - 1 #有多少列,就是多少个像素(28*28=784)
n_class = len(set(train_df['label']))#set删除重复项后,就是类别数
print('Number of training samples: {0}'.format(n_train))
print('Number of training pixels: {0}'.format(n_pixels))
print('Number of classes: {0}'.format(n_class))
# 读取测试集
test_df = pd.read_csv('/Users/xiaoshunv/Desktop/MNIST_csv/test.csv')
n_test = len(test_df)
n_pixels = len(test_df.columns)
print('Number of test samples: {0}'.format(n_test))
print('Number of test pixels: {0}'.format(n_pixels))
数据可视化
import numpy as np
from torchvision.utils import make_grid
import torch
import matplotlib.pyplot as plt
random_sel = np.random.randint(len(train_df), size=8)#从0-len(train_df),随机取八个
data = (train_df.iloc[random_sel,1:].values.reshape(-1,1,28,28)/255.)#iloc索引行,随机取八行,然后取数据(第0行是label所以不取)
grid = make_grid(torch.Tensor(data), nrow=8)#把8个图片拼到一起
plt.rcParams['figure.figsize'] = (16, 2)
plt.imshow(grid.numpy().transpose((1,2,0)))#把第0维channel数换到第三维
plt.axis('off')
plt.show()
print(*list(train_df.iloc[random_sel, 0].values), sep = ', ')
运行结果:
类别是否均衡
# 检查类别是否不均衡
plt.figure(figsize=(8,5))
plt.bar(train_df['label'].value_counts().index, train_df['label'].value_counts())
plt.xticks(np.arange(n_class))
plt.xlabel(<