Chapter 3はClassification Systemについて学ぶ。
<MNIST>
参考LINK
*MNISTデータ取扱の説明
http://tensorflow.classcat.com/2016/03/09/tensorflow-cc-mnist-data-download/
MNIST handwritten digit database, Yann LeCun et al.
githubにコードを掲載しています。
今回のコードまとめ↓
https://github.com/ekaminuma/MachineLearning_RC17/blob/master/170904_p83.ipynb
---------------------------------------------------------------------------------------------------------------
以下、githubのipynbと同じです
### pip install keras (事前にインストールしておく)
### pip install tensorflow (こちらも要るかも?)
from keras.datasets import mnist
(X_train,y_train),(X_test,y_test)=mnist.load_data()
X_train=X_train.reshape(60000,784)/255
X_test=X_test.reshape(10000,784)/255
X_train.shape
(60000, 28, 28)
y_train.shape
(60000,)
X_test.shape
(10000, 28, 28)
y_test.shape
(10000,)
28*28
784
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('ggplot')
#some_digit = X[36000] in book
idx=36000
size=28
a,b=np.meshgrid(range(size),range(size))
some_digit_image=X_train[idx].reshape(size,size)
some_digit_image=some_digit_image[::-1,:]
print('number:{}'.format(y_train[idx]))
plt.imshow(some_digit_image, cmap = matplotlib.cm.binary,
interpolation="nearest")
#plt.axis("off")
save_fig("some_digit_plot")
plt.show()
plt.figure(figsize=(2.5,2.5))
plt.xlim(0,27)
plt.ylim(0,27)
plt.pcolor(a,b,some_digit_image)
y_train[36000]
def plot_digits(instances, images_per_row=10, **options):
size = 28
images_per_row = min(len(instances), images_per_row)
images = [instance.reshape(size,size) for instance in instances]
n_rows = (len(instances) - 1) // images_per_row + 1
row_images = []
n_empty = n_rows * images_per_row - len(instances)
images.append(np.zeros((size, size * n_empty)))
for row in range(n_rows):
rimages = images[row * images_per_row : (row + 1) * images_per_row]
row_images.append(np.concatenate(rimages, axis=1))
image = np.concatenate(row_images, axis=0)
plt.imshow(image, cmap = matplotlib.cm.binary, **options)
plt.axis("off")
plt.figure(figsize=(9,9))
example_images = np.r_[X_train[:12000:600], X_train[13000:30600:600], X_train[30600:60000:590]]
plot_digits(example_images, images_per_row=10)
save_fig("more_digits_plot")
plt.show()
import numpy as np
shuffle_index = np.random.permutation(60000)
X_train, y_train = X_train[shuffle_index], y_train[shuffle_index]