Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<load_from_csv>
os.listdir('.. /input/digit-recognizer')
train = pd.read_csv('.. /input/tabular-playground-series-feb-2021/train.csv')
 test = pd.read_csv('.. /input/tabular-playground-series-feb-2021/test.csv')
 sample_sub = pd.read_csv('.. /input/tabular-playground-series-feb-2021/sample_submission.csv' )<drop_column>
PATH = '.. /input/digit-recognizer'
df_train = pd.read_csv(os.path.join(PATH, 'train.csv'))
train_y = df_train['label'].values
train_x = df_train.drop(['label'], axis=1 ).values
df_test = pd.read_csv(os.path.join(PATH, 'test.csv'))
test_x = df_test.values
print(train_x.shape)
 print(train_y.shape)
 print(test_x.shape )
delete_columns = ['id']
train.drop(delete_columns, axis=1, inplace=True)
 test.drop(delete_columns, axis=1, inplace=True )<define_variables>
IMG_SIZE = 32
categorical_features = ['cat0', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6','cat7', 'cat8', 'cat9']<categorify>
def resize(img_array):
tmp = np.empty(( img_array.shape[0], IMG_SIZE, IMG_SIZE))
for i in range(len(img_array)) :
img = img_array[i].reshape(28, 28 ).astype('uint8')
 img = cv2.resize(img,(IMG_SIZE, IMG_SIZE))
img = img.astype('float32')/255
tmp[i] = img
return tmp
train_x_resize = resize(train_x)
 test_x_resize = resize(test_x )
for c in train.columns:
if train[c].dtype == 'object':
lbl = LabelEncoder()
lbl.fit(list(train[c].values)+list(test[c].values))
train[c] = lbl.transform(train[c].values)
 test[c] = lbl.transform(test[c].values)
 display(train.head())
<prepare_x_and_y>
train_y_final = to_categorical(train_y, num_classes=10)
 print(train_y_final.shape )
y_train = train['target']
X_train = train.drop('target', axis = 1)
 X_test = test<init_hyperparams>
vgg16 = VGG16(weights = 'imagenet',
include_top = False,
input_shape=(IMG_SIZE, IMG_SIZE, 3)
)
 model = Sequential()
model.add(vgg16)
 model.add(Flatten())
 model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
 model.summary()
y_preds = []
models = []
oof_train = np.zeros(len(X_train))
cv = KFold(n_splits=5, shuffle=True, random_state=0)
 params = {
'random_state':42,
'metric': 'rmse',
'n_jobs': -1,
'cat_feature': [x for x in range(len(categorical_features)) ],
'bagging_seed':42,
'feature_fraction_seed':42,
'learning_rate': 0.0011992715138089741,
'max_depth': 101,
'num_leaves': 86,
'reg_alpha': 7.504329214783163,
'reg_lambda': 1.5631184517427836,
'colsample_bytree': 0.22354989226986266,
'min_child_samples': 149,
'subsample_freq': 4,
'subsample': 0.5143496951794435,
'max_bin': 720,
'min_data_per_group': 55,
'cat_smooth': 78,
'cat_l2': 7
}
y_preds = 0
for fold_id,(train_index, valid_index)in enumerate(cv.split(X_train, y_train)) :
X_tr = X_train.loc[train_index, :]
X_val = X_train.loc[valid_index, :]
y_tr = y_train[train_index]
y_val = y_train[valid_index]
lgb_train = lgb.Dataset(X_tr, y_tr, categorical_feature=categorical_features)
 lgb_eval = lgb.Dataset(X_val, y_val, reference=lgb_train, categorical_feature=categorical_features)
 model = lgb.train(params, lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval = 1000,
num_boost_round = 30000,
early_stopping_rounds=1000)
 oof_train[valid_index] = model.predict(X_val, num_iteration=model.best_iteration)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration)
 y_preds += y_pred/5
models.append(model )<save_to_csv>
x_train, x_test, y_train, y_test = train_test_split(train_x_final, train_y_final, test_size=0.2, random_state=2019)
 print(x_train.shape)
 print(x_test.shape)
 print(y_train.shape)
 print(y_test.shape)
pd.DataFrame(oof_train ).to_csv('oof_train_kfold.csv', index=False )<create_dataframe>
es = EarlyStopping(monitor='val_acc', verbose=1, patience=5)
 mc = ModelCheckpoint(filepath='mnist-vgg13.h5', verbose=1, monitor='val_acc')
 cb = [es, mc]
y_preds = pd.DataFrame(y_preds )<prepare_output>
history = model.fit(x_train, y_train,
epochs=100,
batch_size=128,
validation_data=(x_test, y_test),
callbacks=cb )
y_subs = y_preds<save_to_csv>
preds = model.predict(test_x_final, batch_size=128 )
sample_sub['target'] = y_subs
sample_sub.to_csv('submission_CV.csv', index=False )<import_modules>
results = np.argmax(preds, axis=-1)
 results.shape
<load_from_csv><EOS> 
sub = pd.read_csv(os.path.join(PATH, 'sample_submission.csv'))
sub.head()
df = pd.DataFrame({'ImageId': sub['ImageId'], 'Label': results})
 df.to_csv('submission.csv', index=False)
 os.listdir('./' )