Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<set_options>
%matplotlib inline
warnings.filterwarnings('ignore' )<load_from_csv>
data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 print(data.shape )
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 sub = pd.read_csv('/kaggle/input/titanic/gender_submission.csv' )<sort_values>
test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 print(test_data.shape )
train.isnull().sum().sort_values(ascending = False )<sort_values>
sample_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 print(sample_submission.shape )
test.isnull().sum().sort_values(ascending = False )<feature_engineering>
encoder = OneHotEncoder(sparse=False,categories='auto')
 yy = [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]
encoder.fit(yy)
 train_label = train_label.reshape(-1,1)
 val_label = val_label.reshape(-1,1)
 train_label = encoder.transform(train_label)
 val_label = encoder.transform(val_label)
 print('train_label shape: %s'%str(train_label.shape))
print('val_label shape: %s'%str(val_label.shape))
train.loc[train.Cabin.notnull() ,'Cabin']=1
train.loc[train.Cabin.isnull() ,'Cabin']=0<feature_engineering>
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD
from keras.layers.normalization import BatchNormalization
from keras.layers import LeakyReLU
test.loc[test.Cabin.notnull() ,'Cabin']=1
test.loc[test.Cabin.isnull() ,'Cabin']=0<count_missing_values>
model = Sequential()
model.add(Conv2D(32,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(32,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(256, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu', name='my_dense'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.summary()
train.Cabin.isnull().sum()<count_missing_values>
datagen = ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range = 15,
horizontal_flip = False,
zoom_range = 0.20 )
test.Cabin.isnull().sum()<define_variables>
model.compile(loss='categorical_crossentropy',optimizer=Adam() ,metrics=['accuracy'])
 datagen.fit(train_image)
 history = model.fit_generator(datagen.flow(train_image,train_label, batch_size=32),
epochs = 75,
shuffle=True,
validation_data =(val_image,val_label),
verbose = 1,
steps_per_epoch=train_image.shape[0] // 32 )
def detect_outliers(df, n, features):
outlier_indices = []
for col in features:
Q1 = np.percentile(df[col], 25)
 Q3 = np.percentile(df[col], 75)
 IQR = Q3 - Q1
outlier_step = 1.5 * IQR
outlier_list_col = df[(df[col] < Q1 - outlier_step)|(df[col] > Q3 + outlier_step)].index
outlier_indices.extend(outlier_list_col)
 outlier_indices = Counter(outlier_indices)
 multiple_outliers = list(key for key, value in outlier_indices.items() if value > n)
 return multiple_outliers<define_variables>
intermediate_output = intermediate_layer_model.predict(train_image)
 intermediate_output = pd.DataFrame(data=intermediate_output )
outliers_to_drop = detect_outliers(train, 2, ['Age', 'SibSp', 'Parch', 'Fare'])
 print("The {} indices for the outliers to drop are: ".format(len(outliers_to_drop)) , outliers_to_drop )<filter>
val_data = intermediate_output[40000:]
train.loc[outliers_to_drop, :]<drop_column>
submission_cnn = model.predict(test_image )
print("Before: {} rows".format(len(train)))
 train = train.drop(outliers_to_drop, axis = 0 ).reset_index(drop = True)
 print("After: {} rows".format(len(train)) )<filter>
intermediate_test_output = intermediate_layer_model.predict(test_image)
 intermediate_test_output = pd.DataFrame(data=intermediate_test_output )
outliers_to_drop_to_test = detect_outliers(test, 2, ['Age', 'SibSp', 'Parch', 'Fare'] )<filter>
xgbmodel = XGBClassifier(objective='multi:softprob',
num_class= 10)
 xgbmodel.fit(intermediate_output, train_label1)
 xgbmodel.score(val_data, val_label1 )
test.loc[outliers_to_drop_to_test, :]<count_values>
submission_xgb = xgbmodel.predict(intermediate_test_output )
train['SibSp'].value_counts(dropna = False )<sort_values>
submission_cnn = submission_cnn.astype(int)
 submission_xgb = submission_xgb.astype(int)
train[['SibSp', 'Survived']].groupby('SibSp', as_index = False ).mean().sort_values(by = 'Survived', ascending = False )<count_values>
submission_cnn
label = np.argmax(submission_cnn,1)
 id_ = np.arange(0,label.shape[0])
 label
train['Parch'].value_counts(dropna = False )<sort_values>
final_sub = submission_xgb
train[['Parch', 'Survived']].groupby('Parch', as_index = False ).mean().sort_values(by = 'Survived', ascending = False )<count_missing_values>
save = pd.DataFrame({'ImageId':sample_submission.ImageId,'label':final_sub})
 print(save.head(10))
save.to_csv('submission.csv',index=False )
train['Age'].isnull().sum()<count_missing_values>
%matplotlib inline
train['Fare'].isnull().sum()<count_values>
data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 print(data.shape )
train['Pclass'].value_counts(dropna = False )<sort_values>
test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 print(test_data.shape )
train[['Pclass', 'Survived']].groupby('Pclass', as_index = False ).mean().sort_values(by = 'Survived', ascending = False )<count_values>
sample_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 print(sample_submission.shape )
train['Sex'].value_counts(dropna = False )<sort_values>
encoder = OneHotEncoder(sparse=False,categories='auto')
 yy = [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]
encoder.fit(yy)
 train_label = train_label.reshape(-1,1)
 val_label = val_label.reshape(-1,1)
 train_label = encoder.transform(train_label)
 val_label = encoder.transform(val_label)
 print('train_label shape: %s'%str(train_label.shape))
print('val_label shape: %s'%str(val_label.shape))
train[['Sex', 'Survived']].groupby('Sex', as_index = False ).mean().sort_values(by = 'Survived', ascending = False )<count_values>
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD
from keras.layers.normalization import BatchNormalization
from keras.layers import LeakyReLU
train['Embarked'].value_counts(dropna = False )<sort_values>
model = Sequential()
model.add(Conv2D(32,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(32,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(256, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu', name='my_dense'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.summary()
train[['Embarked', 'Survived']].groupby(['Embarked'], as_index = False ).mean().sort_values(by = 'Survived', ascending = False )<drop_column>
datagen = ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range = 15,
horizontal_flip = False,
zoom_range = 0.20 )
train = train.drop(['Ticket'], axis = 1)
 test = test.drop(['Ticket'], axis = 1 )<sort_values>
model.compile(loss='categorical_crossentropy',optimizer=Adam() ,metrics=['accuracy'])
 datagen.fit(train_image)
 history = model.fit_generator(datagen.flow(train_image,train_label, batch_size=32),
epochs = 75,
shuffle=True,
validation_data =(val_image,val_label),
verbose = 1,
steps_per_epoch=train_image.shape[0] // 32 )
train.isnull().sum().sort_values(ascending = False )<set_options>
intermediate_output = intermediate_layer_model.predict(train_image)
 intermediate_output = pd.DataFrame(data=intermediate_output )
mode = train['Embarked'].dropna().mode() [0]
mode<set_options>
val_data = intermediate_output[40000:]
train['Embarked'].fillna(mode, inplace = True )<sort_values>
submission_cnn = model.predict(test_image )
test.isnull().sum().sort_values(ascending = False )<correct_missing_values>
intermediate_test_output = intermediate_layer_model.predict(test_image)
 intermediate_test_output = pd.DataFrame(data=intermediate_test_output )
test['Fare'].fillna(median, inplace = True )<concatenate>
xgbmodel = XGBClassifier(objective='multi:softprob',
num_class= 10)
 xgbmodel.fit(intermediate_output, train_label1)
 xgbmodel.score(val_data, val_label1 )
combine = pd.concat([train, test], axis = 0 ).reset_index(drop = True)
 combine.head()<sort_values>
submission_xgb = xgbmodel.predict(intermediate_test_output )
combine.isnull().sum().sort_values(ascending = False )<categorify>
submission_cnn = submission_cnn.astype(int)
 submission_xgb = submission_xgb.astype(int)
combine['Sex'] = combine['Sex'].map({'male': 0, 'female': 1} )<filter>
submission_cnn
label = np.argmax(submission_cnn,1)
 id_ = np.arange(0,label.shape[0])
 label
age_nan_indices = list(combine[combine['Age'].isnull() ].index)
 len(age_nan_indices )<define_variables>
final_sub = submission_xgb
<count_missing_values><EOS> 
save = pd.DataFrame({'ImageId':sample_submission.ImageId,'label':final_sub})
 print(save.head(10))
save.to_csv('submission.csv',index=False )