如何更改mnist数据集 python

如何更改mnist数据集 python,第1张

其实就是python怎么读取binnary file

mnist的结构如下,选取train-images

TRAINING SET IMAGE FILE (train-images-idx3-ubyte):

[offset] [type] [value] [description]

0000 32 bit integer 0x00000803(2051) magic number

0004 32 bit integer 60000number of images

0008 32 bit integer 28 number of rows

0012 32 bit integer 28 number of columns

0016 unsigned byte ?? pixel

0017 unsigned byte ?? pixel

........

xxxx unsigned byte ?? pixel

也就是之前我们要读取4个 32 bit integer

试过很多方法,觉得最方便的,至少对我来键野说还是使用

struct.unpack_from()

filename = 'train-images.idx3-ubyte'

binfile = open(filename , 'rb')

buf = binfile.read()

先使用二进制方式把文件都读进来

index = 0

magic, numImages , numRows , numColumns = struct.unpack_from('>IIII' , buf , index)

index += struct.calcsize('>IIII')

然后使用struc.unpack_from

'>IIII'是说使用大端法读取4个unsinged int32

然后读取一个图片测试是否读取成功

im = struct.unpack_from('稿戚喊>784B' ,buf, index)

index += struct.calcsize('>784B')

im = np.array(im)

im = im.reshape(28,28)

fig = plt.figure()

plotwindow = fig.add_subplot(111)

plt.imshow(im , cmap='gray')

plt.show()

'>784B'的意思就是用大端法读取784个unsigned byte

完整代码如下

import numpy as np

import struct

import matplotlib.pyplot as plt

filename = 'train-images.idx3-ubyte'

binfile = open(filename , 'rb')

buf = binfile.read()

index = 0

magic, numImages , numRows , numColumns = struct.unpack_from('>IIII' , buf , index)

index += struct.calcsize('>IIII')

im = struct.unpack_from('>784B' ,buf, index)

index += struct.calcsize('>784B')

im = np.array(im)

im = im.reshape(28,28)

fig = plt.figure()

plotwindow = fig.add_subplot(111)

plt.imshow(im , cmap='gray')

plt.show()

只是为了测试是否成功所以只读仔拆了一张图片

Python中的input函数用于向用户提供一个输入的入口。

input() 函数用于向用户生成一条提示,然后获取悉昌信用户输入的内容。由于 input() 函数总会将用户输入的内容放入迅腊字符串中,因此用户可以

输入任何内容,input() 函数总是返回一个字符串。

例如如下程序:

msg = input("请输入你的值:")

print (type(msg))

print(msg)

运行该程序,我们输入一个整数,运行过程如下:

请输入你的值:2

<class 'str'>

2

无论输入哪种内容,input() 函数都返回字睁轮符串,程序总会将用户输入的内容转换成字符串。

关于python输入数据的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11984329.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存