前のページで保存した学習用,テスト用のデータを開いてみよう.
データファイルを開く (04-readdata.py)
import numpy as np
# ファイルを開いて読み込む
x_train = np.load('train_X_data.npy')
y_train = np.load('train_Y_data.npy')
x_test = np.load('test_X_data.npy')
y_test = np.load('test_Y_data.npy')
print(x_train)
print(y_train)
print(x_test)
print(y_test)
Using TensorFlow backend. [[0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] ... [1 1 0 ... 0 1 1] [1 1 1 ... 1 1 0] [0 0 0 ... 0 0 0]] ['3' '0' '4' '3' '7' '7' '7' '1' '4' '5' '8' '0' '2' '5' '0' '4' '5' '2' '4' '9' '4' '8' '3' '2' '2' '6' '3' '1' '8' '9' '9' '1' '3' '0' '7' '7' '0' '1' '6' '0' '5' '2' '7' '9' '1' '5' '1' '4' '6' '9' '6' '9' '1' '0' '6' '2' '9' '3' '3' '2' '1' '1' '4' '5' '8' '7' '5' '3' '9' '2' '4' '5' '5' '4' '2' '8' '9' '4' '3' '8'] [[0 0 0 ... 0 0 0] [1 1 1 ... 0 1 1] [0 0 0 ... 0 0 0] ... [0 0 0 ... 0 0 0] [0 0 1 ... 0 0 0] [0 0 1 ... 0 0 0]] ['6' '9' '3' '8' '7' '6' '8' '2' '8' '0' '6' '6' '5' '0' '8' '0' '1' '6' '7' '7']
one-hot-encoding とは正解の列だけ1で,その他の列が0になるような行列表現です.後の利用のため,y_train
と y_test
を one-hot-encoding 化してみよう.Keras の to_categorical を使えば,簡単に one-hot-encoding 化が可能です.
データファイルを開いて one-hot-encoding化 (04-readdata.py)
import numpy as np
from keras.utils import to_categorical
# ファイルを開いて読み込む
x_train = np.load('train_X_data.npy')
y_train = np.load('train_Y_data.npy')
x_test = np.load('test_X_data.npy')
y_test = np.load('test_Y_data.npy')
print(y_train)
# 正解ラベルを one-hot-encoding にする
y_train = to_categorical(y_train, 10)
print(y_train)
print(y_test)
# 正解ラベルを one-hot-encoding にする
y_test = to_categorical(y_test, 10)
print(y_test)
Using TensorFlow backend. ['3' '0' '4' '3' '7' '7' '7' '1' '4' '5' '8' '0' '2' '5' '0' '4' '5' '2' '4' '9' '4' '8' '3' '2' '2' '6' '3' '1' '8' '9' '9' '1' '3' '0' '7' '7' '0' '1' '6' '0' '5' '2' '7' '9' '1' '5' '1' '4' '6' '9' '6' '9' '1' '0' '6' '2' '9' '3' '3' '2' '1' '1' '4' '5' '8' '7' '5' '3' '9' '2' '4' '5' '5' '4' '2' '8' '9' '4' '3' '8'] [[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.] ...(中略)... [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.] [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]] ['6' '9' '3' '8' '7' '6' '8' '2' '8' '0' '6' '6' '5' '0' '8' '0' '1' '6' '7' '7'] [[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.] [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.] [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.] [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.] [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.] [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]]
上の実行結果を確認すると,正解ラベルに対応する列だけが1になっていることがわかることでしょう.