Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<count_values>
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )
df_train[df_train['StateHoliday'] == '0']['StateHoliday'].value_counts()<count_values>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
df_train['StateHoliday'] = df_train['StateHoliday'].apply(lambda x: 0 if x == '0' else x)
 df_train['StateHoliday'].value_counts()<load_from_csv>
X_train = X_train / 255.0
test = test / 255.0
df_store = pd.read_csv('.. /input/rossmann-store-sales/store.csv')
 df_store.head()<count_missing_values>
Y_train = to_categorical(Y_train, num_classes = 10 )
df_store.isnull().sum()<count_values>
random_seed = 2
df_store['StoreType'].value_counts()<count_values>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed )
df_store['Assortment'].value_counts()<count_values>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
df_store['StoreType'] = df_store['StoreType'].apply(lambda x: 1 if x == 'a' else(2 if x == 'b' else(3 if x == 'c' else 4)))
 df_store['StoreType'].value_counts()<count_values>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )
df_store['Assortment'] = df_store['Assortment'].apply(lambda x: 1 if x == 'a' else(2 if x == 'b' else 3))
df_store['Assortment'].value_counts()<feature_engineering>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
df_store['CompetitionDistance'] = df_store['CompetitionDistance'].fillna(max(df_store['CompetitionDistance']))
df_store.info()<categorify>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
def mapping(features):
for feature in features:
temp_dict = {}
temp_dict = pd.Series(df_store[feature].values, index = df_store['Store'] ).to_dict()
df_train[feature] = df_train['Store'].map(temp_dict )<categorify>
epochs = 29
batch_size = 86
mapping(['StoreType', 'Assortment', 'CompetitionDistance'] )<filter>
df_train[df_train['Sales'] == 0]<filter>
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 )
df_train[df_train['Open'] == 0]<count_values>
history = model.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] )
df_train[df_train['Open'] == 0]['Sales'].value_counts()<count_values>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<count_values><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )