mnist数据集介绍

背景

MNIST数据集来自美国国家标准与技术研究所收集

下载

tensorflow2.0版本以后,mnist数据集可以通过keras.datasets的API接口直接下载

from tensorflow import keras
import matplotlib.pyplot as plt

# 加载mnist数据集
(train_images, train_labels), (test_images, test_labels) = keras.datasets.mnist.load_data()

下载好mnist数据集以后,可以打印输出训练集、测试集、的图像大小及标签信息

from tensorflow import keras
import matplotlib.pyplot as plt
# 加载mnist数据集
'''
.shape的使用:
    print(img.shape)        # 返回图像的高度、宽度以及通道数
    print(img.shape[0])     # 元组的第一个元素为图片数量
    print(img.shape[1])     # 元组的第一列元素为维度
    print(img.shape[2])     # 元组的第二列元素为列数
'''
(train_images, train_labels), (test_images, test_labels) = keras.datasets.mnist.load_data()
print("train_images info:", train_images.shape, train_images.shape[0], train_images.shape[1],train_images.shape[2])
print("train_labels info:", train_labels.shape, train_labels.shape[0])
print("test_images info:", test_images.shape, test_images.shape[0], test_images.shape[1],test_images.shape[2])
print("test_labels info:", test_labels.shape, test_labels.shape[0])

输出结果:

train_images info: (60000, 28, 28) 60000 28 28
train_labels info: (60000,) 60000
test_images info: (10000, 28, 28) 10000 28 28
test_labels info: (10000,) 10000

从上面运行结果可以看出出:
训练集样本有60000张图片,每张图片大小为28*28;
训练集标签有60000个;
测试集与训练集类似。

可视化手写体图片

下面利用matplotlib函数库绘制训练集第一张图片:

# 绘制训练集第一张图片
# print(train_images[0]) # 第一张图像矩阵
plt.imshow(train_images[0])
plt.show()

在这里插入图片描述
查看训练集第一张图片对应的标签:

# 查看第一张训练集图片的标签
print(train_labels[0])

在这里插入图片描述
代码如下:

# @function: mnist数据集
# @Description:一只萤火虫
from tensorflow import keras
import matplotlib.pyplot as plt
# 加载mnist数据集
'''
.shape的使用:
    print(img.shape)        # 返回图像的高度、宽度以及通道数
    print(img.shape[0])     # 元组的第一个元素为图片数量
    print(img.shape[1])     # 元组的第一列元素为维度
    print(img.shape[2])     # 元组的第二列元素为列数
'''
# 查看mnist数据集的训练集、测试集的大小,查看第一张图片的信息,并绘制第一张图片
(train_images, train_labels), (test_images, test_labels) = keras.datasets.mnist.load_data()
print("train_images info:", train_images.shape, train_images.shape[0], train_images.shape[1],train_images.shape[2])
print("train_labels info:", train_labels.shape, train_labels.shape[0])
print("test_images info:", test_images.shape, test_images.shape[0], test_images.shape[1],test_images.shape[2])
print("test_labels info:", test_labels.shape, test_labels.shape[0])
# 查看训练集第一张图片
print(train_images[0])
print(train_labels[0])
plt.imshow(train_images[0])
plt.show()
# 查看第一张训练集图片的标签
print(train_labels[0])
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值