Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<set_options>
%matplotlib inline
!pip install --upgrade 'scikit-learn>=1.0'
%matplotlib inline
!pip install --upgrade 'scikit-learn>=1.0' 
<load_from_csv>
labeled_data = pd.read_csv(".. /input/train.csv")
 print(labeled_data.shape)
 labeled_data.head()
labeled_data = pd.read_csv(".. /input/train.csv")
 print(labeled_data.shape)
 labeled_data.head()<load_from_csv>
unlabeled_data= pd.read_csv(".. /input/test.csv")
 print(unlabeled_data.shape)
 unlabeled_data.head()
unlabeled_data= pd.read_csv(".. /input/test.csv")
 print(unlabeled_data.shape)
 unlabeled_data.head()<data_type_conversions>
X_labeled_raw =(labeled_data.iloc[:,1:].values ).astype('float32')
 y_labeled_raw = labeled_data.iloc[:,0].values.astype('int32')
 X_unlabeled_raw = unlabeled_data.values.astype('float32' )
X_labeled_raw =(labeled_data.iloc[:,1:].values ).astype('float32')
 y_labeled_raw = labeled_data.iloc[:,0].values.astype('int32')
 X_unlabeled_raw = unlabeled_data.values.astype('float32' )<prepare_output>
y_labeled = y_labeled_raw
y_labeled = y_labeled_raw<split>
X_train, X_val, y_train, y_val = train_test_split(X_labeled, y_labeled, test_size=0.20, random_state=42, shuffle=True )
X_train, X_val, y_train, y_val = train_test_split(X_labeled, y_labeled, test_size=0.20, random_state=42, shuffle=True )<prepare_x_and_y>
training_data = tf.data.Dataset.from_tensor_slices(( X_train, y_train))
validation_data = tf.data.Dataset.from_tensor_slices(( X_val, y_val))
print(training_data.element_spec )
training_data = tf.data.Dataset.from_tensor_slices(( X_train, y_train))
validation_data = tf.data.Dataset.from_tensor_slices(( X_val, y_val))
print(training_data.element_spec )<import_modules>
print(device_lib.list_local_devices() )
print(device_lib.list_local_devices() )<choose_model_class>
def get_model() :
model = Sequential([
Input(shape=(28,28,1)) ,
data_augmentation,
Convolution2D(32,(5,5), activation='relu'),
BatchNormalization(axis=1),
Convolution2D(32,(5,5), activation='relu'),
MaxPooling2D() ,
Dropout(0.25),
Convolution2D(64,(3,3), activation='relu'),
BatchNormalization(axis=1),
Convolution2D(64,(3,3), activation='relu'),
MaxPooling2D() ,
Dropout(0.25),
Flatten() ,
Dense(256, activation='relu'),
Dropout(0.25),
Dense(10, activation='softmax')
 ])
 model.compile(optimizer=tf.keras.optimizers.Adam() ,
loss=tf.keras.losses.SparseCategoricalCrossentropy() ,
metrics=['accuracy'])
 return model
model = get_model()
model.summary()
def get_model() :
model = Sequential([
Input(shape=(28,28,1)) ,
data_augmentation,
Convolution2D(32,(5,5), activation='relu'),
BatchNormalization(axis=1),
Convolution2D(32,(5,5), activation='relu'),
MaxPooling2D() ,
Dropout(0.25),
Convolution2D(64,(3,3), activation='relu'),
BatchNormalization(axis=1),
Convolution2D(64,(3,3), activation='relu'),
MaxPooling2D() ,
Dropout(0.25),
Flatten() ,
Dense(256, activation='relu'),
Dropout(0.25),
Dense(10, activation='softmax')
 ])
 model.compile(optimizer=tf.keras.optimizers.Adam() ,
loss=tf.keras.losses.SparseCategoricalCrossentropy() ,
metrics=['accuracy'])
 return model
model = get_model()
model.summary()<train_model>
EPOCHS=60
BATCH=64
autotune = tf.data.AUTOTUNE
train_data_batches = training_data.shuffle(buffer_size=40000 ).batch(BATCH ).prefetch(buffer_size=autotune)
 val_data_batches = validation_data.shuffle(buffer_size=10000 ).batch(BATCH ).prefetch(buffer_size=autotune)
 history = model.fit(train_data_batches, epochs=EPOCHS,
validation_data=val_data_batches, verbose=1 )
EPOCHS=60
BATCH=64
autotune = tf.data.AUTOTUNE
train_data_batches = training_data.shuffle(buffer_size=40000 ).batch(BATCH ).prefetch(buffer_size=autotune)
 val_data_batches = validation_data.shuffle(buffer_size=10000 ).batch(BATCH ).prefetch(buffer_size=autotune)
 history = model.fit(train_data_batches, epochs=EPOCHS,
validation_data=val_data_batches, verbose=1 )<predict_on_test>
probabilities = model.predict(X_val)
 y_predicted = np.argmax(probabilities, axis=1)
 ConfusionMatrixDisplay.from_predictions(y_val, y_predicted)
<save_to_csv><EOS> 
probabilities = model.predict(X_test, verbose=0)
 predictions = np.argmax(probabilities, axis=1)
 sample_submission = pd.read_csv('.. /input/sample_submission.csv')
 sample_submission['Label'] = predictions
sample_submission.to_csv("submission.csv",index=False )