2025年python中idx是什么意思_使用Python解析MNIST数据集(IDX文件格式)

python中idx是什么意思_使用Python解析MNIST数据集(IDX文件格式)前言 最近在学习 Keras 要使用到 LeCun 大神的 MNIST 手写数字数据集 直接从官网上下载了 4 个压缩包 MNIST 数据集 解压后发现里面每个压缩包里有一个 idx ubyte 文件 没有图片文件在里面 回去仔细看了一下官网后发现原来这是 IDX 文件格式 是一种用来存储向量与多维度矩阵的文件格式 IDX 文件格式 官网上的介绍如下 THE IDX FILE

大家好,我是讯享网,很高兴认识大家。

前言

最近在学习Keras,要使用到LeCun大神的MNIST手写数字数据集,直接从官网上下载了4个压缩包:

84f72791806f
讯享网

MNIST数据集

解压后发现里面每个压缩包里有一个idx-ubyte文件,没有图片文件在里面。回去仔细看了一下官网后发现原来这是IDX文件格式,是一种用来存储向量与多维度矩阵的文件格式。

IDX文件格式

官网上的介绍如下:

THE IDX FILE FORMAT

the IDX file format is a simple format for vectors and multidimensional matrices of various numerical types.

The basic format is

magic number

size in dimension 0

size in dimension 1

size in dimension 2

.....

size in dimension N

data

The magic number is an integer (MSB first). The first 2 bytes are always 0.

The third byte codes the type of the data:

0x08: unsigned byte

0x09: signed byte

0x0B: short (2 bytes)

0x0C: int (4 bytes)

0x0D: float (4 bytes)

0x0E: double (8 bytes)

The 4-th byte codes the number of dimensions of the vector/matrix: 1 for vectors, 2 for matrices....

The sizes in each dimension are 4-byte integers (MSB first, high endian, like in most non-Intel processors).

The data is stored like in a C array, i.e. the index in the last dimension changes the fastest.

解析脚本

根据以上解析规则,我使用了Python里的struct模块对文件进行读写(如果不熟悉struct模块的可以看我的另一篇博客文章《Python中对字节流/二进制流的操作:struct模块简易使用教程》)。IDX文件的解析通用接口如下:

# 解析idx1格式

def decode_idx1_ubyte(idx1_ubyte_file):

"""

解析idx1文件的通用函数

:param idx1_ubyte_file: idx1文件路径

:return: np.array类型对象

"""

return data

def decode_idx3_ubyte(idx3_ubyte_file):

"""

解析idx3文件的通用函数

:param idx3_ubyte_file: idx3文件路径

:return: np.array类型对象

"""

return data

针对MNIST数据集的解析脚本如下

小讯
上一篇 2025-01-29 14:29
下一篇 2025-02-14 15:42

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/40344.html