Kaggle competition: Digit Recognizer
num_df_train = df_train.drop(categorical_features, axis=1)
 num_df_test = df_test.drop(categorical_features, axis=1 )<categorify>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )
df_train_encoded = pd.concat([num_df_train, encoder_cols_train], axis=1)
 df_test_encoded = pd.concat([num_df_test, encoder_cols_test], axis=1)
 print("Train dataset shape:", df_train_encoded.shape)
 print("Test dataset shape:", df_test_encoded.shape )<sort_values>
( x_train1, y_train1),(x_test1, y_test1)= mnist.load_data()
train1 = np.concatenate([x_train1, x_test1], axis=0)
 y_train1 = np.concatenate([y_train1, y_test1], axis=0)
 Y_train1 = y_train1
X_train1 = train1.reshape(-1, 28*28 )
df_train_encoded.median().sort_values(ascending=False )<train_model>
X_train = X_train / 255.0
test = test / 255.0
X_train1 = X_train1 / 255.0
X = df_train_encoded.drop(['price_doc_log'], axis=1)
 y = df_train_encoded['price_doc_log']
print("X shape:", X.shape)
 print("y shape:", y.shape )<split>
X_train = np.concatenate(( X_train.values, X_train1))
Y_train = np.concatenate(( Y_train, Y_train1))
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=2022)
 X_test = df_test_encoded<predict_on_test>
Y_train = to_categorical(Y_train, num_classes = 10 )
tree = DecisionTreeRegressor(random_state=2022, max_depth=5, min_samples_split=20)
 tree.fit(X_train, y_train)
 tree_predictions_log = tree.predict(X_val)
 tree_predictions = np.exp(tree_predictions_log )<compute_test_metric>
random_seed = 2
print('RMSLE:', np.sqrt(mean_squared_log_error(np.exp(y_val), tree_predictions)) )<predict_on_test>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed )
predict = np.exp(tree.predict(X_test))
submission = pd.DataFrame({'id': id_test, 'price_doc': predict})
 submission.head()<save_to_csv>

 model = Sequential()
model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation = "softmax"))
model.summary()
submission.to_csv('DecisionTree.csv', index=False )<prepare_x_and_y>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )
dmatrix_train = xgb.DMatrix(X_train, y_train)
 dmatrix_val = xgb.DMatrix(X_val, y_val)
 dmatrix_test = xgb.DMatrix(X_test )<train_model>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
xgb_params = {
'eta': 0.05,
'max_depth': 5,
'subsample': 1.0,
'colsample_bytree': 0.7,
'objective': 'reg:squarederror',
'eval_metric': 'rmse',
'verbosity': 0
}
partial_model = xgb.train(xgb_params, dmatrix_train, num_boost_round=1000, evals=[(dmatrix_val, 'val')],
early_stopping_rounds=20, verbose_eval=20)
 num_boost_round = partial_model.best_iteration<train_model>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
model = xgb.train(dict(xgb_params, verbose=1), dmatrix_train, num_boost_round=num_boost_round )<predict_on_test>
epochs = 50
batch_size = 32
predict = np.exp(model.predict(dmatrix_val))
print('RMSLE:', np.sqrt(mean_squared_log_error(np.exp(y_val), predict)) )<predict_on_test>
ylog_pred = model.predict(dmatrix_test)
 y_pred = np.exp(ylog_pred)
 submission = pd.DataFrame({'id': id_test, 'price_doc': y_pred})
 submission.head()<save_to_csv>
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 )
submission.to_csv("XGB_new_clear_submission.csv", index=False )<prepare_x_and_y>

 history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )
dmatrix_train = xgb.DMatrix(X_train, y_train)
 dmatrix_test = xgb.DMatrix(X_test )<init_hyperparams>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
xgb_params = {
'eta': 0.05,
'max_depth': 5,
'subsample': 0.7,
'colsample_bytree': 0.7,
'objective': 'reg:squarederror',
'eval_metric': 'rmse',
'verbosity': 0
}<compute_train_metric>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_submission.csv",index=False )