Kaggle competition: Digit Recognizer
train_df=pd.read_csv("/kaggle/input/titanic/train.csv")
 test_df = pd.read_csv("/kaggle/input/titanic/test.csv" )<concatenate>
import tensorflow as tf
import cv2 as cv
import matplotlib.pyplot as plt
from tensorflow.keras import models, layers
train_len = len(train_df)
 combined = train_df.append(test_df,ignore_index=True)
 combined.fillna(np.nan )<count_missing_values>
train_data = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_data = pd.read_csv('.. /input/digit-recognizer/test.csv' )
combined.isnull().sum()<groupby>
model = models.Sequential()
model.add(layers.Conv2D(filters=32, kernel_size=(4, 4), strides=(1,1), padding = 'same', activation='relu', input_shape = data_shape))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.Conv2D(filters=32, kernel_size=(4, 4), strides=(1,1), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.35))
model.add(layers.Conv2D(filters=64, kernel_size=(3, 3), strides=(1,1), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.Conv2D(filters=64, kernel_size=(3, 3), strides=(1,1), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.35))
model.add(layers.Conv2D(filters=64, kernel_size=(2, 2), strides=(1,1), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.Conv2D(filters=64, kernel_size=(2, 2), strides=(1,1), padding = 'same', activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU(0.2))
model.add(layers.Dropout(0.35))
model.add(layers.Flatten())
 model.add(layers.Dense(units=256, activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.35))
model.add(layers.Dense(units=256, activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.35))
model.add(layers.Dense(units=10, activation='softmax'))
model.summary()
combined.groupby(['Pclass','Sex'])['Age'].mean()<feature_engineering>
combined['AgeGroup'] = 'adult'
combined.loc[combined['Name'].str.contains('Master'),'AgeGroup'] = "child"
combined.loc[combined['Age'] <= 14.0,'AgeGroup'] = "child"
combined.loc[(combined['Age'].isnull())&(combined['Name'].str.contains('Miss')) &(combined['Parch'] != 0),'AgeGroup'] = "child"<groupby>
combined[combined['Age'].notnull() ].groupby(['Pclass','Sex','AgeGroup'])['Age'].mean()<categorify>
def Age(cols):
Age=cols[0]
Pclass=cols[1]
Sex=cols[2]
AgeGroup=cols[3]
if pd.isnull(Age):
if Pclass==1:
if Sex=="male":
if AgeGroup=='adult':
return 42
else:
return 7
elif Sex=="female":
if AgeGroup=='adult':
return 37
else:
return 8
elif Pclass==2:
if Sex=="male":
if AgeGroup=='adult':
return 33
else:
return 4
elif Sex=="female":
if AgeGroup=='adult':
return 31
else:
return 7
elif Pclass==3:
if Sex=="male":
if AgeGroup=='adult':
return 29
else:
return 7
elif Sex=="female":
if AgeGroup=='adult':
return 27
else:
return 5
else:
return Age
combined["Age"]=combined[["Age","Pclass","Sex","AgeGroup"]].apply(Age,axis=1 )<categorify>
def AgeBand(col):
Age=col[0]
if Age <=7:
return "0-7"
elif Age <=14:
return "8-14"
elif Age <=21:
return "15-21"
elif Age <= 28:
return "22-28"
elif Age <= 35:
return "29-35"
elif Age <= 42:
return "36-42"
elif Age <= 49:
return "43-49"
elif Age <= 56:
return "50-56"
elif Age <= 63:
return "57-63"
else:
return ">=64"
combined["AgeBand"]=combined[["Age"]].apply(AgeBand,axis=1 )<groupby>
combined.groupby(['Pclass','Embarked'])['PassengerId'].count()<feature_engineering>
combined[combined['Embarked'].isnull() ]['Embarked'] = combined['Embarked'].mode()<merge>
epochs_num = 40
batch_size = 128
validation_split_part = 0.2
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 history = model.fit(train_image, train_label, epochs=epochs_num, batch_size=batch_size, validation_split=validation_split_part, shuffle=True )
ticketCount = combined.groupby('Ticket')['PassengerId'].count().reset_index()
ticketCount.rename(columns={'PassengerId':'Count on Ticket'},inplace=True)
 combined = combined.merge(ticketCount, on="Ticket",how="left" )<feature_engineering>
model.save('Digit_Recognition_CNN.model' )
combined['Diff'] = combined['FamilySize'] - combined['Count on Ticket']
combined['Family Status'] = combined.apply(lambda x:"Has Family On Same Ticket" if(x['FamilySize'] - x['Count on Ticket'])<= 0 else "Family Not on same ticket",axis=1)
<feature_engineering>
predictions = model.predict(test_image )
combined['Family Status'] = combined.apply(lambda x:"Is Alone" if(x['FamilySize']==1)&(x['Count on Ticket']==1)else x['Family Status'],axis=1 )<feature_engineering>
predictions = np.argmax(predictions, axis = 1 )
combined['Cabin Class'] = 'No Cabin'
combined['Cabin Class'] = combined.apply(lambda x: "No Cabin" if pd.isna(x["Cabin"])else x["Cabin"][0] , axis=1)
<merge>
submission = pd.DataFrame({'ImageId' : range(1,28001), 'Label' : predictions} )
<feature_engineering><EOS> 
submission.to_csv("submission.csv",index=False )