首頁  >  文章  >  後端開發  >  python如何讀取二進位mnist實例詳解

python如何讀取二進位mnist實例詳解

黄舟
黄舟原創
2017-06-04 10:18:461916瀏覽

這篇文章主要介紹了python讀取二進位mnist實例詳解的相關資料,需要的朋友可以參考下

python讀取二進位mnist實例詳解

training data 資料結構:

<br>[offset] [type]     [value]     [description]
0000   32 bit integer 0x00000803(2051) magic number
0004   32 bit integer 60000      number 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

  將整個檔案讀入:

filename = &#39;train-images.idx3-ubyte&#39;
binfile = open(filename , &#39;rb&#39;)
buf = binfile.read()

#讀取頭四個32bit的interger:

index = 0
magic, numImages , numRows , numColumns = struct.unpack_from(&#39;>IIII&#39; , buf , index)
index += struct.calcsize(&#39;>IIII&#39;)

#讀取一個圖片,784=28*28 :

im = struct.unpack_from(&#39;>784B&#39; ,buf, index)
index += struct.calcsize(&#39;>784B&#39;)
 
im = np.array(im)
im = im.reshape(28,28)
 
fig = plt.figure()
plotwindow = fig.add_subplot(111)
plt.imshow(im , cmap=&#39;gray&#39;)
plt.show()

以上是python如何讀取二進位mnist實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn