python:mnist 数据集下载,parse

pip install mnist

pip show mnist

Name: mnist

Version: 0.2.2

Summary: Python utilities to download and parse the MNIST dataset

Home-page: https://github.com/datapythonista/mnist

Author: Marc Garcia

Author-email: garcia.marc@gmail.com

License: BSD

Location: d:\python39\lib\site-packages

Requires: numpy

https://opendatalab.com/MNIST/

复制代码
D:\python>TREE /F data

D:\PYTHON\DATA
└─MNIST
    └─raw
            t10k-images-idx3-ubyte
            t10k-images-idx3-ubyte.gz
            t10k-labels-idx1-ubyte
            t10k-labels-idx1-ubyte.gz
            train-images-idx3-ubyte
            train-images-idx3-ubyte.gz
            train-labels-idx1-ubyte
            train-labels-idx1-ubyte.gz

修改 D:\Python39\Lib\site-packages\mnist\init.py

找到第143行:fname = download_file(fname, target_dir=target_dir, force=force) 注释掉,修改为:

python 复制代码
def download_and_parse_mnist_file(fname, target_dir=None, force=False):
    """Download the IDX file named fname from the URL specified in dataset_url
    and return it as a numpy array.

    Parameters
    ----------
    fname : str
        File name to download and parse

    target_dir : str
        Directory where to store the file

    force : bool
        Force downloading the file, if it already exists

    Returns
    -------
    data : numpy.ndarray
        Numpy array with the dimensions and the data in the IDX file
    """
    #fname = download_file(fname, target_dir=target_dir, force=force)
    if target_dir == None:
        target_dir = './data/MNIST/raw'
    fname = os.path.join(target_dir, fname)
        
    fopen = gzip.open if os.path.splitext(fname)[1] == '.gz' else open
    with fopen(fname, 'rb') as fd:
        return parse_idx(fd)

编写一个测试脚本 mnist_test.py 如下

python 复制代码
import mnist

print('mnist train shape:')
train_X = mnist.train_images()
train_y = mnist.train_labels()
print(train_X.shape, train_y.shape)

print('mnist test shape:')
test_X = mnist.test_images()
test_y = mnist.test_labels()
print(test_X.shape, test_y.shape)

运行 cmd

python mnist_test.py

python 复制代码
D:\python> python mnist_test.py
mnist train shape:
(60000, 28, 28) (60000,)
mnist test shape:
(10000, 28, 28) (10000,)

编写一个显示手写数字图像的脚本 mnist_image1.py 如下

python 复制代码
from PIL import Image
import numpy as np
import mnist

images = mnist.train_images()
img = Image.fromarray(images[0]).resize((280, 280), Image.Resampling.NEAREST)
#img.save('mnist_img1.jpg') # 可选:保存图片
img.show() # 可选:显示图片 

python mnist_image1.py

相关推荐
2301_809204708 分钟前
mysql在docker容器中如何部署_利用docker-compose快速启动
jvm·数据库·python
2301_800976931 小时前
正则表达式
开发语言·python·正则表达式
码界奇点1 小时前
基于Python的新浪微博数据爬虫系统设计与实现
数据库·爬虫·python·毕业设计·新浪微博·源代码管理
AI木马人2 小时前
1.人工智能实战:大模型推理接口响应慢?从模型加载到 FastAPI 部署的完整优化方案
人工智能·python·fastapi
青少儿编程课堂2 小时前
2026青少儿信息素养大赛备赛指南!Python/Scratch/C++备考要点
开发语言·c++·python
用户8356290780512 小时前
使用 Python 设置 Excel 数据验证
后端·python
Nick_zcy2 小时前
小说在线阅读网站和小说管理系统 · 功能全解析
java·后端·python·springboot·ruoyi
*Lisen2 小时前
从零手写 FlashAttention(PyTorch实现 + 原理推导)
人工智能·pytorch·python
用户8356290780513 小时前
用 Python 轻松在 Excel 工作表中应用条件格式
后端·python
red1giant_star3 小时前
Python根据文件后缀统计文件大小、找出文件位置(仿Everything)
后端·python