image_output_11.png image_output_24.png image_output_25.png text_plain_output_5.png text_plain_output_15.png image_output_17.png text_plain_output_9.png image_output_14.png image_output_28.png text_plain_output_20.png image_output_23.png text_plain_output_4.png text_plain_output_13.png image_output_13.png image_output_5.png text_plain_output_14.png image_output_18.png image_output_21.png text_plain_output_27.png text_plain_output_10.png text_plain_output_6.png image_output_7.png text_plain_output_24.png text_plain_output_21.png text_plain_output_25.png image_output_20.png text_plain_output_18.png text_plain_output_3.png image_output_4.png text_plain_output_22.png text_plain_output_7.png image_output_8.png text_plain_output_16.png image_output_16.png text_plain_output_8.png text_plain_output_26.png image_output_27.png image_output_6.png text_plain_output_23.png image_output_12.png text_plain_output_28.png image_output_22.png text_plain_output_2.png text_plain_output_1.png image_output_3.png text_plain_output_19.png image_output_2.png image_output_1.png image_output_10.png text_plain_output_17.png text_plain_output_11.png text_plain_output_12.png image_output_15.png image_output_9.png image_output_19.png image_output_26.png
import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import sklearn.preprocessing as pre

train_data = pd.read_csv('../input/train.csv')
test_data = pd.read_csv('../input/test.csv')
images = train_data.drop('label', 1)
observations, features = images.shape
pixel_width = int(np.sqrt(features))
X = images.as_matrix()
X_train = X.reshape(observations, pixel_width, pixel_width, 1)
labels = train_data['label']
Y = labels.as_matrix()
labels = pre.LabelEncoder().fit_transform(labels)[:, None]
Y_train = pre.OneHotEncoder().fit_transform(labels).todense()
t = test_data.as_matrix()
tr, tc = t.shape
test = t.reshape(tr, pixel_width, pixel_width, 1)

def showImage(X, index):
    N, w, h, c = X.shape
    grid = np.zeros((w, h))
    for i in range(w):
        for j in range(h):
            grid[i, j] = X[index, i, j, 0]
    plt.rcParams['figure.figsize'] = [1.5, 1.5]
    plt.imshow(grid, cmap='gray')
    plt.ion()
    plt.show()
showImage(X_train, 3)
showImage(X_train, 875)
showImage(X_train, 40000)