Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<prepare_x_and_y>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
train_x = df_train[feature_cols]
train_y = df_train.target
test_x = df_test<choose_model_class>
np.random.seed(1)
 df_train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 df_train = df_train.iloc[np.random.permutation(len(df_train)) ]
folds = KFold(n_splits=5, shuffle=True, random_state=2021 )<train_model>
sample_size = df_train.shape[0]
validation_size = int(df_train.shape[0] * 0.1)
 train_x = np.asarray(df_train.iloc[:sample_size - validation_size:, 1:] ).reshape([sample_size - validation_size, 28, 28, 1])
 train_y = np.asarray(df_train.iloc[:sample_size - validation_size:, 0] ).reshape([sample_size - validation_size, 1])
 val_x = np.asarray(df_train.iloc[sample_size - validation_size:,1:] ).reshape([validation_size,28,28,1])
 val_y = np.asarray(df_train.iloc[sample_size - validation_size:, 0] ).reshape([validation_size, 1] )
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.values
self.train_y = train_y.values
for tr_idx, va_idx in tqdm(folds.split(train_x)) :
tr_x, va_x = self.train_x[tr_idx], self.train_x[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<init_hyperparams>
df_test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 test_x = np.asarray(df_test.iloc[:, :] ).reshape([-1, 28, 28, 1] )
best_lgb_params = {
'seed': 2021,
'objective': 'regression',
'metric': 'rmse',
'verbosity': -1,
'feature_pre_filter': False,
'lambda_l1': 6.540486456085813,
'lambda_l2': 0.01548480538099245,
'num_leaves': 256,
'feature_fraction': 0.52,
'bagging_fraction': 0.6161835249194311,
'bagging_freq': 7,
'min_child_samples': 20
}
best_lgb_params["learning_rate"] = 0.001
best_lgb_params["early_stopping_round"] = 1000
best_lgb_params["num_iterations"] = 20000<statistical_test>
train_x = train_x/255
val_x = val_x/255
test_x = test_x/255
folds_average_lgbm = FoldsAverageLGBM(folds )<train_model>
model = models.Sequential()
folds_average_lgbm.fit(best_lgb_params, train_x, train_y )<compute_test_metric>
model.add(Conv2D(32,3, padding ="same",input_shape=(28, 28, 1)))
 model.add(LeakyReLU())
 model.add(Conv2D(32,3, padding ="same"))
model.add(LeakyReLU())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,3, padding ="same"))
model.add(LeakyReLU())
 model.add(Conv2D(64,3, padding ="same"))
model.add(LeakyReLU())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256,activation='relu'))
model.add(Dense(32,activation='relu'))
model.add(Dense(10,activation="sigmoid"))
np.sqrt(mean_squared_error(df_train.target, folds_average_lgbm.oof_preds))<predict_on_test>
initial_lr = 0.001
loss = "sparse_categorical_crossentropy"
model.compile(Adam(lr = initial_lr), loss = loss, metrics = ['accuracy'])
 model.summary()
y_pred = folds_average_lgbm.predict(test_x )<save_to_csv>
epochs = 20
batch_size = 256
history_1 = model.fit(train_x, train_y, batch_size = batch_size, epochs = epochs, validation_data =(val_x, val_y))
sub = df_sample.copy()
sub["target"] = y_pred
sub.to_csv("submission_lgbm_1.csv", index=False)
 sub.head()<load_from_csv>
val_p = np.argmax(model.predict(val_x), axis = 1)
 error = 0
confusion_matrix = np.zeros([10, 10])
 for i in range(val_x.shape[0]):
confusion_matrix[val_y[i], val_p[i]] += 1
if val_y[i] != val_p[i]:
error += 1
print("Confusion Matrix:
", confusion_matrix)
 print("
Errors in validation set: ", error)
 print("
Error Persentage: ",(error * 100)/ val_p.shape[0])
 print("
Accuracy: ", 100 -(error * 100)/ val_p.shape[0])
 print("
Validation set Shape: ", val_p.shape[0] )
train = pd.read_csv(input_path / 'train.csv', index_col='id')
 display(train.head() )<load_from_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(train_x )
test = pd.read_csv(input_path / 'test.csv', index_col='id')
 display(test.head() )<load_from_csv>
lrr = ReduceLROnPlateau(monitor = 'val_accuracy', patience = 2, verbose = 1, factor = 0.5, min_lr = 0.00001 )
submission = pd.read_csv(input_path / 'sample_submission.csv', index_col='id')
 display(submission.head() )<install_modules>
epochs = 30
history_2 = model.fit_generator(datagen.flow(train_x, train_y, batch_size = batch_size), steps_per_epoch = int(train_x.shape[0]/batch_size)+ 1, epochs = epochs, validation_data =(val_x, val_y), callbacks = [lrr])
!pip install pytorch-tabnet 
<prepare_x_and_y>
val_p = np.argmax(model.predict(val_x), axis = 1)
 error = 0
confusion_matrix = np.zeros([10, 10])
 for i in range(val_x.shape[0]):
confusion_matrix[val_y[i], val_p[i]] += 1
if val_y[i] != val_p[i]:
error += 1
print("Confusion Matrix:
", confusion_matrix)
 print("
Errors in validation set: ", error)
 print("
Error Persentage: ",(error * 100)/ val_p.shape[0])
 print("
Accuracy: ", 100 -(error * 100)/ val_p.shape[0])
 print("
Validation set Shape: ", val_p.shape[0] )
features = train.columns[1:-1]
X = train[features]
y = np.log1p(train["target"])
 X_test = test[features] 
<data_type_conversions>
test_y = np.argmax(model.predict(test_x), axis = 1 )
<train_on_grid><EOS> 
df_submission = pd.DataFrame([df_test.index + 1, test_y], ["ImageId", "Label"] ).transpose()
df_submission.to_csv("MySubmission.csv", index = False )