Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<merge>
%matplotlib inline
companion = pd.pivot_table(combined, values='PassengerId',index=['Ticket'],columns=['AgeGroup'], aggfunc="count" ).reset_index().fillna(0)
 companion.columns = ['Ticket','No.of Adult Companion', 'No.of Child Companion']
combined = combined.merge(companion, on='Ticket',how='left' )<feature_engineering>
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 train_df.head()
combined.loc[combined['AgeGroup']=='adult','No.of Adult Companion'] = combined.loc[combined['AgeGroup']=='adult','No.of Adult Companion'] - 1
combined.loc[combined['AgeGroup']=='child','No.of Child Companion'] = combined.loc[combined['AgeGroup']=='child','No.of Child Companion'] - 1
combined['Companion'] = 'Adult & Child Companion'
combined['Companion'] = combined.apply(lambda x:'Only Adult Companion' if(x['No.of Adult Companion'] > 0)&(x['No.of Child Companion']==0)else x['Companion'],axis=1)
 combined['Companion'] = combined.apply(lambda x:'Only Child Companion' if(x['No.of Adult Companion'] == 0)&(x['No.of Child Companion']>0)else x['Companion'],axis=1)
 combined['Companion'] = combined.apply(lambda x:'No Companion' if(x['No.of Adult Companion'] == 0)&(x['No.of Child Companion']==0)else x['Companion'],axis=1 )<groupby>
X = train_df.drop('label', axis=1 ).values.reshape(( -1, 28, 28, 1)) / 255
y = utils.to_categorical(train_df['label'], 10)
 print(X.shape, y.shape )
combined[combined["Survived"].notnull() ].groupby(['AgeGroup','Companion'])['PassengerId'].count()<groupby>
train_datagen = ImageDataGenerator(
 rotation_range=8,
shear_range=0.3,
zoom_range=0.08,
width_shift_range=0.08,
height_shift_range=0.08,
validation_split=0.1)
 train_datagen.fit(X )
combined[combined["Survived"].notnull() ].groupby(['AgeGroup','Companion'])['Survived'].mean()<drop_column>
keras.backend.clear_session()
model = keras.Sequential(
 [
keras.Input(shape=(28, 28, 1)) ,
layers.Conv2D(32, kernel_size=5, padding='same', kernel_regularizer=regularizers.l2(5e-4), activation='relu'),
layers.BatchNormalization() ,
layers.Conv2D(32, kernel_size=5, padding='same', kernel_regularizer=regularizers.l2(5e-4), activation='relu'),
layers.BatchNormalization() ,
layers.MaxPool2D(pool_size=(2,2)) ,
layers.Conv2D(64, kernel_size=3, kernel_regularizer=regularizers.l2(5e-4), activation='relu'),
layers.BatchNormalization() ,
layers.Conv2D(64, kernel_size=3, kernel_regularizer=regularizers.l2(5e-4), activation='relu'),
layers.BatchNormalization() ,
layers.MaxPool2D(pool_size=(2,2), strides=(2,2)) ,
layers.Conv2D(128, kernel_size=3, kernel_regularizer=regularizers.l2(5e-4), activation='relu'),
layers.BatchNormalization() ,
layers.Flatten() ,
layers.Dense(256, activation='relu'),
layers.BatchNormalization() ,
layers.Dense(10, activation='softmax'),
] 
)
 model.summary()
train_copy = combined[:train_len]
test_copy = combined[train_len:].reset_index(drop=True)
 test_copy.drop(columns=['Survived'],inplace=True )<drop_column>
stopping = keras.callbacks.EarlyStopping(patience=2, monitor ='val_accuracy')
 optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-07)
 callbacks = [stopping]
combined.drop(columns=['PassengerId','Name','Age', 'AgeGroup','SibSp','Parch','Ticket','Cabin','Count on Ticket','Diff','No.of Adult Companion','No.of Child Companion'],inplace=True )<categorify>
batch_size = 84
epochs = 100
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc'])
 history = model.fit(
 train_datagen.flow(X, y, subset='training'),
validation_data=train_datagen.flow(X, y, batch_size=batch_size, subset='validation'),
epochs=epochs,
steps_per_epoch = X.shape[0] // batch_size,
batch_size=batch_size,
callbacks=callbacks,
use_multiprocessing=True )
combined = pd.get_dummies(combined, columns = ["Sex","Embarked","AgeBand","Family Status","Cabin Class","Companion"],drop_first=True )<drop_column>
submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
train = combined[:train_len]
test = combined[train_len:]
test.drop(columns=['Survived'],inplace=True )<split>
X_test = test_df.values.reshape(( -1, 28, 28, 1)) / 255
y_hat = model.predict(X_test)
 y_class = y_hat.argmax(axis=-1)
 submission['Label'] = y_class
<compute_train_metric><EOS> 
submission.to_csv('output.csv', index=False)
 display(submission.tail() )