博客
关于我
Python 手写数字识别-1
阅读量:802 次
发布时间:2023-03-07

本文共 2268 字,大约阅读时间需要 7 分钟。

MNIST 数据集处理与可视化

1. 导入必要的库

为了完成本文的数据处理与图形可视化,我们需要引入以下主要库:

import osimport os.pathimport urllibimport gzipimport shutilimport numpy as npimport matplotlib.pyplot as plt

2. 下载并解压 MNIST 数据集

MNIST 是一个常用的手写数字数据库,包含训练集和测试集。我们从 Yann LeCun 的官方网站下载相关文件。以下是下载并解压文件的具体步骤:

if not os.path.exists('mnist'):    os.mkdir("mnist")def download_and_gzip(name):    if not os.path.exists(name + '.gz'):        urllib.urlretrieve('http://yann.lecun.com/exdb/' + name + '.gz', name + '.gz')    if not os.path.exists(name):        with gzip.open(name + '.gz', "rb") as f_in, open(name, 'wb') as f_out:            shutil.copyfileobj(f_in, f_out)download_and_gzip("mnist/train-images-idx3-ubyte")download_and_gzip('mnist/train-labels-idx1-ubyte')download_and_gzip('mnist/t10k-images-idx3-ubyte')download_and_gzip("mnist/t10k-labels-idx1-ubyte")

3. 读取数据文件

接下来,我们读取训练集和测试集的图像数据以及标签数据。

# 读取训练集图像数据loaded = np.fromfile("mnist/train-images-idx3-ubyte", dtype='uint8')loaded.shape
# 读取训练集标签数据loaded = np.fromfile('mnist/train-labels-idx1-ubyte', dtype='uint8')loaded.shape
# 读取测试集图像数据loaded = np.fromfile("mnist/t10k-images-idx3-ubyte", dtype='uint8')text_x = loaded[16:].reshape(10000, 28, 28)print(text_x.shape)
# 读取测试集标签数据loaded = np.fromfile("mnist/t10k-labels-idx1-ubyte", dtype='uint8')test_y = loaded[8:].reshape(10000)print(test_y.shape)

4. 数据处理与可视化

将图像数据从 1D 转换为 3D 格式,便于后续处理和可视化。

train_x = loaded[16:].reshape(60000, 28, 28)text_x = loaded[16:].reshape(10000, 28, 28)print(train_x.shape)print(text_x.shape)

为了更直观地观察图像,我们可以使用 matplotlib 进行可视化。

plt.imshow(train_x[0], cmap="BrBG")plt.axis("off")plt.show()

我们也可以选择将图像按行和列分组进行批量显示。

def plot_images(images, row, col):    show_image = np.vstack(np.split(np.hstack(images[:col*row]), row, axis=1))    plt.imshow(show_image, cmap='binary')    plt.axis("off")    plt.show()row, col = 4, 5plot_images(train_x, row, col)

通过上述代码,我们可以方便地查看训练集和测试集的图像数据分布。

5. MNIST 数据集的特点

MNIST 数据集包含 60,000 个训练样本和 10,000 个测试样本,每个样本包含 28x28 的图像数据。标签数据以单个字节的编码形式存储,因此我们需要将其转换为整数类型进行处理。

train_labels = train_y[0:20].reshape(4, 5)print(train_labels)

6. 使用 matplotlib 进行图形绘制

为了更直观地展示数据,我们可以使用 matplotlib 库进行图形绘制。以下是一些常用的绘图方法和示例:

# 选择颜色映射cmap = "BrBG"# 绘制图像plt.imshow(train_x[0], cmap=cmap)# 去掉坐标轴plt.axis("off")# 显示图像plt.show()

通过以上方法,我们可以清晰地看到 MNIST 数据集中的图像分布情况,方便后续的模型训练和验证。

转载地址:http://opofk.baihongyu.com/

你可能感兴趣的文章
python | werkzeug,一个不可思议的 Python 库!
查看>>
python | xlsxwriter,一个实用的 Python 库!
查看>>
python | xlwings,一个非常实用的 Excel 相关的 Python 库!
查看>>
python | 一文看懂Python闭包机制与变量作用域规则
查看>>
python读取含中文的json
查看>>
python | 如何用Python锁避免并发错误?
查看>>
python | 提升代码迭代速度的Python重载方法
查看>>
python | 深入理解Python并发编程中的GIL限制与解决方案
查看>>
Python | 爬虫实战——亚马逊搜索页监控(附详细源码)
查看>>
python | 高效使用Python工具自动生成模块文档的秘诀
查看>>
python 一个list去除另一个list中的值
查看>>
python 三大框架的 介绍。
查看>>
Python 下载的 11 种姿势,一种比一种高级!
查看>>
python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
查看>>
Python 中 3 个不可思议的返回功能
查看>>
python 中 dict 的另一种用法
查看>>
Python 中 PIL 读取图片出现异常旋转的解决方法
查看>>
python 中os.path.join 双斜杠的解决办法
查看>>
Python 中Semaphore 信号量对象、Event事件、Condition
查看>>
python 中with的使用及样例
查看>>