Kaggle competition: Digit Recognizer
del(train_lectures )<categorify>
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 print(train.shape)
 train.head()
print("1")
 cum = train_df.groupby('user_id')['content_type_id'].agg(['cumsum', 'cumcount'])
 train_df['user_lecture_cumsum'] = cum['cumsum'].astype('int8')
 train_df['user_lecture_lv'] =(cum['cumsum'] / cum['cumcount'] ).astype('float16')
 user_lecture_agg = train_df.groupby('user_id')['content_type_id'].agg(['sum', 'count'] )<groupby>
test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 print(test.shape)
 test.head()
print("1")
 max_timestamp_u = train_df[['user_id','timestamp']].groupby(['user_id'] ).agg(['max'] ).reset_index()
max_timestamp_u.columns = ['user_id', 'max_time_stamp']<data_type_conversions>
X_train = train.iloc[:, 1:].values.astype('float32')
 y_train = train.iloc[:, 0].values.astype('int32')
 X_test = test.values.astype('float32' )
print("1")
 train_df['lagtime'] = train_df.groupby('user_id')['timestamp'].shift()
train_df['lagtime']=train_df['timestamp']-train_df['lagtime']
train_df['lagtime'].fillna(0, inplace=True)
 train_df.lagtime=train_df.lagtime.astype('int16')
<data_type_conversions>
%matplotlib inline
print("1")
 lagtime_agg = train_df.groupby('user_id')['lagtime'].agg(['mean'])
 train_df['lagtime_mean'] = train_df['user_id'].map(lagtime_agg['mean'])
 train_df.lagtime_mean=train_df.lagtime_mean.astype('float16' )<groupby>
mean_px = X_train.mean().astype(np.float32)
 std_px = X_train.std().astype(np.float32)
 print(mean_px, std_px)
 def standardize(x):
return(x - mean_px)/ std_px
print("1")
 user_prior_question_elapsed_time = train_df[['user_id','prior_question_elapsed_time']].groupby(['user_id'] ).tail(1)
 user_prior_question_elapsed_time.columns = ['user_id', 'prior_question_elapsed_time']<data_type_conversions>
y_train = to_categorical(y_train)
 y_train.shape
print("1")
 train_df['delta_prior_question_elapsed_time'] = train_df.groupby('user_id')['prior_question_elapsed_time'].shift()
train_df['delta_prior_question_elapsed_time'].fillna(0, inplace=True)
 train_df['delta_prior_question_elapsed_time']=(train_df['prior_question_elapsed_time']-train_df['delta_prior_question_elapsed_time'] ).astype('int16')
<data_type_conversions>
num_classes = y_train.shape[1]
num_classes
train_df['timestamp']=(train_df['timestamp']/(1000*3600)).astype('int16')
<data_type_conversions>
seed = 43
np.random.seed(seed )
train_df['lag'] = train_df.groupby('user_id')[target].shift()
cum = train_df.groupby('user_id')['lag'].agg(['cumsum', 'cumcount'])
 cum.fillna(0,inplace=True)
 train_df['user_correctness'] =(cum['cumsum'] / cum['cumcount'] ).astype('float16')
 train_df['user_correct_cumsum'] = cum['cumsum'].astype('int16')
 train_df['user_correct_cumcount'] = cum['cumcount'].astype('int16')
 train_df.drop(columns=['lag'], inplace=True)
 train_df['user_correct_cumsum'].fillna(0, inplace=True)
<data_type_conversions>
from keras.models import Sequential
from keras.layers.core import Lambda, Dense, Flatten, Dropout
from keras.callbacks import EarlyStopping
from keras.layers import BatchNormalization, Convolution2D, MaxPooling2D
train_df.prior_question_had_explanation=train_df.prior_question_had_explanation.astype('int8')
 train_df['lag'] = train_df.groupby('user_id')['prior_question_had_explanation'].shift()<data_type_conversions>
model = Sequential()
model.add(Lambda(standardize, input_shape=(28, 28, 1)))
 model.add(Flatten())
 model.add(Dense(10, activation='softmax'))
cum = train_df.groupby('user_id')['lag'].agg(['cumsum', 'cumcount'])
 cum.fillna(0,inplace=True)
 train_df['explanation_mean'] =(cum['cumsum'] / cum['cumcount'] ).astype('float16')
 train_df['explanation_cumsum'] = cum['cumsum'].astype('int16')
 train_df.drop(columns=['lag'], inplace=True)
 train_df['explanation_mean'].fillna(0, inplace=True)
 train_df['explanation_cumsum'].fillna(0, inplace=True)
<set_options>
model.compile(optimizer=RMSprop(lr=0.001),
loss='categorical_crossentropy',
metrics=['accuracy'] )
del cum
gc.collect()<data_type_conversions>
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42)
 batches = gen.flow(X_train, y_train, batch_size=64)
 val_batches = gen.flow(X_val, y_val, batch_size=64 )
train_df["attempt_no"] = 1
train_df.attempt_no=train_df.attempt_no.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>
print(len(batches))
print(batches.n // 64 )
explanation_agg = train_df.groupby('user_id')['prior_question_had_explanation'].agg(['sum', 'count'])
 explanation_agg=explanation_agg.astype('int16')
<groupby>
history = model.fit_generator(generator=batches,
steps_per_epoch=len(batches),
epochs=30,
validation_data=val_batches,
validation_steps=len(val_batches))
user_agg = train_df.groupby('user_id')[target].agg(['sum', 'count'])
 task_container_agg = train_df.groupby('task_container_id')[target].agg(['sum', 'count','var'])
<data_type_conversions>
loss = history_dict['loss']
val_loss = history_dict['val_loss']
epochs = range(1, len(loss)+ 1 )
user_agg=user_agg.astype('int16')
 content_agg=content_agg.astype('float32')
 task_container_agg=task_container_agg.astype('float32' )<categorify>
def get_fc_model() :
model = Sequential([
Lambda(standardize, input_shape=(28, 28, 1)) ,
Flatten() ,
Dense(512, activation='relu'),
Dense(10, activation='softmax')
 ])
 model.compile(optimizer='Adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
 return model
attempt_no_agg=train_df.groupby(["user_id","content_id"])["attempt_no"].agg(['sum'])
 attempt_no_agg=attempt_no_agg.astype('int8')
<data_type_conversions>
fc = get_fc_model()
fc.optimizer.lr
train_df['content_count'] = train_df['content_id'].map(content_agg['count'] ).astype('int32')
 train_df['content_sum'] = train_df['content_id'].map(content_agg['sum'] ).astype('int32')
 train_df['content_correctness'] = train_df['content_id'].map(content_agg['sum'] / content_agg['count'] ).astype("float16")
 train_df.content_correctness=train_df.content_correctness.astype('float16')
 train_df['task_container_sum'] = train_df['task_container_id'].map(task_container_agg['sum'] ).astype('int32')
 train_df['task_container_std'] = train_df['task_container_id'].map(task_container_agg['var'] ).astype('float16')
 train_df['task_container_correctness'] = train_df['task_container_id'].map(task_container_agg['sum'] / task_container_agg['count'])
 train_df.task_container_correctness=train_df.task_container_correctness.astype('float16' )<data_type_conversions>
history = fc.fit_generator(generator=batches,
steps_per_epoch=len(batches),
epochs=30,
validation_data=val_batches,
validation_steps=len(val_batches))
questions_df['content_correctness'] = questions_df['content_id'].map(content_agg['sum'] / content_agg['count'])
 questions_df.content_correctness=questions_df.content_correctness.astype('float16')
 questions_df['content_correctness_std'] = questions_df['content_id'].map(content_agg['var'])
 questions_df.content_correctness_std=questions_df.content_correctness_std.astype('float16' )<categorify>
def get_cnn_model() :
model = Sequential([
Lambda(standardize, input_shape=(28, 28, 1)) ,
Convolution2D(32,(3, 3), activation='relu'),
Convolution2D(32,(3, 3), activation='relu'),
MaxPooling2D() ,
Convolution2D(64,(3, 3), activation='relu'),
Convolution2D(64,(3, 3), activation='relu'),
MaxPooling2D() ,
Flatten() ,
Dense(512, activation='relu'),
Dense(10, activation='softmax')
 ])
 model.compile(Adam() ,
loss='categorical_crossentropy',
metrics=['accuracy'])
 return model
part_agg = questions_df.groupby('part')['content_correctness'].agg(['mean', 'var'])
 questions_df['part_correctness_mean'] = questions_df['part'].map(part_agg['mean'])
 questions_df['part_correctness_std'] = questions_df['part'].map(part_agg['var'])
 questions_df.part_correctness_mean=questions_df.part_correctness_mean.astype('float16')
 questions_df.part_correctness_std=questions_df.part_correctness_std.astype('float16' )<categorify>
model = get_cnn_model()
bundle_agg = questions_df.groupby('bundle_id')['content_correctness'].agg(['mean'])
 questions_df['bundle_correctness'] = questions_df['bundle_id'].map(bundle_agg['mean'])
 questions_df.bundle_correctness=questions_df.bundle_correctness.astype('float16' )<categorify>
history = model.fit_generator(generator=batches,
steps_per_epoch=len(batches),
epochs=30,
validation_data=val_batches,
validation_steps=len(val_batches))
tags1_agg = questions_df.groupby('tags1')['content_correctness'].agg(['mean', 'var'])
 questions_df['tags1_correctness_mean'] = questions_df['tags1'].map(tags1_agg['mean'])
 questions_df['tags1_correctness_std'] = questions_df['tags1'].map(tags1_agg['var'])
 questions_df.tags1_correctness_mean=questions_df.tags1_correctness_mean.astype('float16')
 questions_df.tags1_correctness_std=questions_df.tags1_correctness_std.astype('float16' )<drop_column>
gen1 = ImageDataGenerator()
gen2 = ImageDataGenerator(
 rotation_range=8,
width_shift_range=0.08,
shear_range=0.3,
height_shift_range=0.08,
zoom_range=0.08 
)
 batches = gen2.flow(X_train, y_train, batch_size=64)
 val_batches = gen1.flow(X_val, y_val, batch_size=64 )
questions_df.drop(columns=['content_correctness'], inplace=True )<drop_column>
model = get_cnn_model()
history = model.fit_generator(generator=batches,
steps_per_epoch=len(batches),
epochs=30,
validation_data=val_batches,
validation_steps=len(val_batches))
del bundle_agg
del part_agg
del tags1_agg
gc.collect()<train_model>
def get_bn_model() :
model = Sequential([
Lambda(standardize, input_shape=(28, 28, 1)) ,
Convolution2D(32,(3, 3), activation='relu'),
BatchNormalization(axis=3),
Convolution2D(32,(3, 3), activation='relu'),
MaxPooling2D() ,
BatchNormalization(axis=3),
Convolution2D(64,(3, 3), activation='relu'),
BatchNormalization(axis=3),
Convolution2D(64,(3, 3), activation='relu'),
MaxPooling2D() ,
Flatten() ,
BatchNormalization() ,
Dense(512, activation='relu'),
BatchNormalization() ,
Dense(10, activation='softmax')
 ])
 model.compile(Adam() ,
loss='categorical_crossentropy',
metrics=['accuracy'])
 return model
train_df['user_correctness'].fillna(1, inplace=True)
 train_df['attempt_no'].fillna(1, inplace=True)
 train_df.fillna(0, inplace=True )<categorify>
model = get_bn_model()
history = model.fit_generator(generator=batches,
steps_per_epoch=len(batches),
epochs=30,
validation_data=val_batches,
validation_steps=len(val_batches))
<define_variables><EOS> 
predictions = model.predict_classes(X_test, verbose=0)
 submissions = pd.DataFrame({'ImageID': list(range(1, len(predictions)+ 1)) ,
'Label': predictions})
 submissions.to_csv('cnn_batchnorm.csv', index=False, header=True )