Keras MNIST手写数字识别数据集

这篇博客介绍了如何使用Keras处理MNIST手写数字识别数据集,包括数据下载、读取、预处理图像和标签的过程,以及数据集的结构和内容展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

《TensorFlow+keras深度学习人工智能实践应用》读书笔记2

1.创建Keras程序,下载并读取MINIST数据

#导入keras相关模块
import numpy as np
import pandas as pd
from keras.utils import np_utils
np.random.seed(10)

#导入MNIST模块
from keras.datasets import mnist

#MNIST数据下载
(X_train_image,y_train_label),\
(X_test_image,y_test_label)=mnist.load_data()

#查看MNIST数据
print('train data = ',len(X_train_image))
print(' test data = ',len(X_test_image))

运行完成后能在本地C:\Users\dongzijing\.keras\datasets找到下载的文件mnist.npz

最后print打印 train data=60000,test data = 10000,由结果可知 train训练数据60000项,test测试数据10000项

2.训练数据有images和label组成,iamges是数字图片,labels是对应的数字。

3.查看训练数据中的images和label

#定义plot_image查看数字图像
import matplotlib.pyplot as plt
def plot_image(image):
    fig = plt.gcf()
    fig.set_size_inches(2, 2)
    plt.imshow(image, cmap='binary')
    plt.show()

#执行plot_image函数,查看第0个图像
plot_image(X_train_image[0])

执行后,显示出数字图像如下

#查看对应的第0个label
y_train_label[0]

打印5

4.查看多项训练数据images和label

#创建plot_images_labels_prediction()函数
import matplotlib.pyplot as plt
def plot_images_labels_prediction(images,labels,prediction,idx,num=10):
    fig = plt.gcf()
    #设置图形大小
    fig.set_size_inches(12,14)
    #如果显示项数参数大于25则设为2,以免发生错误
    if num>25:num=25
    #画出num个数字图形
    for i in range(0,num):
        ax = plt.subplot(5,5,i+1)#建立subplot子图形为5行5列
        ax.imshow(images[idx],cmap='binary')#画出subplot子图形
        title = "label="+str(labels[idx])
        if len(prediction)>0:#如果传入预测结果
            title += ",predit = "+str(prediction[idx])
            
        ax.set_title(title,fontsize=10)
        ax.set_xticks([])#设置不显示刻度
        ax.set_yticks([])
        idx+=1
    pit.show()

plot_images_labels_prediction(X_train_image,y_train_label,[],0,10)

运行完成显示前10个图形及label

5.features图像预处理

分为两个步骤:

      1将28X28的数字图像以reshape转换为一维向量,长度为784,并且转换为float

      2.image的数字标准化

#查看每一个数字图像的shape
print('x_train_image: ',X_train_image.shape)
print('y_train_label: ',y_train_label.shape)

x_train_image: (60000, 28, 28) y_train_label: (60000,)

#转换为一维向量
x_Train = X_train_image.reshape(60000,784).astype('float32')
x_Test = X_test_image.reshape(10000,784).astype('float32')

#查看数字图像为784个浮点数
print('x_train: ',x_Train.shape)
print('x_test: ',x_Test.shape)
x_train:  (60000, 784)
x_test:  (10000, 784)
#查看images第0项内容
X_train_image[0]

array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, .....

6.label数据预处理

#取前5项
y_train_label[:5]

#one-hot encoding转换
y_TrainOneHot = np_utils.to_categorical(y_train_label)
y_TestOneHot = np_utils.to_categorical(y_test_label)

#查看
y_TrainOneHot[:5]

第一行输出:

array([5,0,4,1,9], dtype=uint8)

最后一行输出:

array([

[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],

[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],

[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],

[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],

[0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]], dtype=float32)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值