Kaggle competition: Digit Recognizer
def days_from_christmas_for_train(x):
if x['Year']== 2010 :
diff=datetime.datetime(2010, 12, 31)-x['Date']
return diff.days
if(( x['Year']== 2011)and(x['Date']< datetime.datetime(2011, 12, 30))):
diff=datetime.datetime(2011, 12, 30)-x['Date']
return diff.days
else:
return 0<feature_engineering>
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
def days_from_christmas_for_test(x):
if x['Year']== 2010 :
diff=datetime.datetime(2010, 12, 31)-x['Date']
return diff.days
if(( x['Year']== 2011)and(x['Date']< datetime.datetime(2011, 12, 30))):
diff=datetime.datetime(2011, 12, 30)-x['Date']
return diff.days
if(( x['Year']== 2012)and(x['Date']< datetime.datetime(2012, 12, 28))):
diff=datetime.datetime(2012, 12, 28)-x['Date']
return diff.days
if(( x['Year']== 2013)and(x['Date']< datetime.datetime(2013, 12, 27))):
diff=datetime.datetime(2013, 12, 27)-x['Date']
return diff.days
else:
return 0<categorify>
sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train = pd.read_csv(".. /input/digit-recognizer/train.csv" )
def days_from_thanksgiving_for_train(x):
if(( x['Year']== 2010)and(x['Date']< datetime.datetime(2010, 11, 26))):
diff=datetime.datetime(2010, 11, 26)-x['Date']
return diff.days
if(( x['Year']== 2011)and(x['Date']< datetime.datetime(2011, 11, 25))):
diff=datetime.datetime(2011, 11, 25)-x['Date']
return diff.days
else:
return 0<feature_engineering>
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
X_test = test / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
 X_test = X_test.values.reshape(-1,28,28,1)
 Y_train = to_categorical(Y_train, num_classes = 10 )
def days_from_thanksgiving_for_test(x):
if(( x['Year']== 2010)and(x['Date']< datetime.datetime(2010, 11, 26))):
diff=datetime.datetime(2010, 11, 26)-x['Date']
return diff.days
if(( x['Year']== 2011)and(x['Date']< datetime.datetime(2011, 11, 25))):
diff=datetime.datetime(2011, 11, 25)-x['Date']
return diff.days
if(( x['Year']== 2012)and(x['Date']< datetime.datetime(2012, 11, 23))):
diff=datetime.datetime(2012, 11, 23)-x['Date']
return diff.days
if(( x['Year']== 2013)and(x['Date']< datetime.datetime(2013, 11, 29))):
diff=datetime.datetime(2013, 11, 29)-x['Date']
return diff.days
else:
return 0<categorify>
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )
def holiday_type(x):
if(x['IsHoliday']== 1)&(x['Week']==6):
return 1
elif(x['IsHoliday']== 1)&(x['Week']==36):
return 2
elif(x['IsHoliday']== 1)&(x['Week']==47):
return 3
elif(x['IsHoliday']== 1)&(x['Week']==52):
return 4
else:
return 0<feature_engineering>
nets = 15
model = [0] *nets
for j in range(nets):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size = 3, activation='relu', input_shape =(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(128, kernel_size = 4, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )
def holiday_label(final_data):
final_data.loc[(final_data.IsHoliday==True),'IsHoliday']= 1
final_data.loc[(final_data.IsHoliday==False),'IsHoliday']= 0
return final_data<categorify>
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 history = [0] * nets
epochs = 20
for j in range(nets):
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.1)
 history[j] = model[j].fit_generator(datagen.flow(X_train2,Y_train2, batch_size=66),
epochs = epochs, steps_per_epoch = X_train2.shape[0]//66,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0)
 print("CNN {0:d}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(j+1,epochs,max(history[j].history['accuracy']),max(history[j].history['val_accuracy'])) )
def type_label(final_data):
final_data.loc[(final_data.Type=='A'),'Type']= 1
final_data.loc[(final_data.Type=='B'),'Type']= 2
final_data.loc[(final_data.Type=='C'),'Type']= 3
return final_data<categorify>
results = np.zeros(( X_test.shape[0],10))
for j in range(nets):
results = results + model[j].predict(X_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission_digit.csv",index=False )