Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<sort_values>
pd.set_option('display.max_rows', 1000)
 warnings.filterwarnings("ignore")
 dftrain = pd.read_csv('.. /input/digit-recognizer/train.csv')
 dftest = pd.read_csv('.. /input/digit-recognizer/test.csv' )
print(cv_res.sort_values(ascending=False, by='CrossValMeans'))<train_model>
IMG_SIZE = 28
x_train = dftrain.iloc[:,1:]
x_train = x_train.values.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
 y_train = dftrain.iloc[:,0]
x_test = dftest
x_test = x_test.values.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
 x_train = x_train/255.0
x_test = x_test/255.0
def objective(trial):
params = {
'alpha': trial.suggest_loguniform("alpha", 0.1, 5),
'fit_intercept': trial.suggest_categorical('fit_intercept', [True, False]),
'normalize': trial.suggest_categorical('normalize', [True, False]),
}
reg = Ridge(**params)
 reg.fit(X_train, y_train)
 y_pred = reg.predict(X_test)
 mae = mean_absolute_error(y_test, y_pred)
 return mae 
<find_best_params>
X_train, X_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state = 42)
 datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1)
 datagen.fit(X_train )
study = optuna.create_study()
study.optimize(objective, n_trials=100)
 print(f'best score: {study.best_value:.4f}, best params: {study.best_params}' )<find_best_model_class>
earlystopping = EarlyStopping(monitor ="val_accuracy",
mode = 'auto', patience = 30,
restore_best_weights = True)
 model = Sequential()
model.add(Conv2D(128,(3, 3), input_shape = x_train.shape[1:]))
model.add(BatchNormalization())
 model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
model.add(Conv2D(512,(3, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
model.add(Flatten())
 model.add(Dense(512))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
params = {'alpha': 1.7927096237808111, 'fit_intercept': True, 'normalize': True}
reg = Ridge(**params)
 reg.fit(X_train, y_train)
 prediction_log = reg.predict(test_X)
 prediction =np.exp(prediction_log)
 print(prediction )<save_to_csv>
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True )
submission = pd.DataFrame({"Id":test_Id, "Prediction":prediction})
 submission.to_csv("submission.csv", index=False )<load_from_csv>
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
 EPOCHS = 1000
BATCH_SIZE=64
history = model.fit(datagen.flow(X_train, y_train), epochs=EPOCHS, batch_size=BATCH_SIZE, validation_data=(X_val, y_val), callbacks=[earlystopping] )
df = pd.read_csv('/kaggle/input/restaurant-revenue-prediction/train.csv.zip')
 df.shape<load_from_csv>
print("Max.Validation Accuracy: {}%".format(round(100*max(history.history['val_accuracy']), 2)) )
test_data = pd.read_csv('/kaggle/input/restaurant-revenue-prediction/test.csv.zip')
 test_data.shape<count_missing_values>
predictions = model.predict([x_test])
 solutions = []
for i in range(len(predictions)) :
solutions.append(np.argmax(predictions[i]))
df.sum().isnull()<feature_engineering>
final = pd.DataFrame()
final['ImageId']=[i+1 for i in dftest.index]
final['Label']=solutions
final.to_csv('submission.csv', index=False )