Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<drop_column>
%matplotlib inline
train_id = df_train["id"]
test_id = df_test["id"]
df_train.drop("id", axis=1, inplace=True)
 df_test.drop("id", axis=1, inplace=True )<define_variables>
train_data=pd.read_csv(".. /input/train.csv")
 test_data=pd.read_csv('.. /input/test.csv' )
cat_features = [f"cat{i}" for i in range(9 + 1)]<categorify>
y_label=train_data['label']
onehot_encoder = ce.one_hot.OneHotEncoder()
onehot_encoder.fit(pd.concat([df_train[cat_features], df_test[cat_features]], axis=0))
train_ohe = onehot_encoder.transform(df_train[cat_features])
 test_ohe = onehot_encoder.transform(df_test[cat_features])
 train_ohe.columns = [f"OHE_{col}" for col in train_ohe]
test_ohe.columns = [f"OHE_{col}" for col in test_ohe]<define_variables>
img_rows, img_cols = 28, 28
num_classes = 10
numerical_features = [f"cont{i}" for i in range(13 + 1)]<concatenate>
def data_prep(raw):
out_y = keras.utils.to_categorical(raw.label, num_classes)
 num_images = raw.shape[0]
x_as_array = raw.values[:,1:]
x_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)
 out_x = x_shaped_array / 255
return out_x, out_y
train_x = pd.concat([
df_train[numerical_features],
train_ohe
], axis=1 )<concatenate>
train_size =len(train_data )
test_x = pd.concat([
df_test[numerical_features],
test_ohe
], axis=1 )<prepare_x_and_y>
x,y = data_prep(train_data )
train_y = df_train["target"]<choose_model_class>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=20,
zoom_range = 0.18,
width_shift_range=0.15,
height_shift_range=0.15,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(x )
folds = KFold(n_splits=5, shuffle=True, random_state=2021 )<train_model>
X_train, X_val, Y_train, Y_val = train_test_split(x, y, test_size = 0.1, random_state=2 )
class FoldsAverageLGBM:
def __init__(self, folds):
self.folds = folds
self.models = []
def fit(self, lgb_params, train_x, train_y):
oof_preds = np.zeros_like(train_y)
 self.train_x = train_x
self.train_y = train_y.values
for tr_idx, va_idx in tqdm(folds.split(train_x)) :
tr_x, va_x = self.train_x.iloc[tr_idx], self.train_x.iloc[va_idx]
tr_y, va_y = self.train_y[tr_idx], self.train_y[va_idx]
lgb_train_dataset = lgb.Dataset(tr_x, tr_y)
 lgb_valid_dataset = lgb.Dataset(va_x, va_y)
 model = lgb.train(lgb_params, lgb_train_dataset, valid_sets=[lgb_valid_dataset], verbose_eval=100)
 self.models.append(model)
 oof_pred = model.predict(va_x)
 oof_preds[va_idx] = oof_pred
self.oof_preds = oof_preds
def predict(self, test_x):
preds = []
for model in tqdm(self.models):
pred = model.predict(test_x)
 preds.append(pred)
 preds = np.mean(preds, axis=0)
 return preds
def get_feature_importance(self, importance_type="gain"):
feature_names = self.models[0].feature_name()
feature_importances_list = [model.feature_importance(importance_type)for model in self.models]
out_df = pd.DataFrame()
for i, name in enumerate(feature_names):
out_df[name] = [v[i] for v in feature_importances_list]
return out_df<init_hyperparams>
class myCallback(keras.callbacks.Callback):
def on_epoch_end(self,epoch,logs={}):
if(logs.get('acc')>0.997):
print("
Reached 99.7% accuracy so cancelling training")
 self.model.stop_training=True
lgb_params = {
'objective': 'regression',
'metric': 'rmse',
'verbosity': -1,
'learning_rate': 0.01,
'feature_pre_filter': False,
'lambda_l1': 6.271548464074981,
'lambda_l2': 6.442666191955093e-05,
'num_leaves': 244,
'feature_fraction': 0.4,
'bagging_fraction': 0.6165715549446614,
'bagging_freq': 6,
'min_child_samples': 100
}
lgb_params["learning_rate"] = 0.001
lgb_params["early_stopping_round"] = 1000
lgb_params["num_iterations"] = 20000<statistical_test>
model = Sequential()
folds_average_lgbm = FoldsAverageLGBM(folds )<train_model>
model.add(Conv2D(filters = 16, kernel_size =(3,3),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Conv2D(filters = 32, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Flatten())
 model.add(Dense(512, activation = "relu"))
model.add(Dense(10, activation = "softmax"))
folds_average_lgbm.fit(lgb_params, train_x, train_y )<compute_test_metric>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
np.sqrt(mean_squared_error(df_train.target, folds_average_lgbm.oof_preds))<predict_on_test>
adam = keras.optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
 model.compile(
 loss=keras.losses.categorical_crossentropy,
optimizer= adam,
metrics=['accuracy'] )
y_pred = folds_average_lgbm.predict(test_x )<save_to_csv>
callbacks=myCallback()
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=128),
epochs = 80, validation_data =(X_val,Y_val), callbacks=[callbacks] )
sub = df_sample.copy()
sub["target"] = y_pred
sub.to_csv("submission_optuna_lgbm_ohe_v2.csv", index=False)
 sub.head()<import_modules>
num_images=test_data.shape[0]
test_as_array = test_data.values[:,:]
test_shaped_array = test_as_array.reshape(num_images, img_rows, img_cols, 1)
 out_test= test_shaped_array / 255
y_pred=model.predict_classes(out_test )
import lightgbm as lgb
import optuna.integration.lightgbm as oplgb
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from tqdm.notebook import tqdm
import matplotlib.pyplot as plt
import category_encoders as ce
import seaborn as sns<load_from_csv>
submission = pd.DataFrame({"ImageId": np.arange(1, len(test_data)+1), "Label": y_pred})
 submission.to_csv('submission.csv', index=False )