读取mnist数据集方法大全(train-images-idx3-ubyte.gz,train-labels.idx1-ubyte等)(python读取gzip文件)
文章目录gzip包keras读取mnist数据集本地读取mnist数据集下载数据集解压读取不解压读取读取bytes数据gzip包如果仅仅是读取.gz文件使用gzip包即可。例子:当前目录有一个input.gz文件,用以下代码来读取:import gzipwith gzip.open('input.gz') as file:all_content = file.read()这样input.gz的文件
·
注:import导入的包如果未安装使用pip安装
gzip包
如果仅仅是读取.gz文件使用gzip包即可。
例子:当前目录有一个input.gz文件,用以下代码来读取:
import gzip
with gzip.open('input.gz') as file:
all_content = file.read()
这样input.gz的文件都读取到了all_content里面
keras读取mnist数据集
from tensorflow import keras
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
本地读取mnist数据集
下载数据集
数据集下载地址数据集界面:
![](https://i-blog.csdnimg.cn/blog_migrate/84bba3617b6b86f970f6c567034a6a2a.png)
解压读取
方法一
from mnist import MNIST
mndata = MNIST('samples')
images, labels = mndata.load_training()
# or
images, labels = mndata.load_testing()
index = random.randrange(0, len(images)) # choose an index ;-)
print(mndata.display(images[index]))
方法二
将.gz文件解压后读取
from mlxtend.data import loadlocal_mnist
import platform
if not platform.system() == 'Windows':
X, y = loadlocal_mnist(
images_path='train-images.idx3-ubyte',
labels_path='train-labels.idx1-ubyte')
else:
X, y = loadlocal_mnist(
images_path='train-images.idx3-ubyte',
labels_path='train-labels.idx1-ubyte')
print('Dimensions: %s x %s' % (X.shape[0], X.shape[1]))
print('\n1st row', X[0])
gzip包读取
import gzip
import numpy as np
import matplotlib.pyplot as plt
with gzip.open('train-images-idx3-ubyte.gz') as all_img:
all_img = all_img.read()
# print(all_img[:4])
# print((len(all_img)-16)/784)
img1 = all_img[16:16+784]
img = []
for i in range(28):
for j in range(28):
img.append(img1[28*i+j])
#print(img)
img = np.array(img).reshape(28, 28)
print(img.shape)
plt.imshow(img)
plt.show()
读取bytes数据
参考stackoverflowConvert bytes to a string
>>> b"abcde"
b'abcde'
# utf-8 is used here because it is a very common encoding, but you
# need to use the encoding your data is actually in.
>>> b"abcde".decode("utf-8")
'abcde'
![Logo](https://devpress.csdnimg.cn/79de2bf0b7994defa4242ef90d5513fa.jpg)
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)