Python解析MNIST数据集
来源:互联网 发布:热血江湖衣服强化数据 编辑:程序博客网 时间:2024/06/06 02:15
#coding=UTF-8import numpy as npimport structimport matplotlib.pyplot as pltdef parese_idx3(idx3_file): """ idx3文件解析方法 :param idx3_file: idx3文件路径 :return: 数据集 """ # 读取二进制数据 bin_data = open(idx3_file, 'rb').read() # 解析文件头信息 magic、imgs、height、width # '>IIII'是说使用大端法读取4个unsinged int32 offset = 0 fmt_header = '>iiii' magic, imgs, height, width = struct.unpack_from(fmt_header, bin_data, offset) print ('magic:%d, imgs: %d, heightXwidth: %dX%d' % (magic, imgs, height, width)) # 解析数据集 image_size = height * width offset += struct.calcsize(fmt_header) fmt_image = '>' + str(image_size) + 'B' images = np.empty((imgs, height, width)) for i in range(imgs): if (i + 1) % 10000 == 0: print ('已解析 %d' % (i + 1) + '张'); images[i] = np.array(struct.unpack_from(fmt_image, bin_data, offset)).reshape((height, width)) offset += struct.calcsize(fmt_image) return imagesdef parese_idx1(idx1_file): """ idx1文件解析方法 :param idx1_file: idx1文件路径 :return: 数据集 """ # 读取二进制数据 bin_data = open(idx1_file, 'rb').read() # 解析文件头信息 magic、imgs offset = 0 fmt_header = '>ii' magic, imgs = struct.unpack_from(fmt_header, bin_data, offset) print ('magic:%d, imgs: %d' % (magic, imgs)) # 解析数据集 offset += struct.calcsize(fmt_header) fmt_image = '>B' labels = np.empty(imgs) for i in range(imgs): if (i + 1) % 10000 == 0: print ('已解析 %d' % (i + 1) + '张') labels[i] = struct.unpack_from(fmt_image, bin_data, offset)[0] offset += struct.calcsize(fmt_image) return labelsimgs = parese_idx3("ubyte/t10k-images.idx3-ubyte");labs = parese_idx1("ubyte/t10k-labels.idx1-ubyte");for i in range(10): print(labs[i]) plt.imshow(imgs[i]) plt.show()
PS:
t10k-images.idx3-ubyte = img1
t10k-labels.idx1-ubyte = lab1
lab1是img1的标签信息
阅读全文
0 0
- Python解析MNIST数据集
- Python解析lmdb格式mnist数据集
- 使用Python解析MNIST数据集(IDX文件格式)
- matlab 解析 mnist 数据集
- 读取 mnist 数据集 --- python
- python读取MNIST数据集
- C语言解析MNIST数据集
- mnist数据集转成图片 Python 实现
- python读取MNIST image数据
- python读取MNIST image数据
- 使用Python将MNIST数据集转化为图片
- python 实现 softmax分类器(MNIST数据集)
- 识别MNIST数据集:用Python实现神经网络
- MNIST数据集规则化
- mnist手写数据集
- MNIST数据集分析
- 详解 MNIST 数据集
- tensorflow MNIST数据集
- Window系统下安装tomcat图文教程
- 子框架页面跳出到父框架页面代码
- 安装NERDTree
- mysql远程连接
- 推荐几款有趣的实用app
- Python解析MNIST数据集
- 任意次方的后三位,打鱼晒网,计算某年的天数!
- 转载几篇 Angular2 快速入门博文
- 安卓微信分享流程
- spring boot 配置 log4j2
- Python 的闭包
- 第四周项目一 建立单链表
- maven中pom.xml文件统一修改jdk的插件
- 在linux上使用nexus搭建maven私服,详细步骤