Kaggle competition: Digit Recognizer
n=14
commits_df.loc[n, 'n_commit'] = n+3
commits_df.loc[n, 'low'] = 0.01
commits_df.loc[n, 'high'] = 0.88
commits_df.loc[n, 'LB_score'] = 0.246
commits_df.loc[n, 'LB_private'] = 0.226<feature_engineering>
X_train = []
Y_train = []
with open('/kaggle/input/digit-recognizer/train.csv')as train_file:
for line in train_file.readlines() [1:]:
content = line.strip().split(',')
 label, image_raw_data = content[0], content[1:]
image_np = np.array_split(np.array(image_raw_data), 28)
 X_train.append(image_np)
 Y_train.append(label)
 X_train = np.array(X_train ).astype(float)
 X_train = np.expand_dims(X_train, axis=3)
 Y_train = np.array(Y_train ).astype(float)
 print(X_train.shape)
 print(Y_train.shape )
n=15
commits_df.loc[n, 'n_commit'] = n+3
commits_df.loc[n, 'low'] = 0.1
commits_df.loc[n, 'high'] = 0.88
commits_df.loc[n, 'LB_score'] = 0.246
commits_df.loc[n, 'LB_private'] = 0.226<data_type_conversions>
X_test = []
with open('/kaggle/input/digit-recognizer/test.csv')as test_file:
for line in test_file.readlines() [1:]:
content = line.strip().split(',')
 image_raw_data = content[:]
image_np = np.array_split(np.array(image_raw_data), 28)
 X_test.append(image_np)
 X_test = np.array(X_test ).astype(float)
 X_test = np.expand_dims(X_test, axis=3)
 print(X_test.shape )
commits_df['LB_score'] = pd.to_numeric(commits_df['LB_score'])
 commits_df['LB_private'] = pd.to_numeric(commits_df['LB_private'])
 commits_df = commits_df.sort_values(by=['LB_score'], ascending = False ).reset_index(drop=True)
 commits_df['max'] = 0
commits_df.loc[15, 'max'] = 1
commits_df.loc[0, 'max'] = 2
commits_df['max_private'] = 0
commits_df.loc[0, 'max_private'] = 1
commits_df.loc[15, 'max_private'] = 2<sort_values>
!wget --recursive --no-parent 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Regular.ttf' -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
!wget --recursive --no-parent 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Light.ttf' -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
!wget --recursive --no-parent 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-SemiBold.ttf' -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
!wget --recursive --no-parent 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Bold.ttf' -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
commits_df.sort_values(by=['LB_score'], ascending = True )<sort_values>
model = tf.keras.models.Sequential([
tf.keras.Input(shape=(28, 28, 1)) ,
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(32,(5,5), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(32,(5,5), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(64,(3,3), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(64,(3,3), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(128,(3,3), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(128,(3,3), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation='relu', kernel_initializer = 'he_uniform'),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(256, activation='relu', kernel_initializer = 'he_uniform'),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(128, activation='relu', kernel_initializer = 'he_uniform'),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(64, activation='relu', kernel_initializer = 'he_uniform'),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(10, activation='softmax')
 ])
 model.compile(
 optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'] 
)
 model.summary()
early_stopping_callback = tf.keras.callbacks.EarlyStopping(monitor="accuracy", mode='max', patience=25)
 model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint('./best_model.hdf5', monitor='accuracy',
mode='max', verbose=1, save_best_model=True)
 reduce_lr_callback = tf.keras.callbacks.ReduceLROnPlateau(monitor='accuracy', mode='max', patience=15,
verbose=1, factor=0.5, min_lr=0.0001)
 history = model.fit(
 X_train / 255,
tf.keras.utils.to_categorical(Y_train, num_classes=10),
epochs=1000,
shuffle=True,
callbacks=[early_stopping_callback, model_checkpoint_callback,reduce_lr_callback]
)
<feature_engineering><EOS> 
predictions = model.predict(X_test / 255)
 y_pred = np.argmax(predictions, axis=-1)
 submission_df = pd.DataFrame(data={
'ImageId': np.arange(1, X_test.shape[0] + 1),
'label': y_pred
})
 submission_df.to_csv('submission.csv', index=False)
 submission_df