text_plain_output_1.png
from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')

x_train = train.iloc[:, 1:].values / 255.0
y_train = train.iloc[:, 0].values
x_test = test.values / 255

x_train = np.reshape(x_train, (-1, 1, 28, 28))
x_test = np.reshape(x_test, (-1, 1, 28, 28))
(x_train.shape, x_test.shape)

random_seed = 234
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.1, random_state=random_seed)
(x_train.shape, x_val.shape, y_train.shape, y_val.shape)

def display(rows, columns, images, values=[], predictions=[], save_image=False, filename='image.png', show=False):
    fig = plt.figure(figsize=(15, 15))
    ax = []
    for i in range(columns * rows):
        img = images[i]
        ax.append(fig.add_subplot(rows, columns, i + 1))
        title = ''
        if len(values) == 0 and len(predictions) != 0:
            title = 'Pred:' + str(predictions[i])
        elif len(values) != 0 and len(predictions) == 0:
            title = 'Value:' + str(values[i])
        elif len(values) != 0 and len(predictions) != 0:
            title = '\nValue:' + str(values[i]) + '\nPred:' + str(predictions[i])
        ax[-1].set_title(title)
        plt.imshow(img)
        plt.axis('off')
    if save_image:
        plt.savefig(filename)
    if not show:
        plt.close(fig)
idx = np.random.randint(1, 1000, size=9)
images = x_train[idx, :]
images = images[:, 0]
values = y_train[idx]
print(images.shape)
display(rows=3, columns=3, images=images, values=values, predictions=[], save_image=True, filename='Autoencoder-validation.png', show=True)