Python 手書き文字mnistデータを触ってみる
◆mnistデータを用意
・トレーニングデータが60000(画像&ラベル)、テストデータが10000(画像&ラベル)
・ゼロから分かるディープラーニングのgithubからダウンロード
◆ 内容
t_train, t_train, x_test, t_test
◆さわりはじめ
(1)とりあえず”dataset.mnist”モジュールから”load_mnist”をインポート
import sys, os
sys.path.append(os.pardir)
from dataset.mnist import load_mnist
(2)定義する
(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False)
(3)テストデータの形状を確認
print(x_train.shape)
print(x_test.shape)
=>
(60000, 784)
(10000, 784)
◆”load_mnist”
・load_mnist(トレーニング画像, ラベル),(テスト画像, ラベル)の形式でmnistデータを返す。
・load_mnist(normalize=False, flatten=True, one_hot_label=Fals)のように引数設定。
◆画像の表示・・・PIL:python image libraryモジュールを使う
from PIL import Image
☞ ”トレーニング”の15番目を見てみる
print(x_train[15]) # mnistデータの”x_train”の15番目を表示
※28x28になっていない、8ビット
# ”トレーニングデータラベル”の1番目を見てみる
print(t_train[15])
=>
7 #「7」とのこと
☞ 28x28にする
image = x_train[15].reshape(28,28) #"reshape"で形を整える
print(image)
※28x28になっているけどjupyternotebookで改行
☞ ゼロから学ぶDLの関数を使う
# 関数を定義
def img_show(img):
pil_img = Image.fromarray(np.uint8(img))
pil_img.show()
☞ とりあえず作業痕跡。。
(1)その1
(2)その2
◆触れたこと
☞ scikit-learn (サイキット・ラーン) ・・・機械学習ライブラリ
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
from sklearn.datasets import fetch_mldata
☞ fetch_mldata()・・・scikit-learnの関数、webからmnistデータをもってくる。
例)
mnist = fetch_mldata('MNIST original')
☞ shuffle / random_state=**
☞ split
☞ could not read bytes
・中途半端なデータが残っているせいか。scikit_learn_data/mldataのフォルダごと削除して再度やり直し。
◆参考
☞ Pythonマニュアル
https://docs.python.jp/3/tutorial/index.html
☞ scikit-learn
http://scikit-learn.org/stable/tutorial/machine_learning_map/
☞ 参考
http://tawara.hatenablog.com/entry/2016/11/06/DeepLearningFromZero-04
https://openbook4.me/sections/1452