Kaggle competition: Digit Recognizer
lgbm_params = {
"random_state": 2021,
"metric": "rmse",
"n_jobs": -1,
"cat_feature": [x for x in range(len(categorical_columns)) ],
"early_stopping_round": 150,
"reg_alpha": 6.147694913504962,
"reg_lambda": 0.002457826062076097,
"colsample_bytree": 0.3,
"learning_rate": 0.01,
"max_depth": 30,
"num_leaves": 100,
"min_child_samples": 275,
"n_estimators": 30000,
"cat_smooth": 40.0,
"max_bin": 512,
"min_data_per_group": 100,
"bagging_freq": 1,
"bagging_fraction": 0.7,
"cat_l2": 12.0,
}<train_model>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
train_oof = np.zeros(( 300000,))
test_preds = 0
train_oof.shape
NUM_FOLDS = 5
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=42)
 for f,(train_ind, val_ind)in tqdm(enumerate(kf.split(x, y))):
tmp_train_df, tmp_val_df = x.iloc[train_ind][feature_cols], x.iloc[val_ind][feature_cols]
train_target, val_target = y[train_ind], y[val_ind]
model = LGBMRegressor(**lgbm_params)
 model.fit(
 tmp_train_df,
train_target,
eval_set=[(tmp_val_df, val_target)],
early_stopping_rounds = 100,
verbose=False 
)
 temp_oof = model.predict(tmp_val_df)
 temp_test = model.predict(test_df[feature_cols])
 train_oof[val_ind] = temp_oof
test_preds += temp_test/NUM_FOLDS
print(mean_squared_error(temp_oof, val_target, squared=False))<save_to_csv>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
sub_df['target'] = test_preds
sub_df.to_csv('submission_lgbm_cv_optimized.csv', index=False)
 sub_df.head()
sub_lgbm_cv_optimized = test_preds<train_model>
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1 )
train_oof = np.zeros(( 300000,))
test_preds = 0
train_oof.shape
NUM_FOLDS = 5
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=42)
 for f,(train_ind, val_ind)in tqdm(enumerate(kf.split(xmod, ymod))):
tmp_train_df, tmp_val_df = xmod.iloc[train_ind][feature_cols_mod], xmod.iloc[val_ind][feature_cols_mod]
train_target, val_target = ymod[train_ind], ymod[val_ind]
model = LGBMRegressor(**lgbm_params)
 model.fit(
 tmp_train_df,
train_target,
eval_set=[(tmp_val_df, val_target)],
early_stopping_rounds = 100,
verbose=False 
)
 temp_oof = model.predict(tmp_val_df)
 temp_test = model.predict(mod_test_df[feature_cols_mod])
 train_oof[val_ind] = temp_oof
test_preds += temp_test/NUM_FOLDS
print(mean_squared_error(temp_oof, val_target, squared=False))<save_to_csv>
X_train = X_train / 255.0
test = test / 255
sub_df['target'] = test_preds
sub_df.to_csv('submission_lgbm_mod_cv_optimized.csv', index=False)
 sub_df.head()
sub_lgbm_mod_cv_optimized = test_preds<set_options>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2 )
h2o.init()<split>
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28, 1)) ,
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
 ] )
train_hf = h2o.H2OFrame(train_df)
 test_hf = h2o.H2OFrame(test_df)
 predictors = list(feature_cols)
 response = 'target'
train, valid = train_hf.split_frame(ratios=[.8], seed=1234 )<choose_model_class>
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] )
aml = H2OAutoML(
 max_models=20,
max_runtime_secs=200,
exclude_algos = ["DeepLearning", "DRF"],
seed=42,
)<train_model>
history = model.fit(X_train, Y_train, epochs=5 )
aml.train(x=predictors,
y=response,
training_frame=train,
validation_frame=valid
)<compute_test_metric>
hist = pd.DataFrame(history.history)
 hist['epoch'] = history.epoch
hist.tail()
print('The model performance in RMSE: {}'.format(aml.leader.rmse(valid=True)))
 print('The model performance in MAE: {}'.format(aml.leader.mae(valid=True)) )<predict_on_test>
test_loss, test_acc = model.evaluate(X_val, Y_val)
 print('Test accuracy:', test_acc )
preds = aml.predict(test_hf ).as_data_frame()
preds.head()<save_to_csv>
model = keras.Sequential([
tf.keras.layers.Conv2D(32,(3,3), padding='same', activation=tf.nn.relu,
input_shape=(28, 28, 1)) ,
tf.keras.layers.Conv2D(32,(3,3), padding='same', activation=tf.nn.relu),
tf.keras.layers.MaxPooling2D(( 2, 2), strides=2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(64,(3,3), padding='same', activation=tf.nn.relu),
tf.keras.layers.Conv2D(64,(3,3), padding='same', activation=tf.nn.relu),
tf.keras.layers.MaxPooling2D(( 2, 2), strides=2),
tf.keras.layers.Dropout(0.25),
keras.layers.Flatten() ,
keras.layers.Dense(256, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.50),
keras.layers.Dense(10, activation=tf.nn.softmax)
 ] )
sub_df['target'] = preds['predict']
sub_df.to_csv('submission_h2o.csv', index=False)
 sub_df.head()
sub_automl = preds['predict']<define_variables>
model.compile(optimizer = 'adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] )
sub1 = 0.3*sub_xgb_cv_optimized + 0.3*sub_lgbm_cv_optimized + 0.4*sub_lgbm_optuna
sub2 = 0.4*sub_xgb_cv_optimized + 0.4*sub_lgbm_cv_optimized + 0.2*sub_lgbm_optuna
sub3 = 0.3*sub_xgb_cv_optimized + 0.4*sub_lgbm_cv_optimized + 0.3*sub_lgbm_optuna
sub4 = 0.3*sub_xgb_cv_optimized + 0.3*sub_lgbm_cv_optimized + 0.3*sub_lgbm_optuna + 0.1*sub_automl
sub5 = 0.3*sub_xgb_cv_optimized + 0.3*sub_lgbm_cv_optimized + 0.2*sub_lgbm_optuna + 0.2*sub_automl
sub6 = 0.5*sub_lgbm_mod_cv_optimized + 0.5*sub_xgb_mod_cv_optimized
sub7 = 0.7*sub_lgbm_mod_cv_optimized + 0.3*sub_xgb_mod_cv_optimized
sub8 = 0.2*sub_xgb_cv_optimized + 0.2*sub_lgbm_cv_optimized + 0.3*sub_lgbm_mod_cv_optimized + 0.3*sub_xgb_mod_cv_optimized
sub9 = 0.1*sub_xgb_cv_optimized + 0.1*sub_lgbm_cv_optimized + 0.4*sub_lgbm_mod_cv_optimized + 0.4*sub_xgb_mod_cv_optimized
sub10 = 0.1*sub_xgb_cv_optimized + 0.2*sub_lgbm_cv_optimized + 0.4*sub_lgbm_mod_cv_optimized + 0.3*sub_xgb_mod_cv_optimized
sub11 = 0.5*sub_xgb_cv_optimized + 0.5*sub_lgbm_cv_optimized
sub12 = 0.6*sub_xgb_cv_optimized + 0.4*sub_lgbm_cv_optimized
sub13 = 0.4*sub_xgb_cv_optimized + 0.6*sub_lgbm_cv_optimized
sub14 = 0.2*sub_xgb_cv_optimized + 0.7*sub_lgbm_cv_optimized + 0.1*sub_lgbm_mod_cv_optimized
sub14 = 0.2*sub_xgb_cv_optimized + 0.7*sub_lgbm_cv_optimized + 0.1*sub_xgb_mod_cv_optimized<save_to_csv>
history = model.fit(X_train, Y_train, epochs=30 )
sub_df['target'] = sub1
sub_df.to_csv('submission_01.csv', index=False)
 sub_df['target'] = sub2
sub_df.to_csv('submission_02.csv', index=False)
 sub_df['target'] = sub3
sub_df.to_csv('submission_03.csv', index=False)
 sub_df['target'] = sub4
sub_df.to_csv('submission_04.csv', index=False)
 sub_df['target'] = sub5
sub_df.to_csv('submission_05.csv', index=False)
 sub_df['target'] = sub6
sub_df.to_csv('submission_06.csv', index=False)
 sub_df['target'] = sub7
sub_df.to_csv('submission_07.csv', index=False)
 sub_df['target'] = sub8
sub_df.to_csv('submission_08.csv', index=False)
 sub_df['target'] = sub9
sub_df.to_csv('submission_09.csv', index=False)
 sub_df['target'] = sub10
sub_df.to_csv('submission_10.csv', index=False)
 sub_df['target'] = sub11
sub_df.to_csv('submission_11.csv', index=False)
 sub_df['target'] = sub12
sub_df.to_csv('submission_12.csv', index=False)
 sub_df['target'] = sub13
sub_df.to_csv('submission_13.csv', index=False)
 sub_df['target'] = sub14
sub_df.to_csv('submission_14.csv', index=False )<load_from_csv>
test_loss, test_acc = model.evaluate(X_val, Y_val)
 print('Test accuracy:', test_acc )
PATH = '.. /input/tabular-playground-series-feb-2021/'
train = pd.read_csv(PATH + 'train.csv')
 test = pd.read_csv(PATH + 'test.csv')
 sample = pd.read_csv(PATH + 'sample_submission.csv')
 print(train.shape, test.shape )<drop_column>
predictions = model.predict(test )
FEATURES = train.drop(['id', 'target'], 1 ).columns
FEATURES<categorify>
np.argmax(predictions[0] )
for i in cat_features:
le = LabelEncoder()
le.fit(train[i])
 train[i] = le.transform(train[i])
 test[i] = le.transform(test[i])
 train.head()<choose_model_class>
predictions = np.argmax(predictions,axis = 1)
 predictions = pd.Series(predictions,name="Label" )
<split><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),predictions],axis = 1)
 submission.to_csv("mnist_submission_v6.csv",index=False )