MNIST 数据处理

来源:互联网 发布:网络转换器辐射大吗 编辑:程序博客网 时间:2024/05/23 02:03



# -*- coding: utf-8 -*-"""Created on Tue Mar 01 15:29:30 2016@author: wzhao"""import osos.getcwd()os.chdir("D:\\Workspace")import numpy as npimport structimport matplotlib.pyplot as plt """读入训练集, 60000"""filename = 'D:\\Workspace\\train-images.idx3-ubyte'binfile = open(filename , 'rb')buf = binfile.read() index = 0magic, numImages , numRows , numColumns = struct.unpack_from('>IIII' , buf , index)index += struct.calcsize('>IIII')images= [[0 for col in range(numImages)] for row in range(numRows*numColumns)]type(images) # listimages=np.array(images)images=images.reshape(numImages, numRows*numColumns)images.shape # (60000, 784)i=0while i<numImages:    images[i,] = struct.unpack_from('>784B' ,buf, index)    print i,index    index += struct.calcsize('>784B')    i+=1binfile.close()np.save("Train_Images.npy", images)"""读入训练集Label, 60000"""filename = 'D:\\Workspace\\train-labels.idx1-ubyte'binfile = open(filename , 'rb')buf = binfile.read()index = 0magic, numImages = struct.unpack_from('>II' , buf , index)index += struct.calcsize('>II')labels= [[0] for col in range(numImages) ]labels=np.array(labels)labels=labels.reshape(numImages, 1)labels.shape # (60000, 1)i=0while i<numImages:    labels[i,] = struct.unpack_from('>1B' ,buf, index)    print i,index    index += struct.calcsize('>1B')    i+=1    binfile.close()"""label_out=[[0 for col in range(60000)] for row in range(10)]label_out=np.array(label_out)label_out=label_out.reshape(60000, 10)label_out.shape # (60000, 10)i=0while i<numImages:    label_out[i, labels[i]]=1    i+=1"""# labels[:5]# label_out[:5,]np.save("Train_Labels.npy", labels)"""# 读入文件结束""""""## 导出数据到文件np.save("MNIST.npy", images)# c = np.load( "MNIST.npy" )images.tofile("MNIST.bin")images.dtype # dtype('int32')b = np.fromfile("MNIST.bin", dtype=np.int32)type(b)b.shapeb=b.reshape(numImages, 784)""""""# 打印图形,查看是否正确导入文件"""fig = plt.figure()plotwindow = fig.add_subplot(111)plt.imshow(im , cmap='gray')plt.show()


0 0
原创粉丝点击