Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<feature_engineering>
%matplotlib inline
data['ts_fa_year'] = data['timestamp_first_active'].dt.year
data['ts_fa_month'] = data['timestamp_first_active'].dt.month
data['ts_fa_day'] = data['timestamp_first_active'].dt.day<drop_column>
train = pd.read_csv(".. /input/train.csv" ).values
test = pd.read_csv(".. /input/test.csv" ).values
X_train = train[:, 1:].astype('float32')
 Y_train = train[:, 0].astype('int32')
 X_test = test[:, :].astype('float32' )
data.drop('timestamp_first_active', inplace = True, axis = 1 )<categorify>
X_train = X_train/255.
 X_test = X_test/255.
 X_train = X_train.reshape(-1, 28, 28, 1)
 X_test = X_test.reshape(-1, 28, 28, 1)
 Y_train = to_categorical(Y_train, num_classes=10)
 X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.15, random_state=23)
 datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range=0.15,
width_shift_range=0.15,
height_shift_range=0.15)
 datagen.fit(X_train )
le = LabelEncoder()
y = le.fit_transform(labels )<prepare_x_and_y>
model = Sequential()
model.add(Conv2D(16, kernel_size=(5, 5),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(Conv2D(32, kernel_size=(3, 3),
padding='same',
activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(3, 3),
padding='same',
activation='relu'))
model.add(Conv2D(64, kernel_size=(3, 3),
padding='same',
activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss=categorical_crossentropy,
optimizer=RMSprop(lr=1e-3),
metrics=['accuracy'] )
X = data[:train.shape[0]]
X_test = data[train.shape[0]:]<train_model>
reduce_lr = ReduceLROnPlateau(monitor='val_loss',
factor=0.2,
patience=2)
 history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=70),
epochs=20,
validation_data=(X_val, Y_val),
verbose=2,
callbacks=[reduce_lr] )
xgb = XGBClassifier(use_label_encoder=False)
 xgb.fit(X, y)
<predict_on_test>
Y_val_pred = model.predict_classes(X_val)
 Y_val_true = np.argmax(Y_val, axis=1)
 print(confusion_matrix(Y_val_true, Y_val_pred))
y_pred = xgb.predict_proba(X_test )<define_variables>
Y_test_pred = model.predict_classes(X_test)
 submission = pd.DataFrame({ 'ImageId': range(1, 28001), 'Label': Y_test_pred })
 submission.to_csv("submission.csv", index=False )