Kaggle competition: Digit Recognizer
test_user=pd.read_csv("/kaggle/input/airbnb-recruiting-new-user-bookings/test_users.csv.zip")
 print(test_user.shape)
 test_user.head()<categorify>
train=pd.read_csv('.. /input/train.csv')
 test=pd.read_csv('.. /input/test.csv')
 sub=pd.read_csv('.. /input/sample_submission.csv' )
class Custom_Proccess(BaseEstimator, TransformerMixin):
def transform(self,X,y=None):
X.gender.replace('-unknown-', 'OTHER', inplace=True)
 X['age'].fillna(-1,inplace=True)
 X['timestamp_first_active']=X['timestamp_first_active'].apply(lambda s:datetime(year=int(str(s)[0:4]), month=int(str(s)[4:6]),
day=int(str(s)[6:8])).strftime('%Y-%m-%d'))
X['timestamp_first_active']=X['timestamp_first_active'].astype('datetime64[ns]')
 X['age']=X['age'].astype('int64')
 X['date_account_created']=X['date_account_created'].astype('datetime64[ns]')
 X['dac_year']=X['date_account_created'].dt.year
X['dac_month']=X['date_account_created'].dt.month
X['dac_day']=X['date_account_created'].dt.day
X['tfa_year']=X['timestamp_first_active'].dt.year
X['tfa_month']=X['timestamp_first_active'].dt.month
X['tfa_day']=X['timestamp_first_active'].dt.day
X.signup_app.replace(['iOS','Android'],'SmartDevice',inplace=True)
 X.drop(['date_first_booking','date_account_created','timestamp_first_active','first_device_type','first_browser'],axis=1,inplace=True)
 return X
def fit(self, X, y=None, **fit_params):
return self<drop_column>
x=train.drop(['label'],axis=1)
 x_test=test.copy()
session.drop(['action_detail','device_type'],inplace=True,axis=1)
 session.dropna(subset=['user_id','action'],inplace=True)
 session.action_type=session.action_type.fillna('Other')
 session.secs_elapsed=session.secs_elapsed.fillna(0 )<categorify>
x=x/255
x_test=x_test/255
session_group=session.groupby(['user_id','action_type'] ).agg({'action':'count','secs_elapsed':'sum'} ).reset_index()
session_df=pd.get_dummies(session_group,columns=['action_type'] ).groupby(['user_id'] ).sum().reset_index()
session_df.head()<merge>
y=train['label']
train_user_df=train_user.merge(session_df,left_on=['id'],
right_on=['user_id'],how='left' ).drop(['user_id'],axis=1 ).reset_index(drop=True)
 train_user_df.shape<merge>
y=pd.Categorical(y )
test_user_df=test_user.merge(session_df,left_on=['id'],
right_on=['user_id'],how='left' ).drop(['user_id'],axis=1 ).reset_index(drop=True)
 test_user_df.shape<data_type_conversions>
y=pd.get_dummies(y )
train_user_df.secs_elapsed.fillna(-1,inplace=True)
 train_user_df.action.fillna(-1,inplace=True)
 train_user_df.iloc[:,-11:]=train_user_df.iloc[:,-11:].fillna(-1)
 train_user_df['secs_elapsed']=train_user_df['secs_elapsed'].astype('int64')
 train_user_df['action']=train_user_df['action'].astype('int64' )<data_type_conversions>
x_train,x_val,y_train,y_val=train_test_split(x,y,test_size=0.1,random_state=0 )
test_user_df.secs_elapsed.fillna(-1,inplace=True)
 test_user_df.action.fillna(-1,inplace=True)
 test_user_df.iloc[:,-11:]=test_user_df.iloc[:,-11:].fillna(-1)
 test_user_df['secs_elapsed']=test_user_df['secs_elapsed'].astype('int64')
 test_user_df['action']=test_user_df['action'].astype('int64' )<count_missing_values>
x_train=x_train.values
y_train=y_train.values
x_val=x_val.values
y_val=y_val.values
x_test=x_test.values
train_user_df.isnull().sum() /train_user_df.shape[0] *100<define_variables>
x_train=x_train.reshape(37800,28,28,1)
 x_val=x_val.reshape(4200,28,28,1)
 x_test=x_test.reshape(28000,28,28,1 )
categorical_cols=[cname for cname in train_user_df.columns if cname not in ['id','date_account_created','date_first_booking','first_device_type','first_browser',
'timestamp_first_active','country_destination'] and
train_user_df[cname].dtype == "object"]
numerical_cols=[cname for cname in train_user_df.columns if cname not in ['id','date_account_created','date_first_booking','first_device_type','first_browser',
'timestamp_first_active','country_destination'] and
train_user_df[cname].dtype != "object"]
print("Categorical_cols -
",categorical_cols)
 print("Numerical_cols -
",numerical_cols )<categorify>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 numerical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('cat', categorical_transformer, categorical_cols), 
('num', numerical_transformer, numerical_cols)
 ] )<drop_column>
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta() ,
metrics=['accuracy'] )
test_id = test_user_df.id
test_X = test_user_df.drop(['id'], axis='columns' )<categorify>
model.fit(x_train, y_train,
batch_size=128,
epochs=15,
verbose=1,
validation_data=(x_val, y_val))
labels = train_user_df.country_destination
le = LabelEncoder()
train_y = le.fit_transform(labels )<drop_column>
pred=model.predict(x_test,verbose=0)
 new_pred = [np.argmax(y, axis=None, out=None)for y in pred]
output=pd.DataFrame({'ImageId':sub['ImageId'],'Label':new_pred})
 output.to_csv('Digit_recognizer.csv', index=False )