Training data data structure:
[Offset] [Type] [Value] [description] 0000 -bit integer 0x00000803 (2051) magic number 0004- bit integer 60000 number of Images 0008- bit integer number of rows 0012 a bit integer number of columns 0016
unsigned byte ?? Pixel 0017 unsigned byte ?? Pixel ... xxxx unsigned byte ?? Pixel
To read the entire file in:
filename = ' train-images.idx3-ubyte ' binfile = open (filename, ' rb ') BUF = Binfile.read ()
Read the first four 32bit Interger:
index = 0magic, Numimages, numrows, NumColumns = Struct.unpack_from (' >iiii ', buf, index) Index + = struct.calcsize (' & GT;IIII ')
Read a picture, 784=28*28:
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 reads binary mnist