import numpy as np
from matplotlib import pyplot as plt
from PIL import Image
import os
def load_data(path='mnist.npz'):
f = np.load(path)
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
f.close()
return (x_train, y_train), (x_test, y_test)
(x_train ,y_train),( x_test ,y_test)=load_data("C:/Python35/学习深度/KARES/mnist.npz")
def showImg(mg,index):
#plt.imshow(mg[index])
#plt.show()
print(mg[index].dtype)
im = Image.fromarray(mg[index].astype(mg[index].dtype))
if os.path.isdir("./"+str(y_train[index]))==False:
os.mkdir("./"+str(y_train[index]))
im.save("./"+str(y_train[index])+"/"+str(index)+".jpg")
print(x_train.ndim)
print(x_train.shape[0])
for _is in range(x_train.shape[0]):
print(_is)
showImg(x_train, _is)
#print(x_train[_is])
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
本文介绍了一个Python脚本,用于从MNIST数据集中加载训练和测试数据,并将图像数据展示为图片文件。该脚本使用numpy进行数据处理,matplotlib和PIL库来显示图像,并通过遍历所有训练样本将其保存为本地文件。

3万+

被折叠的 条评论
为什么被折叠?



