2017/10/13

Python - 印出 mnist 的手寫圖形

安裝 keras

請參考 https://etrex.blogspot.tw/2017/10/windows-10-keras.html

安裝 PIL

在 cmd 輸入以下指令:

pip3 install Image

若想查看已安裝了哪些套件,可在 cmd 輸入以下指令:

pip3 list

印出 MNIST 的手寫數字圖形

MNIST 是一個知名的手寫數字資料集,被當作機器學習界的 hello world 來使用。

import keras
from keras.datasets import mnist
from PIL import Image

(x_train, y_train), (x_test, y_test) = mnist.load_data()
Image.fromarray(x_train[0,]).show()

x_train 是一個大小為 60000,28,28 的三維陣列,代表 60000 張 28x28 的灰階圖檔。
可輸入以下指令查看:

x_train.shape
# (60000,28,28)

x_train[0,] 是一個大小為 28,28 的二維陣列,代表第一張圖片。

Image.fromarray(x_train[0,]).show() 把代表第一張圖片的二維陣列轉為圖片並顯示。

沒有留言: