Kaggle competition: Digit Recognizer
print('start handle attempt_no...' )<data_type_conversions>
train=pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test=pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 train.head()
train_df["attempt_no"] = 1
train_df.attempt_no=train_df.attempt_no.astype('int8')
 attempt_no_agg=train_df.groupby(["user_id","content_id"])["attempt_no"].agg(['sum'] ).astype('int8')
 train_df["attempt_no"] = train_df[["user_id","content_id",'attempt_no']].groupby(["user_id","content_id"])["attempt_no"].cumsum()<data_type_conversions>
X_train=train.drop("label", axis=1)
 y_train=train["label"]
X_test=test
print('start handle timestamp...')
 prior_question_elapsed_time_mean=train_df['prior_question_elapsed_time'].mean()
train_df['prior_question_elapsed_time'].fillna(prior_question_elapsed_time_mean, inplace=True )<data_type_conversions>
X_train/=255.0
X_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 )
max_timestamp_u = train_df[['user_id','timestamp']].groupby(['user_id'] ).agg(['max'] ).reset_index()
max_timestamp_u.columns = ['user_id', 'max_time_stamp']
max_timestamp_u.user_id=max_timestamp_u.user_id.astype('int32' )<data_type_conversions>
X_train, x_test, Y_train, y_test= train_test_split(X_train,y_train,test_size=0.1,random_state=0 )
train_df['lagtime'] = train_df.groupby('user_id')['timestamp'].shift()
max_timestamp_u2 = train_df[['user_id','lagtime']].groupby(['user_id'] ).agg(['max'] ).reset_index()
max_timestamp_u2.columns = ['user_id', 'max_time_stamp2']
max_timestamp_u2.user_id=max_timestamp_u2.user_id.astype('int32' )<feature_engineering>
classifier=Sequential()
classifier.add(Conv2D(64,3,3, input_shape=(28,28,1), activation='relu'))
classifier.add(BatchNormalization())
 classifier.add(Conv2D(64,3,3, activation='relu'))
classifier.add(BatchNormalization())
 classifier.add(Conv2D(64,3,3, activation='relu'))
classifier.add(BatchNormalization())
 classifier.add(MaxPooling2D(pool_size=(2,2)))
 classifier.add(Dropout(0.4))
classifier.add(Conv2D(64,3,3, activation='relu'))
classifier.add(BatchNormalization())
 classifier.add(Conv2D(64,3,3, activation='relu'))
classifier.add(BatchNormalization())
 classifier.add(Conv2D(64,3,3, activation='relu'))
classifier.add(BatchNormalization())
 classifier.add(MaxPooling2D(pool_size=(2,2)))
 classifier.add(Dropout(0.4))
classifier.add(Flatten())
 classifier.add(Dense(output_dim=256,activation='relu'))
classifier.add(Dropout(0.4))
classifier.add(Dense(output_dim=512,activation='relu'))
classifier.add(Dropout(0.4))
classifier.add(Dense(output_dim=1024,activation='relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(output_dim=10, activation='softmax'))
train_df['lagtime']=train_df['timestamp']-train_df['lagtime']
lagtime_mean=train_df['lagtime'].mean()
train_df['lagtime'].fillna(lagtime_mean, inplace=True )<data_type_conversions>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )
train_df['lagtime']=train_df['lagtime']/(1000*3600)
 train_df.lagtime=train_df.lagtime.astype('float32' )<data_type_conversions>
classifier.compile(optimizer=optimizer, loss='categorical_crossentropy',
metrics=['accuracy'] )
<data_type_conversions>
datagen = ImageDataGenerator(zoom_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1,
rotation_range = 10 )
train_df['lagtime2'] = train_df.groupby('user_id')['timestamp'].shift(2)
 max_timestamp_u3 = train_df[['user_id','lagtime2']].groupby(['user_id'] ).agg(['max'] ).reset_index()
max_timestamp_u3.columns = ['user_id', 'max_time_stamp3']
max_timestamp_u3.user_id=max_timestamp_u3.user_id.astype('int32')
 train_df['lagtime2']=train_df['timestamp']-train_df['lagtime2']
lagtime_mean2=train_df['lagtime2'].mean()
train_df['lagtime2'].fillna(lagtime_mean2, inplace=True)
<data_type_conversions>
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x )
train_df['lagtime2']=train_df['lagtime2']/(1000*3600)
 train_df.lagtime2=train_df.lagtime2.astype('float32' )<data_type_conversions>
classifier.fit_generator(datagen.flow(X_train, Y_train, batch_size=16),
steps_per_epoch=500,
epochs=40,
verbose=2,
validation_data=(x_test[:400,:], y_test[:400,:]),
callbacks=[annealer] )
train_df['lagtime3'] = train_df.groupby('user_id')['timestamp'].shift(3)
 train_df['lagtime3']=train_df['timestamp']-train_df['lagtime3']
lagtime_mean3=train_df['lagtime3'].mean()
train_df['lagtime3'].fillna(lagtime_mean3, inplace=True)
 train_df['lagtime3']=train_df['lagtime3']/(1000*3600)
 train_df.lagtime3=train_df.lagtime3.astype('float32' )<data_type_conversions>
result=classifier.predict(X_test)
 result=pd.Series(np.argmax(result, axis=1), name='Label')
 result
<data_type_conversions>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),result],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )