Kaggle competition: Digit Recognizer
hyperparameters = {
'loss' : ['hinge', 'log', 'modified_huber', 'squared_hinge', 'perceptron'],
'penalty' : ['l1', 'l2', 'elasticnet'],
'alpha' : [0.01, 0.1, 1, 10]
}
estimator = SGDClassifier(random_state=1, early_stopping=True)
 best_model_sgd = get_best_model(estimator, hyperparameters )<find_best_params>
%matplotlib inline
np.random.seed(2)
evaluate_model(best_model_sgd.best_estimator_, 'sgd' )<choose_model_class>
from keras.layers import Dense
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers import Activation
hyperparameters = {
'loss' : ['deviance', 'exponential'],
'learning_rate' : [0.01, 0.1, 0.2, 0.3],
'n_estimators' : [50, 100, 200],
'subsample' : [0.1, 0.2, 0.5, 1.0],
'max_depth' : [2, 3, 4, 5]
}
estimator = GradientBoostingClassifier(random_state=1)
 best_model_gbc = get_best_model(estimator, hyperparameters )<find_best_params>
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 train_df.head()
evaluate_model(best_model_gbc.best_estimator_, 'gbc' )<choose_model_class>
test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 test_df.head()
hyperparameters = {
'n_estimators' : [10, 50, 100, 500],
'learning_rate' : [0.001, 0.01, 0.1, 1.0]
}
estimator = AdaBoostClassifier(random_state=1)
 best_model_adaboost = get_best_model(estimator, hyperparameters )<find_best_params>
columns = train_df.columns
X = train_df[columns[columns != 'label']]
y = train_df['label']
evaluate_model(best_model_adaboost.best_estimator_, 'adaboost' )<choose_model_class>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state = 4 )
hyperparameters = {
'criterion' : ['gini', 'entropy'],
'splitter' : ['best', 'random'],
'max_depth' : [None, 1, 2, 3, 4, 5],
'min_samples_split' : list(range(2,5)) ,
'min_samples_leaf' : list(range(1,5))
}
estimator = DecisionTreeClassifier(random_state=1)
 best_model_decision_tree = get_best_model(estimator, hyperparameters )<find_best_params>
X_train = X_train.values.reshape(X_train.shape[0], 28, 28, 1 ).astype('float32')
 X_test = X_test.values.reshape(X_test.shape[0], 28, 28, 1 ).astype('float32' )
evaluate_model(best_model_decision_tree.best_estimator_, 'decision_tree' )<define_search_space>
X_train = X_train / 255
X_test = X_test / 255
hyperparameters = {
'n_estimators' : list(range(10, 50, 10)) ,
'max_features' : ['auto', 'sqrt', 'log2'],
'criterion' : ['gini', 'entropy'],
'max_depth' : [None, 1, 2, 3, 4, 5],
'min_samples_split' : list(range(2,5)) ,
'min_samples_leaf' : list(range(1,5))
}
estimator = RandomForestClassifier(random_state=1)
 best_model_random_forest = get_best_model(estimator, hyperparameters )<find_best_params>
y_train = to_categorical(y_train)
 y_test = to_categorical(y_test)
 num_classes = y_test.shape[1]
evaluate_model(best_model_random_forest.best_estimator_, 'random_forest' )<init_hyperparams>
def convolutional_model() :
ADAMAX = optimizers.Adamax(lr = 0.002, beta_1 = 0.9, beta_2 = 0.999)
 model = Sequential()
model.add(Conv2D(32,(4, 4), activation = 'relu', input_shape =(28, 28, 1)))
 model.add(Conv2D(64,(3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size =(2, 2), strides =(2, 2)))
 model.add(BatchNormalization(axis = -1))
model.add(Dropout(0.2))
model.add(Conv2D(128,(3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size =(2, 2), strides =(2, 2)))
 model.add(BatchNormalization(axis = -1))
model.add(Dropout(0.2))
model.add(Conv2D(128,(2, 2), activation = 'relu'))
model.add(Conv2D(256,(2, 2), activation = 'relu'))
model.add(MaxPooling2D(pool_size =(2, 2), strides =(2, 2)))
 model.add(BatchNormalization(axis = -1))
model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dense(128, activation = 'relu'))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation = 'softmax'))
model.compile(optimizer = ADAMAX, loss = 'categorical_crossentropy', metrics = ['accuracy'])
 return model
hyperparameters = {
'learning_rate' : [0.3, 0.4, 0.5],
'gamma' : [0, 0.4, 0.8],
'max_depth' : [2, 3, 4],
'reg_lambda' : [0, 0.1, 1],
'reg_alpha' : [0.1, 1]
}
fit_params = {
'verbose' : False,
'early_stopping_rounds' : 40,
'eval_metric' : 'logloss',
'eval_set' : [(val_X, val_y)]
}
estimator = XGBClassifier(seed=1, tree_method='gpu_hist', predictor='gpu_predictor', use_label_encoder=False)
 best_model_xgb = get_best_model(estimator, hyperparameters, fit_params )<find_best_params>
gen = ImageDataGenerator(rotation_range = 12, width_shift_range = 0.1, shear_range = 0.1,
height_shift_range = 0.1, zoom_range = 0.1, fill_mode = 'nearest', horizontal_flip = False,
vertical_flip = False, featurewise_center = False,
samplewise_center = False, featurewise_std_normalization = False,
samplewise_std_normalization = False)
 test_gen = ImageDataGenerator()
train_generator = gen.flow(X_train, y_train, batch_size = 32)
 test_generator = test_gen.flow(X_test, y_test, batch_size = 32 )
evaluate_model(best_model_xgb.best_estimator_, 'xgb' )<choose_model_class>
reduce_lr = ReduceLROnPlateau(monitor = 'val_accuracy', patience = 3, verbose = 1, factor = 0.4, min_lr = 0.00002,
mode = 'auto', cooldown = 0 )
hyperparameters = {
'boosting_type' : ['gbdt', 'dart', 'goss'],
'num_leaves' : [4, 8, 16, 32],
'learning_rate' : [0.01, 0.1, 1],
'n_estimators' : [25, 50, 100],
'reg_alpha' : [0, 0.1, 1],
'reg_lambda' : [0, 0.1, 1],
}
estimator = LGBMClassifier(random_state=1, device='gpu')
 best_model_lgbm = get_best_model(estimator, hyperparameters )<find_best_params>
model = convolutional_model()
epochs = 80
history = model.fit_generator(train_generator, steps_per_epoch = 40000//16, epochs = epochs,
validation_data = test_generator, validation_steps = 10000//8, verbose = 1,
callbacks=[reduce_lr])
 scores = model.evaluate(X_test, y_test, verbose = 0)
 print("Accuracy: {}
Error: {}".format(scores[1], 100-scores[1]*100))
plot_loss_accuracy(history )
evaluate_model(best_model_lgbm.best_estimator_, 'lgbm' )<save_to_csv>
test_data = test_df.values.reshape(test_df.shape[0], 28, 28, 1 ).astype('float32')
 test_data = test_data / 255
Y_pred = model.predict(test_data )
for model in best_models:
predictions = best_models[model].predict(test_X)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission_' + model + '.csv', index=False )<set_options>
Y_pred = np.argmax(Y_pred,axis = 1)
 Y_pred = pd.Series(Y_pred, name = "Label" )
!pip install -q -U keras-tuner
clear_output() 
<define_variables>
submission_df = pd.DataFrame({
"ImageId": pd.Series(range(1, len(Y_pred)+1)) ,
"Label": pd.Series(Y_pred)} )
<load_from_csv><EOS> 
submission_df.to_csv('/kaggle/working/Submission.csv', index = False )