1. MNIST - Introduce

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from keras.utils import np_utils
from keras.datasets import mnist

def plot_image(image):
    fig = plt.gcf()
    fig.set_size_inches(2, 2)
    plt.imshow(image, cmap = 'binary')
    plt.show()

def plot_images_labels_prediction(images, labels, prediction, idx, num = 10):
    fig = plt.gcf()
    fig.set_size_inches(12, 14)
    if num > 25:
        num = 25
    for i in range(0, num):
        ax = plt.subplot(5, 5, 1 + i)
        ax.imshow(images[idx], cmap = 'binary')
        title = "label: " + str(labels[idx])
        if len(prediction) > 0:
            title += ", predict: " + str(prediction[idx])

        ax.set_title(title, fontsize = 10)
        ax.set_xticks([])
        ax.set_yticks([])
        idx += 1
    plt.show()

np.random.seed(10)

(x_train_image, y_train_label), (x_test_image, y_test_label) = mnist.load_data()

#print('train data: ', len(x_train_image))
#print(' test data: ', len(x_test_image))

#print('x_train_image: ', x_train_image.shape)
#print('y_train_label: ', y_train_label.shape)

#plot_image(x_train_image[0])

#plot_images_labels_prediction(x_train_image, y_train_label, [], 0, 10)

#print('x_test_image: ', x_test_image.shape)
#print('y_test_label: ', y_test_label.shape)

#plot_images_labels_prediction(x_test_image, y_test_label, [], 0, 10)

x_Train = x_train_image.reshape(60000, 784).astype('float32')
x_Test = x_test_image.reshape(10000, 784).astype('float32')

#print('x_Train: ', x_Train.shape)
#print('x_Test: ', x_Test.shape)

#print(x_train_image[0])

x_Train_normalize = x_Train / 255
x_Test_normalize = x_Test / 255

#print(x_Train_normalize[0])
#print(y_train_label[:5])

y_TrainOneHot = np_utils.to_categorical(y_train_label)
y_TestOneHot = np_utils.to_categorical(y_test_label)

print(y_TrainOneHot[:5])

Last updated

Was this helpful?