Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<define_variables>
warnings.filterwarnings('ignore')
 %matplotlib inline
input_path = Path('/kaggle/input/tabular-playground-series-jan-2021/' )<load_from_csv>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv' )
train = pd.read_csv(input_path / 'train.csv', index_col='id')
 display(train.head() )<load_from_csv>
X_train = train.drop(['label'], axis=1)
 y_train = train['label']
test = pd.read_csv(input_path / 'test.csv', index_col='id')
 display(test.head() )<drop_column>
X_train /= 255.0
test /= 255.0
target = train.pop('target' )<split>
X_train1 = X_train.values.reshape(-1, 28, 28, 1)
 test = test.values.reshape(-1, 28, 28, 1 )
X_train, X_test, y_train, y_test = train_test_split(train, target, train_size=0.80 )<import_modules>
y_train = to_categorical(y_train, num_classes=10 )
from sklearn.dummy import DummyRegressor
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_regression
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import Ridge
from sklearn.linear_model import SGDRegressor
from sklearn.linear_model import BayesianRidge
from sklearn.linear_model import LassoLars
from sklearn.linear_model import ARDRegression
from sklearn.linear_model import PassiveAggressiveRegressor
from sklearn.linear_model import TheilSenRegressor
from sklearn.linear_model import LinearRegression
from lightgbm import LGBMRegressor
from xgboost import XGBRegressor 
<compute_train_metric>
X_train, X_val , y_train, y_val = train_test_split(X_train1, y_train, test_size=0.2 )
def FitAndScoreModel(df,name, model,X_tr,y_tr,X_tst,y_tst):
model.fit(X_tr,y_tr)
 Y_pred = model.predict(X_tst)
 score=mean_squared_error(y_tst, Y_pred, squared=False)
 df = df.append({'Model':name, 'MSE': score},ignore_index = True)
 return df<create_dataframe>
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(filters = 64, kernel_size=(5,5), padding='same', activation='relu', input_shape=(28, 28, 1)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.Conv2D(filters = 64, kernel_size=(5,5), padding='same', activation='relu'))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.MaxPool2D(pool_size=(2,2)))
 model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Conv2D(filters = 64, kernel_size=(3,3), padding='same', activation='relu'))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.Conv2D(filters = 64, kernel_size=(3,3), padding='same', activation='relu'))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.MaxPool2D(pool_size=(2,2)))
 model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Flatten())
 model.add(keras.layers.Dense(256, activation='relu'))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(256, activation='relu'))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(10, activation='softmax'))
dResults = pd.DataFrame(columns = ['Model', 'MSE'] )<train_model>
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["accuracy"] )
classifiers = [
DummyRegressor(strategy='median'),
SGDRegressor() ,
BayesianRidge() ,
LassoLars() ,
ARDRegression() ,
PassiveAggressiveRegressor() ,
LinearRegression() ,
LGBMRegressor() ,
RandomForestRegressor() ,
XGBRegressor() ]
for item in classifiers:
print(item)
 clf = item
dResults=FitAndScoreModel(dResults,item,item,X_train,y_train,X_test,y_test )<sort_values>
history = model.fit(X_train, y_train, epochs=25, validation_data=(X_val, y_val))
dResults.sort_values(by='MSE', ascending=True,inplace=True)
 dResults.set_index('MSE',inplace=True)
 dResults.head(dResults.shape[0] )<init_hyperparams>
y_pred = model.predict(test )
<init_hyperparams><EOS> 
submission['Label'] = results
submission.to_csv('submission.csv', index=False )