Kaggle competition: Digit Recognizer
df_test.isnull().sum()<filter>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 print(train.shape, test.shape )
df_test[df_test['Open'].isnull() ]<count_missing_values>
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 del train
df_test[df_test['Open'].isnull() ]['Date'].value_counts()<count_values>
X_train = X_train / 255.0
test = test / 255.0
df_test[df_test['Store'] == 622]['Open'].value_counts()<feature_engineering>
X_train = X_train.values.reshape(X_train.shape[0], 28, 28, 1)
 X_test = test.values.reshape(test.shape[0], 28, 28, 1)
 Y_train = to_categorical(Y_train, num_classes = 10)
 Y_train
df_test['Open'] = df_test['Open'].fillna(1)
 df_test.isnull().sum()<categorify>
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization
from keras.utils import np_utils
def mapping(features):
for feature in features:
temp_dict = {}
temp_dict = pd.Series(df_store[feature].values, index = df_store['Store'] ).to_dict()
df_test[feature] = df_test['Store'].map(temp_dict )<categorify>
model = Sequential()
dim = 28
nclasses = 10
model.add(Conv2D(filters=32, kernel_size=(5,5), padding='same', activation='relu', input_shape=(dim,dim,1)))
 model.add(Conv2D(filters=32, kernel_size=(5,5), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters=64, kernel_size=(5,5), padding='same', activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(5,5), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(120, activation='relu'))
model.add(Dense(84, activation='relu'))
model.add(Dense(nclasses, activation='softmax'))
mapping(['StoreType', 'Assortment', 'Promo2', 'CompetitionDistance'] )<feature_engineering>
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
df_test['CompetitionDistanceLog10'] = np.log10(df_test['CompetitionDistance'])
 df_test.head()<drop_column>
learning_rate_reduction = ReduceLROnPlateau(monitor='acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=1e-7)
 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)
 history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=512),
epochs=50,
verbose=1,
steps_per_epoch=X_train.shape[0]/512,
callbacks=[learning_rate_reduction] )
df_test.drop('CompetitionDistance', inplace = True, axis = 1)
 df_test.head()<feature_engineering>
loss_and_metrics = model.evaluate(X_train, Y_train, batch_size=128)
 print(loss_and_metrics )
df_test['DayOfYear'] = df_test['Date'].map(lambda x: datetime.datetime.strptime(str(x),'%Y-%m-%d' ).timetuple().tm_yday)
 df_test.head(10 )<data_type_conversions>
pred = model.predict_classes(X_test)
 submission = pd.DataFrame({
"ImageId": list(range(1,len(pred)+ 1)) ,
"Label": pred})
 submission.to_csv("submission.csv", index=False, header=True )