Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<feature_engineering>
tf.set_random_seed(42 )
for i in test_ingred_miss:
test_ingredients_count[i] = 0
len(test_ingredients_count )<feature_engineering>
train_path = os.path.join('.. ', 'input', 'train.csv')
 test_path = os.path.join('.. ', 'input', 'test.csv')
 size = 28
lr = 0.001
num_classes = 10
epochs = 30
batch_size = 128
for i in train_ingredients_count.keys() :
train_data[i] = np.zeros(len(train_data))<feature_engineering>
raw_train_df = pd.read_csv(train_path)
 raw_test_df = pd.read_csv(test_path )
for i in test_ingredients_count.keys() :
test_data[i] = np.zeros(len(test_data))<feature_engineering>
def parse_train_df(_train_df):
labels = _train_df.iloc[:,0].values
imgs = _train_df.iloc[:,1:].values
imgs_2d = np.array([[[[float(imgs[index][i*28 + j])/ 255] for j in range(28)] for i in range(28)] for index in range(len(imgs)) ])
 processed_labels = [[0 for _ in range(10)] for i in range(len(labels)) ]
for i in range(len(labels)) :
processed_labels[i][labels[i]] = 1
return np.array(processed_labels), imgs_2d
def parse_test_df(test_df):
imgs = test_df.iloc[:, 0:].values
imgs_2d = np.array([[[[float(imgs[index][i * 28 + j])/ 255] for j in range(28)] for i in range(28)] for index in
range(len(imgs)) ])
 return imgs_2d
for i in range(len(train_data)) :
for j in train_data['ingredients'][i]:
train_data[j].iloc[i] = 1<feature_engineering>
y_train_set, x_train_set = parse_train_df(raw_train_df)
 x_test = parse_test_df(raw_test_df)
 x_train, x_val, y_train, y_val = train_test_split(x_train_set, y_train_set, test_size=0.20, random_state=42 )
for i in range(len(test_data)) :
for j in test_data['ingredients'][i]:
test_data[j].iloc[i] = 1<drop_column>
print("Number of 1: {}".format(len(raw_train_df[raw_train_df['label'] == 1])))
 print("Number of 5: {}".format(len(raw_train_df[raw_train_df['label'] == 5])) )
test_data=test_data[train_data.drop('cuisine',axis=1 ).columns]<import_modules>
model = keras.Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), strides=(1, 1),
activation='relu',
input_shape=(size, size, 1)))
 model.add(Conv2D(32,(3, 3), activation='relu', strides=(2, 2)))
 model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(Conv2D(64,(3, 3), activation='relu', strides=(2, 2)))
 model.add(BatchNormalization())
 model.add(Dropout(0.3))
model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=Adam(lr),
metrics=['accuracy'])
 checkpoint = ModelCheckpoint('model_ckpt.{epoch:02d}.hdf5',
save_best_only=True,
save_weights_only=True)
 lr_reducer = ReduceLROnPlateau(monitor='val_acc', factor=0.5, patience=3,
mode='max', cooldown=3, verbose=1)
 callback_list = [checkpoint, lr_reducer]
from sklearn.linear_model import LogisticRegression
from sklearn import preprocessing<prepare_x_and_y>
training_history = model.fit(
 x_train,
y_train,
epochs=epochs,
verbose=1,
validation_data=(x_val, y_val),
callbacks=callback_list
)
X = train_data.drop(['id','cuisine','ingredients'],axis =1)
 Y = train_data['cuisine']<split>
image_generator = ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1 
)
 image_generator.fit(x_train )
X_train,X_val,y_train,y_val = train_test_split(X,Y,random_state =42 )<train_model>
model_augmented = keras.Sequential()
model_augmented.add(Conv2D(32, kernel_size=(3, 3), strides=(1, 1),
activation='relu',
input_shape=(size, size, 1)))
 model_augmented.add(Conv2D(32,(3, 3), activation='relu', strides=(2, 2)))
 model_augmented.add(BatchNormalization())
 model_augmented.add(Dropout(0.3))
model_augmented.add(Conv2D(64,(3, 3), activation='relu'))
model_augmented.add(Conv2D(64,(3, 3), activation='relu', strides=(2, 2)))
 model_augmented.add(BatchNormalization())
 model_augmented.add(Dropout(0.3))
model_augmented.add(Conv2D(128,(3, 3), activation='relu'))
model_augmented.add(BatchNormalization())
 model_augmented.add(Flatten())
 model_augmented.add(Dense(256, activation='relu'))
model_augmented.add(Dropout(0.25))
model_augmented.add(Dense(128, activation='relu'))
model_augmented.add(Dense(num_classes, activation='softmax'))
model_augmented.compile(loss=keras.losses.categorical_crossentropy,
optimizer=RMSprop(0.001),
metrics=['accuracy'])
 aug_result = model_augmented.fit_generator(
 image_generator.flow(x_train, y_train, batch_size=batch_size),
epochs=epochs,
steps_per_epoch=len(x_train)// batch_size,
verbose=1,
validation_data=(x_val, y_val),
callbacks=callback_list 
)
 model_augmented.save('mnist_model.h5' )
lr = LogisticRegression(solver='liblinear')
 lr.fit(X_train,y_train )<compute_test_metric>
pred = model.predict(x_test)
 pred_aug = model_augmented.predict(x_test )
<predict_on_test><EOS> 
def convert_prediction_result(model_result):
result = []
for i in range(len(model_result)) :
result += [np.argmax(model_result[i])]
return result
def write_submission(_submission_path, result_arr):
f_out = open(_submission_path, 'w')
 f_out.write("ImageId,Label
")
 for i in range(len(result_arr)) :
f_out.write("{},{}
".format(i+1, result_arr[i]))
f_out.close()
write_submission('submission_base.csv', convert_prediction_result(pred))
write_submission('submission_aug.csv', convert_prediction_result(pred))