Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<train_model>
import tensorflow as tf
import numpy as np
import pandas as pd
import random
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense,Conv2D,Flatten,Dropout,MaxPooling2D
def objective(trial):
train_set = lgb.Dataset(X_train, y_train)
 val_set = lgb.Dataset(X_val, y_val)
 param = {
"objective": "regression",
"metric": "rmse",
"verbosity": 1,
"boosting_type": "gbdt",
"num_leaves": trial.suggest_int("num_leaves", 0, 256),
"max_depth": trial.suggest_int("max_depth", 3, 31),
"lambda_l1": trial.suggest_float("lambda_l1", 0.0, 10),
"lambda_l2": trial.suggest_float("lambda_l2", 0.4, 10),
"feature_fraction": trial.suggest_float("feature_fraction", 0.4, 0.9),
"bagging_fraction": trial.suggest_float("bagging_fraction", 0.4, 0.9),
"bagging_freq": trial.suggest_int("bagging_freq", 5, 15),
"min_child_samples": trial.suggest_int("min_child_samples", 5, 100),
}
model = lgb.train(param,
train_set,
num_boost_round=NUM_BOOST_ROUNDS,
early_stopping_rounds=EARLY_STOPPING_ROUNDS,
verbose_eval=VERBOSE_EVAL,
valid_sets=[train_set, val_set])
 val_preds = model.predict(X_val, num_iteration=model.best_iteration)
 scc = math.sqrt(mean_squared_error(val_preds, y_val))
return -1*scc<find_best_params>
train_df = pd.read_csv('.. /input/train.csv' )
study = optuna.create_study(direction="maximize")
 study.optimize(objective, n_trials=100)
 trial = study.best_trial
trial.params['metric'] = 'rmse'<find_best_params>
label_df = train_df.label
train_df = train_df.drop('label', axis=1 )
print(trial.params )<train_model>
DS_SIZE = len(train_df)
 BATCH_SIZE = 64
x_train, x_val, y_train, y_val = train_test_split(
 np.reshape(train_df.values,(DS_SIZE, 28, 28, 1)) ,
label_df.values,
test_size=0.01,
random_state=11)
 train_gen = ImageDataGenerator(
 rescale=1./255,
shear_range=10,
zoom_range=0.1,
width_shift_range=0.05,
height_shift_range=0.05,
rotation_range=15,
brightness_range=(0.9,1.1),
fill_mode='constant' 
)
 train_gen_flow = train_gen.flow(
 x_train,
y_train,
batch_size=BATCH_SIZE
)
for train_idx, val_idx in cv.split(X, y):
X_train, X_val = X.iloc[train_idx], X.iloc[val_idx]
y_train, y_val = y[train_idx], y[val_idx]
train_set = lgb.Dataset(X_train, y_train)
 val_set = lgb.Dataset(X_val, y_val)
 model = lgb.train(trial.params,
train_set,
num_boost_round=NUM_BOOST_ROUNDS,
early_stopping_rounds=EARLY_STOPPING_ROUNDS,
verbose_eval=-10,
valid_sets=[train_set, val_set] 
)
 val_preds = model.predict(X_val, num_iteration=model.best_iteration)
 test_preds = model.predict(
 test[FEATURES], num_iteration=model.best_iteration)
 oof_df.loc[oof_df.iloc[val_idx].index, 'oof'] = val_preds
sample[f'fold{fold_}'] = test_preds
score = mean_squared_error(
 oof_df.loc[oof_df.iloc[val_idx].index]['target'], oof_df.loc[oof_df.iloc[val_idx].index]['oof'])
 print(math.sqrt(score))
fold_ += 1<save_to_csv>
val_gen = ImageDataGenerator(rescale=1./255)
 val_gen_flow = val_gen.flow(
 x_val,
y_val,
batch_size=BATCH_SIZE
)
print(math.sqrt(mean_squared_error(oof_df.target, oof_df.oof)))
 sample['target'] = sample.drop(['id', 'target'], 1 ).mean(axis=1)
 sample[['id', 'target']].to_csv('submission.csv', index=False )<import_modules>
def get_model() :
model = Sequential()
model.add(Conv2D(64,(3, 3), input_shape=(28,28,1), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(rate=0.4))
model.add(Conv2D(96,(4, 4), activation='relu'))
model.add(Conv2D(128,(6, 6), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(64))
model.add(Dropout(0.3))
model.add(Dense(10, activation="softmax"))
return model
model = get_model()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['acc'] )
import pandas as pd
import numpy as np
import datatable as dt
import datetime
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import KFold
from lightgbm import LGBMRegressor<load_from_csv>
history = model.fit_generator(
 train_gen_flow,
steps_per_epoch=len(x_train)/BATCH_SIZE,
epochs=50,
validation_data = val_gen_flow,
validation_steps = len(x_val)/BATCH_SIZE 
)
train = dt.fread('.. /input/tabular-playground-series-feb-2021/train.csv' ).to_pandas()
test = dt.fread('.. /input/tabular-playground-series-feb-2021/test.csv' ).to_pandas()<define_variables>
train_df = pd.read_csv('.. /input/train.csv')
 raw_train_image_ds = np.reshape(
 train_df.drop('label', axis=1 ).values/255.0, 
(len(train_df), 28, 28, 1)
)
 model.evaluate(raw_train_image_ds,train_df.label.values )
cat_col = [c for c in train.columns if 'cat' in c]
cont_col = [c for c in train.columns if 'cont' in c]<categorify>
test_df = pd.read_csv('.. /input/test.csv')
 test_image_ds = np.reshape(
 test_df.values/255.0, 
(len(test_df), 28, 28, 1)
)
 preds = np.round(model.predict(test_image_ds))
for c in cat_col:
le = LabelEncoder()
train[c] = le.fit_transform(train[c])
 test[c] = le.transform(test[c] )<choose_model_class>
sample_submission_df = pd.read_csv('.. /input/sample_submission.csv')
 sample_submission_df.Label = np.argmax(preds, axis=1)
 sample_submission_df.head()
kfold = KFold(5, True, random_state = 87 )<prepare_x_and_y>
sample_submission_df.to_csv('submission.csv', index=False )