Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<count_values>
%matplotlib inline
np.random.seed()
sns.set(style='white', context='notebook', palette='deep')
 print("Done" )
train['gender'].value_counts()<feature_engineering>
dataset = pd.read_csv(".. /input/train.csv")
 competition_dataset = pd.read_csv(".. /input/test.csv")
 dataset.describe
train['age'] = train['age'].apply(lambda x: 122 if x > 122 else x)
 train['age'] = train['age'].apply(lambda x: 18 if x < 18 else x )<feature_engineering>
del dataset
print("Done" )
test['age'] = test['age'].apply(lambda x: 122 if x > 122 else x)
 test['age'] = test['age'].apply(lambda x: 18 if x < 18 else x )<count_values>
label = to_categorical(label, num_classes = 10)
 feature = feature / 255.0
competition_dataset = competition_dataset / 255.0
print("Done" )
train['signup_flow'].value_counts()<count_values>
feature_train, feature_val, label_train, label_val = train_test_split(feature, label, test_size = 0.1, stratify=label )
train['signup_method'].value_counts()<count_duplicates>
model_1 = Sequential()
model_1.add(Dense(200, activation = "relu", input_shape =(784,)))
 model_1.add(Dense(100, activation = "relu"))
model_1.add(Dense(60, activation = "relu"))
model_1.add(Dense(30, activation = "relu"))
model_1.add(Dense(10, activation = "softmax"))
optimizer = optimizers.SGD(lr=0.03, clipnorm=5.)
model_1.compile(optimizer= optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 print(model_1.summary() )
train[train.duplicated() ]<count_values>
history = model_1.fit(feature_train, label_train, batch_size = 100, epochs = 8,
validation_data =(feature_val, label_val), verbose = 1)
session_df['device_type'].value_counts()<count_values>
feature_train, feature_val, label_train, label_val = train_test_split(feature, label, test_size = 0.1, stratify=label )
session_df['action_detail'].value_counts() [:10]<filter>
model_2 = Sequential()
model_2.add(Conv2D(filters = 4, kernel_size =(5,5), strides = 1, padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model_2.add(Conv2D(filters = 8, kernel_size =(4,4), strides = 2, padding = 'Same',
activation ='relu'))
model_2.add(Conv2D(filters = 12, kernel_size =(4,4), strides = 2, padding = 'Same',
activation ='relu'))
model_2.add(Flatten())
 model_2.add(Dense(200, activation = "relu"))
model_2.add(Dense(10, activation = "softmax"))
optimizer = optimizers.SGD(lr=0.03, clipnorm=5.)
model_2.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 print(model_2.summary())
view_search_time = session_df[session_df.action_detail == 'view_search_results']
view_search_time<drop_column>
history = model_2.fit(feature_train, label_train, batch_size = 100, epochs = 16,
validation_data =(feature_val, label_val), verbose = 1)
labels = train['country_destination']
train.drop('country_destination', inplace = True, axis = 1 )<concatenate>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(feature_train)
 print("Done" )
data = pd.concat(( train, test), axis=0, ignore_index=True)
 data = data.drop(['id', 'date_first_booking'], axis=1 )<categorify>
model_3 = Sequential()
model_3.add(Conv2D(filters = 6, kernel_size =(6,6), strides = 1, padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model_3.add(Conv2D(filters = 12, kernel_size =(5,5), strides = 2, padding = 'Same',
activation ='relu'))
model_3.add(Conv2D(filters = 24, kernel_size =(4,4), strides = 2, padding = 'Same',
activation ='relu'))
model_3.add(Flatten())
 model_3.add(Dense(200, activation = "relu"))
model_3.add(Dropout(0.40))
model_3.add(Dense(10, activation = "softmax"))
model_3.compile(optimizer = 'adam' , loss = "categorical_crossentropy", metrics=["accuracy"])
 print(model_3.summary() )
cat_features = ['gender', 'signup_method', 'signup_flow', 'language', 'affiliate_channel',
'affiliate_provider', 'first_affiliate_tracked', 'signup_app', 'first_device_type', 'first_browser']
for f in cat_features:
data_dummy = pd.get_dummies(data[f], prefix=f)
 data.drop([f], axis=1, inplace = True)
 data = pd.concat(( data, data_dummy), axis=1 )<import_modules>
history = model_3.fit(datagen.flow(feature_train,label_train, batch_size=100),
epochs = 8, validation_data =(feature_val, label_val),
verbose = 2 )
from datetime import datetime
from sklearn.preprocessing import LabelEncoder<data_type_conversions>
model_4 = Sequential()
model_4.add(Conv2D(filters = 32, kernel_size =(5,5), strides = 1, padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model_4.add(BatchNormalization())
 model_4.add(Conv2D(filters = 32, kernel_size =(5,5), strides = 1, padding = 'Same',
activation ='relu'))
model_4.add(BatchNormalization())
 model_4.add(Dropout(0.4))
model_4.add(Conv2D(filters = 64, kernel_size =(3,3), strides = 2, padding = 'Same',
activation ='relu'))
model_4.add(BatchNormalization())
 model_4.add(Conv2D(filters = 64, kernel_size =(3,3), strides = 2, padding = 'Same',
activation ='relu'))
model_4.add(BatchNormalization())
 model_4.add(Dropout(0.4))
model_4.add(Flatten())
 model_4.add(Dense(256, activation = "relu"))
model_4.add(Dropout(0.4))
model_4.add(Dense(10, activation = "softmax"))
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model_4.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 print(model_4.summary() )
data['date_account_created'] = pd.to_datetime(data['date_account_created'] )<feature_engineering>
history = model_4.fit(datagen.flow(feature_train,label_train, batch_size=100),
epochs = 35, validation_data =(feature_val, label_val),
verbose = 2, callbacks=[learning_rate_reduction] )
data['ac_year'] = data['date_account_created'].dt.year
data['ac_month'] = data['date_account_created'].dt.month
data['ac_day'] = data['date_account_created'].dt.day<drop_column>
results = model_4.predict(competition_dataset)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<data_type_conversions><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission_MNIST.csv",index=False)