Kaggle competition: Digit Recognizer
df.workingday.value_counts() 
<count_values>
x_train = pd.read_csv('.. /input/train.csv')
 x_train.head()
df.weather.value_counts() 
<categorify>
dim_x = 28
dim_y = 28
batch_size=32
x_train.shape
y_train = np.array(x_train['label'])
 x_train.drop('label', axis = 1, inplace = True)
 x_train = np.array(x_train.values)
 print("data shapes", x_train.shape, y_train.shape, "classes: ",len(np.unique(y_train)))
 classes = len(np.unique(y_train))
x_train = x_train.reshape(( -1, dim_x,dim_y,1))
print(np.unique(y_train))
y = np.zeros(( np.shape(y_train)[0],len(np.unique(y_train))))
for ii in range(len(y_train)) :
y[ii,y_train[ii]] = 1
y_train = y
season=pd.get_dummies(df['season'],prefix='season')
 df=pd.concat([df,season],axis=1)
 df.head()
season=pd.get_dummies(test_df['season'],prefix='season')
 test_df=pd.concat([test_df,season],axis=1)
 test_df.head()<categorify>
no_validation = int(0.1 *(x_train.shape[0]))
x_val = x_train[0:no_validation,...]
y_val = y_train[0:no_validation,...]
x_train = x_train[no_validation:,...]
y_train = y_train[no_validation:,...]
print(x_train.shape, y_train.shape, x_val.shape, y_val.shape)
 train_datagen = ImageDataGenerator(rescale = 1./255,\
rotation_range=30,\
width_shift_range=0.025,\
height_shift_range=0.025,\
shear_range=0.35,\
zoom_range=0.075)
 train_generator = train_datagen.flow(x=x_train,\
y=y_train,\
batch_size=batch_size,\
shuffle=True)
 test_datagen = ImageDataGenerator(rescale=1./255)
 val_generator = test_datagen.flow(x=x_val,\
y=y_val,\
batch_size=batch_size,\
shuffle=True )
weather=pd.get_dummies(df['weather'],prefix='weather')
 df=pd.concat([df,weather],axis=1)
 df.head()
weather=pd.get_dummies(test_df['weather'],prefix='weather')
 test_df=pd.concat([test_df,weather],axis=1)
 test_df.head()<drop_column>
model = Sequential()
model.add(Conv2D(filters=96, kernel_size=(5,5), strides=1,input_shape=(dim_x,dim_y,1), activation=tf.nn.relu))
model.add(MaxPooling2D(pool_size=2, strides=2))
model.add(Conv2D(filters=256, kernel_size=(5,5), strides=1, activation=tf.nn.relu))
model.add(Conv2D(filters=384, kernel_size=(3,3), strides=1, activation=tf.nn.relu))
model.add(Conv2D(filters=384, kernel_size=(3,3), strides=1, activation=tf.nn.relu))
model.add(SpatialDropout2D(rate=0.67))
model.add(Conv2D(filters=250, kernel_size=(3,3), strides=1, activation=tf.nn.relu))
model.add(MaxPooling2D(pool_size=2, strides=2))
model.add(Flatten())
 model.add(Reshape(( 250,1)))
 model.add(AveragePooling1D(pool_size=25,strides=25))
model.add(Reshape(( [10])))
 model.add(Activation(tf.nn.softmax))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-4), metrics=['accuracy'] )
df.drop(['season','weather'],inplace=True,axis=1)
 df.head()
test_df.drop(['season','weather'],inplace=True,axis=1)
 test_df.head()<feature_engineering>
def learning_schedule(epoch):
if epoch <= 1:
lr = 3e-4
elif epoch <= 10:
lr = 1e-5
elif epoch <= 50:
lr = 3e-6
elif epoch <= 150:
lr = 1e-6
else:
lr = 1e-8
return lr
lrate = LearningRateScheduler(learning_schedule)
 early = EarlyStopping(monitor='val_acc', min_delta=0, patience=600, verbose=1, mode='auto' )
df["hour"] = [t.hour for t in pd.DatetimeIndex(df.datetime)]
df["day"] = [t.dayofweek for t in pd.DatetimeIndex(df.datetime)]
df["month"] = [t.month for t in pd.DatetimeIndex(df.datetime)]
df['year'] = [t.year for t in pd.DatetimeIndex(df.datetime)]
df['year'] = df['year'].map({2011:0, 2012:1})
 df.head()
test_df["hour"] = [t.hour for t in pd.DatetimeIndex(test_df.datetime)]
test_df["day"] = [t.dayofweek for t in pd.DatetimeIndex(test_df.datetime)]
test_df["month"] = [t.month for t in pd.DatetimeIndex(test_df.datetime)]
test_df['year'] = [t.year for t in pd.DatetimeIndex(test_df.datetime)]
test_df['year'] = test_df['year'].map({2011:0, 2012:1})
 test_df.head()<drop_column>
steps_per_epoch = int(len(y_train)/batch_size)
 max_epochs = 4096
history = model.fit_generator(generator=train_generator,\
steps_per_epoch=steps_per_epoch,\
validation_data=val_generator,\
validation_steps=50,\
epochs=max_epochs,\
callbacks=[early, lrate],\
verbose=2 )
df.drop('datetime',axis=1,inplace=True)
 df.head()<drop_column>
x_test = pd.read_csv('.. /input/test.csv')
 x_test.head()
x_test = np.array(x_test.values)
 x_test = x_test / 255.
 print("data shape", x_test.shape)
 x_test = x_test.reshape(( -1, dim_x,dim_y,1))
df.drop(['casual','registered'],axis=1,inplace=True )<feature_engineering>
y_pred = model.predict(x_test )
df.drop(['month'],inplace=True,axis=1)
 test_df.drop(['month'],inplace=True,axis=1)
 df['holiday'] = df['holiday']
df['workingday'] = df['workingday']<filter>
results = np.argmax(y_pred,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )