Kaggle competition: Digit Recognizer
train['datetime'] = pd.to_datetime(train['datetime'])
 test['datetime'] = pd.to_datetime(test['datetime'])
 train.head()<feature_engineering>
train_url = '/kaggle/input/digit-recognizer/train.csv'
test_url = '/kaggle/input/digit-recognizer/test.csv'
train = pd.read_csv(train_url)
 test = pd.read_csv(test_url )
def split_datetime(df):
df['year'] = df['datetime'].apply(lambda t: t.year)
 df['month'] = df['datetime'].apply(lambda t: t.month)
 df['day'] = df['datetime'].apply(lambda t: t.day)
 df['dayofweek'] = df['datetime'].apply(lambda t: t.dayofweek)
 df['hour'] = df['datetime'].apply(lambda t: t.hour)
 df = df.drop(['datetime'], axis=1)
 return df<string_transform>
X = train.drop('label', axis=1 ).astype('float32')
 y = train['label']
X_sub = test.astype('float32' )
test = split_datetime(test)
 train = split_datetime(train)
 train = train.drop(['casual', 'registered'], axis=1)
 train.head()<feature_engineering>
from sklearn.model_selection import train_test_split
import tensorflow.keras as keras
from keras import Sequential,layers,callbacks,preprocessing
train['count'] = np.log1p(train['count'] )<filter>
X = X.values.reshape(-1,28,28,1)
 X_sub = X_sub.values.reshape(-1,28,28,1)
 y = keras.utils.to_categorical(y, 10)
 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=7, shuffle=True )
train = train[np.abs(train['count']-train['count'].mean())<=(3*train['count'].std())]<drop_column>
def spatial_cnn() :
model = Sequential([
layers.experimental.preprocessing.Rescaling(scale=1.0/255.0, input_shape=(28,28,1)) ,
layers.Conv2D(32,(3,3), activation='relu', padding='same'),
layers.BatchNormalization() ,
layers.SpatialDropout2D(0.25),
layers.Conv2D(32,(3,3), activation='relu', padding='same'),
layers.BatchNormalization() ,
layers.SpatialDropout2D(0.25),
layers.Conv2D(32,(5,5),(2,2), activation='relu', padding='same'),
layers.BatchNormalization() ,
layers.SpatialDropout2D(0.25),
layers.Conv2D(64,(3,3), activation='relu', padding='same'),
layers.BatchNormalization() ,
layers.SpatialDropout2D(0.25),
layers.Conv2D(64,(3,3), activation='relu', padding='same'),
layers.BatchNormalization() ,
layers.SpatialDropout2D(0.25),
layers.Conv2D(64,(5,5),(2,2), activation='relu', padding='same'),
layers.BatchNormalization() ,
layers.SpatialDropout2D(0.25),
layers.Conv2D(128,(3,3), activation='relu', padding='same'),
layers.BatchNormalization() ,
layers.SpatialDropout2D(0.25),
layers.Flatten() ,
layers.Dense(300, activation="relu"),
layers.BatchNormalization() ,
layers.Dropout(0.4),
layers.Dense(10, activation='softmax')],
name='spatial_cnn')
 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics='accuracy')
 model.summary()
return model
model = spatial_cnn()
drop_idx = train[(train['atemp'] > 20)&(train['atemp'] < 40)&(train['temp'] > 10)&(train['temp'] < 20)].index
train = train.drop(drop_idx )<normalization>
train_datagen = preprocessing.image.ImageDataGenerator(rotation_range=10, width_shift_range=0.1,
height_shift_range=0.1, shear_range=0.1,
zoom_range=0.1)
 train_datagen.fit(X_train )
def scaling(df):
scaler = MinMaxScaler()
num_cols = ['temp', 'atemp', 'humidity', 'windspeed']
df[num_cols] = scaler.fit_transform(df[num_cols])
 return df<normalization>
batch_size = 64
epochs = 45
steps = X_train.shape[0]//batch_size
history = dict()
flow = train_datagen.flow(X_train, y_train, batch_size=batch_size, shuffle=True)
 changed = False
class cbs(callbacks.Callback):
def on_epoch_end(self, epoch, logs):
global changed
if logs['val_loss']<=0.0205 and not changed:
changed = True
print("
Changing LR
")
 global reduce_lr
reduce_lr = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.05, patience=2, min_lr=1.0e-10)
 reduce_lr = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=2, min_lr=1.0e-10)
 early_stop = callbacks.EarlyStopping(monitor="val_loss", patience=10, mode="auto")
 model_hist = model.fit_generator(flow, validation_data=(X_test, y_test), verbose=1, shuffle=True,
steps_per_epoch=steps, epochs=epochs, callbacks=[cbs() , reduce_lr, early_stop] )
train = scaling(train)
 test = scaling(test )<split>
preds_sub = pd.DataFrame(data={"ImageId":list(range(1,X_sub.shape[0]+1)) ,"Label":(model.predict_classes(X_sub)) } ).astype(int )
X_train, X_test, y_train, y_test = train_test_split(train.drop(['count'], axis=1), train['count'], test_size=0.3 )<compute_test_metric>
preds_sub.to_csv("DigitRecog.csv", index=False, header=True )