Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<train_model>
%matplotlib inline
np.random.seed(2)
 dense_regularizer = L1L2(l2=0.0001)
 os.chdir('/kaggle/working')
 sns.set(style='white', context='notebook', palette='deep' )
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X_train, y_train)
 y_pred = model.predict(X_test )<compute_test_metric>
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )
accuracy_score(y_pred, y_test )<compute_test_metric>
Y = train["label"]
X = train.drop(labels = ["label"],axis = 1 )
confusion_matrix(y_pred, y_test )<feature_engineering>
X = X / 255.0
test = test / 255.0
test_data.Fare = test_data.Fare.fillna(test_data.Fare.mean() )<define_search_space>
Y = to_categorical(Y, num_classes = 10 )
param_grid = {
'n_estimators': [200, 500],
'max_features': ['auto', 'sqrt', 'log2'],
'max_depth' : [4,5,6,7,8],
'criterion' :['gini', 'entropy']
} 
<train_model>
random_seed = 2
X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size = 0.1, random_state=random_seed )
rfc1=RandomForestClassifier(random_state=42, max_features='log2', n_estimators= 200, max_depth=6, criterion='entropy')
 rfc1.fit(X_train, y_train )<save_to_csv>
model1 = Sequential()
model1.add(Conv2D(filters = 32, kernel_size =(5,5), padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model1.add(Conv2D(filters = 32, kernel_size =(5,5), padding = 'Same', activation ='relu'))
model1.add(MaxPool2D(pool_size=(2,2)))
 model1.add(Dropout(0.25))
model1.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model1.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model1.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model1.add(Dropout(0.25))
model1.add(Flatten())
 model1.add(Dense(256, activation = "relu"))
model1.add(Dropout(0.5))
model1.add(Dense(10, activation = "softmax"))
model1.compile(optimizer = 'adam' , loss = "categorical_crossentropy", metrics=["accuracy"] )
predictions = rfc1.predict(test_data[features])
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission3.csv', index=False)
 print("Your submission was successfully saved!" )<import_modules>
epochs = 5
batch_size = 86
model1.fit(
 X_train,
Y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(X_val, Y_val),
verbose=2
)
import numpy as np
import pandas as pd
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.naive_bayes import ComplementNB
from sklearn.naive_bayes import BernoulliNB
from sklearn.naive_bayes import CategoricalNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import Perceptron
from sklearn.svm import SVC
from sklearn.linear_model import SGDClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier<load_from_csv>
results = model1.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission1 = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission1.to_csv("sub1.csv",index=False )
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv' )<count_missing_values>
model1.compile(optimizer = 'RMSprop' , loss = "categorical_crossentropy", metrics=["accuracy"] )
missing_values = train_data.isna().any()
print('Columns which have missing values:
{0}'.format(missing_values[missing_values == True].index.tolist()))<count_missing_values>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
print("Percentage of missing values in `Age` column: {0:.2f}".format(100.*(train_data.Age.isna().sum() /len(train_data))))
print("Percentage of missing values in `Cabin` column: {0:.2f}".format(100.*(train_data.Cabin.isna().sum() /len(train_data))))
print("Percentage of missing values in `Embarked` column: {0:.2f}".format(100.*(train_data.Embarked.isna().sum() /len(train_data))))<count_duplicates>
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(X_train )
duplicates = train_data.duplicated().sum()
print('Duplicates in train data: {0}'.format(duplicates))<count_unique_values>
epochs = 5
batch_size = 86
history = model1.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )
categorical = train_data.nunique().sort_values(ascending=True)
 print('Categorical variables in train data:
{0}'.format(categorical))<drop_column>
results = model1.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission2 = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission2.to_csv("sub2.csv",index=False )
def clean_data(data):
data.drop(['Cabin'], axis=1, inplace=True)
 data.drop(['Name', 'Ticket', 'Fare', 'Embarked'], axis=1, inplace=True)
 return data
train_data = clean_data(train_data)
 test_data = clean_data(test_data )<data_type_conversions>
def Model_2(x=None):
model = Sequential()
model.add(Conv2D(64,(5, 5), input_shape=(28,28,1), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(64,(5, 5), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(64,(5, 5), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(128,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(128,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(128,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(256,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(256,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(256,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(512,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(Conv2D(512,(3, 3), padding='same', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
model.add(BatchNormalization())
 model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(3, 3)))
 model.add(Flatten())
 model.add(Dense(10, activation='softmax', kernel_regularizer=dense_regularizer,kernel_initializer="he_normal"))
return model
model2 = Model_2()
train_data['Sex'].replace({'male':0, 'female':1}, inplace=True)
 test_data['Sex'].replace({'male':0, 'female':1}, inplace=True)
 all_data = pd.concat([train_data, test_data])
 average = all_data.Age.median()
print("Average Age: {0}".format(average))
train_data.fillna(value={'Age': average}, inplace=True)
 test_data.fillna(value={'Age': average}, inplace=True )<prepare_x_and_y>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model2.compile(optimizer = optimizer , loss = "sparse_categorical_crossentropy", metrics=["accuracy"] )
X = train_data.drop(['Survived', 'PassengerId'], axis=1)
 y = train_data['Survived']
test_X = test_data.drop(['PassengerId'], axis=1 )<train_on_grid>
Y = train["label"]
Y = np.array(Y )
best_models = {}
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
 def print_best_parameters(hyperparameters, best_parameters):
value = "Best parameters: "
for key in hyperparameters:
value += str(key)+ ": " + str(best_parameters[key])+ ", "
if hyperparameters:
print(value[:-2])
 def get_best_model(estimator, hyperparameters, fit_params={}):
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
 grid_search = GridSearchCV(estimator=estimator, param_grid=hyperparameters, n_jobs=-1, cv=cv, scoring="accuracy")
 best_model = grid_search.fit(train_X, train_y, **fit_params)
 best_parameters = best_model.best_estimator_.get_params()
print_best_parameters(hyperparameters, best_parameters)
 return best_model
def evaluate_model(model, name):
print("Accuracy score:", accuracy_score(train_y, model.predict(train_X)))
 best_models[name] = model<choose_model_class>
k_fold = StratifiedKFold(n_splits=10, random_state=12, shuffle=True)
 batch_size = 86
epochs = 10
for k_train_index, k_test_index in k_fold.split(X, Y):
model2.fit_generator(datagen.flow(X[k_train_index,:],Y[k_train_index], batch_size=batch_size),
epochs = epochs, validation_data =(X[k_test_index,:],Y[k_test_index]),
verbose = 2, steps_per_epoch=X[k_train_index,:].shape[0] // batch_size
, callbacks=[learning_rate_reduction] )
hyperparameters = {
'solver' : ['newton-cg', 'lbfgs', 'liblinear'],
'penalty' : ['l2'],
'C' : [100, 10, 1.0, 0.1, 0.01]
}
estimator = LogisticRegression(random_state=1)
 best_model_logistic = get_best_model(estimator, hyperparameters )<find_best_params>
val_loss, val_acc = model2.evaluate(X, Y)
 val_acc
evaluate_model(best_model_logistic.best_estimator_, 'logistic' )<choose_model_class>
results = model2.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission3 = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission3.to_csv("sub3.csv",index=False )