Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<concatenate>
np.random.seed(2)
 random_seed = 2
all_features = pd.concat(( train_data.iloc[:,1:-1], test_data.iloc[:,1:])) 
<filter>
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 print(train.values.shape)
 print(test.values.shape )
all_features.dtypes[all_features.dtypes == 'int64']<filter>
y_train = train['label']
y_train = to_categorical(y_train, num_classes = 10)
 X_train = train.drop('label', axis=1)
 del train
print(X_train.values.shape)
 print(y_train.shape )
numeric_features = all_features.dtypes[all_features.dtypes != 'object'].index
all_features[numeric_features]<compute_test_metric>
X_train = X_train / 255.0
test = test / 255.0
def normalise(x):
return(( x - x.mean())/x.std() )<feature_engineering>
train_size = int (.9 * X_train.shape[0])
 X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size =.1, random_state=random_seed)
 print(X_train.shape)
 print(X_val.shape )
all_features[numeric_features] = all_features[numeric_features].apply(
 lambda x:(x - x.mean())/(x.std())) 
<data_type_conversions>
datagen = keras.preprocessing.image.ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )
all_features[numeric_features] = all_features[numeric_features].fillna(0 )<categorify>
model = keras.models.Sequential([
keras.layers.InputLayer(input_shape=[28,28,1]),
keras.layers.Conv2D(filters=32, kernel_size=[5,5], padding='Same', activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=32, kernel_size=[5,5], padding='Same', activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2]),
keras.layers.Dropout (.25),
keras.layers.Conv2D(filters=64, kernel_size=[3,3], padding='Same', activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=64, kernel_size=[3,3], padding='Same', activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2], strides=[2,2]),
keras.layers.Dropout (.25),
keras.layers.Conv2D(filters = 128, kernel_size =(3,3), padding = 'Same', activation ='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Dropout(0.25),
keras.layers.Flatten() ,
keras.layers.Dense(units=256, activation='relu'),
keras.layers.BatchNormalization() ,
keras.layers.Dropout (.3),
keras.layers.Dense(units=10, activation='softmax'),
])
 optimizer = keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
all_features = pd.get_dummies(all_features, dummy_na=True)
 all_features 
<data_type_conversions>
LR_callback = keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', patience=3, verbose=4, factor=.5, min_lr=.00001)
 EarlyStop_callback = keras.callbacks.EarlyStopping(patience=20 )
n_train = len(train_data)
 train_features = torch.tensor(all_features[:n_train].values, dtype=torch.float32)
 test_features = torch.tensor(all_features[n_train:].values, dtype=torch.float32)
 train_labels = torch.tensor(train_data.iloc[:,-1].values, dtype=torch.float32)
<choose_model_class>
batch_size = 128
history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size),
epochs = 50, validation_data =(X_val,y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[LR_callback, EarlyStop_callback] )
loss = nn.MSELoss()
in_features = train_features.shape[1]
out_features = 1
def get_net() :
net = nn.Sequential(nn.Linear(in_features, out_features))
return net<compute_test_metric>
model.evaluate(X_val, y_val )
<create_dataframe><EOS> 
y_pred = model.predict(test)
 y_pred = np.argmax(y_pred, axis = 1)
 results = pd.Series(y_pred,name="Label")
 Id = pd.Series(range(1,28001),name = "ImageId")
 submission = pd.concat([Id,results],axis = 1)
 submission.to_csv("MySubmission.csv",index=False )