<jupyter_start><jupyter_text>Convolutional Neural NetworkCNN for MNIST  reference: https://www.codexa.net/cnn-mnist-keras-beginner/<jupyter_code>import os
os.environ['KERAS_BACKEND'] = 'theano'
import keras
from sklearn.datasets import fetch_mldata
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline
# datasets
mnist = fetch_mldata('MNIST original', data_home=".")
x = mnist["data"]
y = mnist["target"]
print(x.shape)
print(y.shape)
# visualize data
m_x = x[0].reshape([28, 28])
plt.imshow(m_x, cmap=plt.cm.binary, interpolation='nearest')
x_train = x[:60000]
x_test = x[60000:]
y_train = y[:60000]
y_test = y[60000:]

assert x_train.shape[0] == 60000
assert x_test.shape[0] == 10000
# reshape data
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32') / 255.0
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype('float32') / 255.0
y_train = keras.utils.np_utils.to_categorical(y_train, 10)
y_test = keras.utils.np_utils.to_categorical(y_test, 10)
# define model
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(32, kernel_size=(3, 3),
                             activation='relu',
                             input_shape=(28, 28, 1)))
model.add(keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(128, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy',
             optimizer='sgd',
             metrics=['accuracy'])
# train
model.fit(x_train, y_train, epochs=20)
# evaluate
loss, acc = model.evaluate(x_test, y_test, batch_size=128)
print(loss, acc)
# prediction
prediction = model.predict_classes(x_test)
pd.DataFrame({'Pred': list(prediction), 'Actual': np.argmax(list(y_test))}).head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15608465608465608, 'fraction_numerical': 0.047619047619047616, 'mean_word_length': 5.2, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '68144', 'license': 'MIT', 'n_tokens_mistral': 789, 'n_tokens_neox': 717, 'n_words': 127, 'path': 'neural_network/convolution/mnist.ipynb', 'repo_name': 'fumiphys/data-science', 'size': '1890'}