text_plain_output_1.png
from keras.layers import Conv2D, MaxPooling2D, Dropout, Activation, Flatten, Dense

from keras.models import Sequential

from keras.optimizers import Adam

from sklearn.decomposition import PCA

from sklearn.manifold import TSNE

from sklearn.preprocessing import LabelBinarizer

from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

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

num_training = len(train.values)
num_testing = len(test.values)

y_train = np.array(train.pop('label').values)
x_train = np.array(train.values)
x_test = np.array(test.values)

x_train = x_train.reshape(num_training, 28, 28)
x_test = x_test.reshape(num_testing, 28, 28)

for i in range(66):
    plt.xticks([])
    plt.yticks([])
plt.tight_layout()

rawx = train.values[:1000]
from sklearn.preprocessing import StandardScaler
xscaled = StandardScaler().fit_transform(rawx)
from sklearn.manifold import TSNE
tsne = TSNE()
vis = tsne.fit_transform(xscaled)
vis = [{'X': vis[i][0], 'Y': vis[i][1], 'K': y_train[i]} for i in range(len(vis))]

sns.FacetGrid(pd.DataFrame.from_dict(vis), hue='K', size=8).map(plt.scatter, 'X', 'Y').add_legend()

from sklearn.decomposition import PCA
pca = PCA(n_components=5)
pca_ani = pca.fit_transform(xscaled)
xpca = pca_ani[:, 0]
ypca = pca_ani[:, 1]
vispca = [{'X': xpca[i], 'Y': ypca[i], 'K': y_train[i]} for i in range(len(xpca))]

sns.FacetGrid(pd.DataFrame.from_dict(vispca), hue='K', size=8).map(plt.scatter, 'X', 'Y').add_legend()

x_train = x_train / 255.0
x_test = x_test / 255.0

from sklearn.preprocessing import LabelBinarizer
y_train_hot = LabelBinarizer().fit_transform(y_train)

model = Sequential()
model.add(Conv2D(6, 5, 5, input_shape=(28, 28, 1), bias=True, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode='same'))
model.add(Dropout(p=0.12))
model.add(Conv2D(16, 5, 5, bias=True, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode='same'))
model.add(Conv2D(35, 5, 5, bias=True, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode='same'))
model.add(Flatten())
model.add(Dense(120, bias=True))
model.add(Activation('relu'))
model.add(Dropout(p=0.5))
model.add(Dense(84, bias=True))
model.add(Activation('relu'))
model.add(Dense(10, bias=True))
model.add(Activation('softmax'))
model.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'], decay=1)

x_train = np.expand_dims(x_train, axis=4)
x_test = np.expand_dims(x_test, axis=4)

training_hist = model.fit(x_train, y_train_hot, nb_epoch=16, batch_size=64, verbose=2, validation_split=0.23)

hdf = pd.DataFrame.from_dict(training_hist.history)
hdf['epochs'] = list(range(16))

pred = model.predict_classes(x_test, verbose=2)
sub_df = pd.DataFrame()
sub_df['ImageId'] = list(range(1, num_testing + 1))
sub_df['Label'] = pred

print('Amount of test points:', num_testing)
print('Amount of predictions:', len(pred))
sub_df.head()