Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<set_options>
warnings.filterwarnings('ignore')
 %matplotlib inline
seed = 5
np.random.seed(seed )
warnings.filterwarnings("ignore" )<load_from_csv>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 print(train.shape, test.shape)
 train.tail()
train_data = pd.read_csv(path+'train.csv')
 test_data = pd.read_csv(path+'test.csv')
 samp_subm = pd.read_csv(path+'sample_submission.csv' )<count_values>
X_train =(train.iloc[:,1:].values ).astype('float32')
 y_train = train.iloc[:,0].values.astype('int32')
 X_train = X_train.reshape(-1, 28, 28,1)
 X_train = X_train / 255.0
print(X_train.shape , y_train.shape)
 test = test.values.reshape(-1, 28, 28, 1)
 test = test.astype(float)
 test /= 255.0
print(test.shape )
print('Number train samples:', len(train_data.index))
print('Number test samples:', len(test_data.index))
print('Number features:', len(train_data.columns))<count_missing_values>
y_train= to_categorical(y_train)
 num_classes = y_train.shape[1]
print("Number of classes: ",num_classes )
print('Missing values on the train data:', train_data.isnull().sum().sum())
 print('Missing values on the test data:', test_data.isnull().sum().sum() )<train_model>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train, test_size = 0.15, random_state=seed)
 print("Shapes of train, validation dataset ")
 print(X_train.shape , Y_train.shape)
 print(X_val.shape , Y_val.shape )
pca = PCA().fit(train_data[train_data.columns[1:-1]])
 plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('No of components')
 plt.ylabel('Cumulative explained variance')
 plt.grid()
plt.show()<define_variables>
filters_1 = 32
filters_2 = 64
filters_3 = 128
model = models.Sequential()
model.add(conv.Convolution2D(filters_1,(3,3), activation="relu", input_shape=(28, 28, 1), border_mode='same'))
model.add(conv.Convolution2D(filters_1,(3,3), activation="relu", border_mode='same'))
model.add(conv.MaxPooling2D(strides=(2,2)))
 model.add(conv.Convolution2D(filters_2,(3,3), activation="relu", border_mode='same'))
model.add(conv.Convolution2D(filters_2,(3,3), activation="relu", border_mode='same'))
model.add(conv.MaxPooling2D(strides=(2,2)))
 model.add(core.Flatten())
 model.add(core.Dropout(0.2))
model.add(core.Dense(128, activation="relu"))
model.add(core.Dense(10, activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
 model.summary()
features = ['cont'+str(i)for i in range(1, 15)]
no_features = ['id', 'target']<feature_engineering>
%%time
print("apply augumentation or data noisy...")
 datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train)
 print("training started...")
 epochs = 15
batch_size = 128
checkpoint = ModelCheckpoint('model-best-trained.h5', verbose=0, monitor='loss',save_best_only=True, mode='auto')
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=5, verbose=1, factor=0.5, min_lr=0.00001)
 history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 1, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction,checkpoint])
 print("training finished!" )
train_data['mean'] = train_data[features].mean(axis=1)
 train_data['std'] = train_data[features].std(axis=1)
 train_data['max'] = train_data[features].max(axis=1)
 train_data['min'] = train_data[features].min(axis=1)
 train_data['sum'] = train_data[features].sum(axis=1)
 test_data['mean'] = test_data[features].mean(axis=1)
 test_data['std'] = test_data[features].std(axis=1)
 test_data['max'] = test_data[features].max(axis=1)
 test_data['min'] = test_data[features].min(axis=1)
 test_data['sum'] = test_data[features].sum(axis=1 )<prepare_x_and_y>
print("Running prediction test.... ")
 predictions = model.predict_classes(test,verbose=1)
 print("done" )
<predict_on_test><EOS> 
np.savetxt('digits-mnist-cnn-3.csv', np.c_[range(1,len(predictions)+1),predictions], delimiter=',', header = 'ImageId,Label', comments = '', fmt='%d')
 print("saved prediction to file")
 sub = pd.read_csv("digits-mnist-cnn-3.csv")
 sub.tail(10 )