Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<data_type_conversions>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from keras.layers import Dropout, Dense, Conv2D, AveragePooling2D, Flatten, MaxPooling2D
from keras.models import Sequential, load_model
from sklearn.metrics import accuracy_score
test['Age'].fillna(test['Age'].median() , inplace = True )<feature_engineering>
train_data = pd.read_csv('.. /input/digit-recognizer/train.csv')
 prediction = pd.read_csv('.. /input/digit-recognizer/test.csv')
 submission1 = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv' )
class_fares = test.groupby('Pclass')['Fare'].median()
test['median_fare'] = test['Pclass'].apply(lambda x: class_fares[x])
 test['Fare'].fillna(test['median_fare'], inplace = True,)
 del test['median_fare']<count_missing_values>
train_data.isnull().sum().sum()
test.isnull().sum()<count_missing_values>
train_data, test_data = train_test_split(train_data, stratify=train_data["label"], test_size = 0.20)
 train_data, cv_data = train_test_split(train_data, stratify=train_data["label"], test_size = 0.25)
 train_data.shape, test_data.shape, cv_data.shape
test.isnull().sum()<concatenate>
train_y = train_data['label']
train_x = train_data.drop(columns = ['label'])
 cv_y = cv_data['label']
cv_x = cv_data.drop(columns = ['label'])
 test_y = test_data['label']
test_x = test_data.drop(columns = ['label'] )
test = str_to_cat(test)
 test,my_table = mydf_preprocessor(test,null_table = None)
 print(test.shape)
 test.head(5 )<count_duplicates>
train_x = np.array(train_x)
 train_x = train_x.reshape(25200, 28, 28, 1)
 train_y = np.array(pd.get_dummies(train_y))
cv_x = np.array(cv_x)
 cv_x = cv_x.reshape(8400, 28, 28, 1)
 cv_y = np.array(pd.get_dummies(cv_y))
test_x = np.array(test_x)
 test_x = test_x.reshape(8400, 28, 28, 1 )
test.duplicated().sum()<drop_column>
prediction.isnull().sum().sum()
train = train.drop('Name', axis = 1)
 test = test.drop(columns=['Name'], axis = 1 )<categorify>
prediction = np.array(prediction)
 prediction = prediction.reshape(28000, 28, 28, 1 )
X_train = train.drop('Survived', axis=1)
 X_train = pd.get_dummies(X_train,columns=['Embarked','Pclass'])
 Y_train = train['Survived']
print(X_train.shape,Y_train.shape )<normalization>
def model() :
model = Sequential()
model.add(Conv2D(32,kernel_size=3,activation='relu',input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
return model
model = model()
model.summary()
scaler = StandardScaler().fit(X_train)
 scaled_X = scaler.transform(X_train)
 print(scaled_X )<categorify>
model.compile(loss = 'categorical_crossentropy', optimizer = "adam", metrics = ["accuracy"])
 hist = model.fit(train_x, train_y, validation_data=(cv_x, cv_y), epochs = 40 )
x_test = pd.get_dummies(test,columns=['Embarked','Pclass'] )<normalization>
log_frame = pd.DataFrame(columns = ["Epoch", "Train_Loss", "Train_Accuracy", "CV_Loss", "CV_Accuracy"])
 log_frame["Epoch"] = epoch_number
log_frame["Train_Loss"] = train_loss
log_frame["Train_Accuracy"] = train_acc
log_frame["CV_Loss"] = val_loss
log_frame["CV_Accuracy"] = val_acc
log_frame
sc = StandardScaler().fit(x_test)
 sc_X = sc.transform(x_test)
 print(sc_X )<train_model>
test_predict = model.predict(test_x )
model_1 = RandomForestClassifier(n_jobs = -1, n_estimators = 10,
bootstrap = True)
 model_1.fit(scaled_X,Y_train )<predict_on_test>
l = []
for i in range(test_predict.shape[0]):
j = test_predict[i].argmax()
l.append(j)
 test_predict = pd.DataFrame(l )
pred = model_1.predict(scaled_X )<train_on_grid>
acc = accuracy_score(test_y, l)* 100
acc
parameters = {'n_estimators': [20, 50, 60, 80, 90, 100, 120, 150, 200], 'max_features': ["auto", "sqrt", "log2"]}
rfc = RandomForestClassifier(random_state = 1)
 cls = GridSearchCV(estimator = rfc, param_grid = parameters)
 cls.fit(scaled_X, Y_train)
 cls.best_params_<train_model>
prediction_predict = model.predict(prediction )
model_2 = RandomForestClassifier(n_estimators=80, max_features="auto")
 model_2.fit(scaled_X, Y_train)
 y_pred = model_2.predict(sc_X )<save_to_csv>
l = []
for i in range(prediction_predict.shape[0]):
j = prediction_predict[i].argmax()
l.append(j)
 submission2 = pd.DataFrame(l)
 submission2 = submission2.rename(columns = {0 : 'Label'} )
sub_df = pd.DataFrame({'PassengerId' : test["PassengerId"],'Survived' : y_pred})
 submission = sub_df.to_csv('submission.csv',index=False )<set_options>
submission1.drop(columns = ['Label'], inplace = True)
 submission = pd.concat([submission1, submission2], axis = 1 )
sns.set(style="ticks", context="talk")
 warnings.filterwarnings('ignore' )<load_from_csv>
t = prediction.reshape(28000, 28, 28)
 plt.imshow(t[2222])
 print(" The label predicted is ", submission["Label"][2222] )
<feature_engineering><EOS> 
submission.to_csv('./submission.csv',index=False )