<jupyter_start><jupyter_text>MNIST Classification using TensorflowTraining a Deep Learning model on handwritten digits using Tensorflow and Keras to accurately predict the test setKaggle link to the challenge - https://www.kaggle.com/c/digit-recognizer Contents  0: Importing libraries and the dataset<jupyter_code># Importing the libraries
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.models import Sequential

import warnings
warnings.filterwarnings("ignore")
# Reading the data
train = pd.read_csv("../input/digit-recognizer/train.csv")
test = pd.read_csv("../input/digit-recognizer/test.csv")
# Seperating features and label
y_train = train["label"]
X_train = train.drop(["label"], axis=1)<jupyter_output><empty_output><jupyter_text>1: Exploratory Data AnalysisPerforming EDA on the training dataset<jupyter_code># Size of the training data
X_train.shape
X_train.head(5)
# Visualize the number of digits under each class
plt.figure(figsize=(9, 5))
sns.countplot(y_train)
plt.title("Count plot");
y_train.value_counts()<jupyter_output><empty_output><jupyter_text>There seem to be an equal distribution of class labels<jupyter_code># Displaying some training samples
img_matrix = X_train.iloc[100].to_numpy()
img_matrix = img_matrix.reshape(28, 28)

plt.imshow(img_matrix, cmap=plt.cm.binary)<jupyter_output><empty_output><jupyter_text>3: Feature EngineeringFeature engineering is an essential step of data preprocessin, this helps the model converge much faster.We will be performing 3 main steps here1. Normalization - We will perform grey scale normalization to reduce the illumination difference it also helps the model train faster2. Reshape - We will drop the shape to 28x28. We will be adding an extra dimension, i.e 28x28x1 (Greyscale data). We have to add this extra dimension for keras, as 3. Label Encoding - encoding labels to one hot vectors<jupyter_code># Normalization
X_train = tf.keras.utils.normalize(X_train, axis=1)
test = tf.keras.utils.normalize(test, axis=1)
# Reshape
X_train = X_train.values.reshape(-1, 28, 28)
test = test.values.reshape(-1, 28, 28)<jupyter_output><empty_output><jupyter_text>4: Train Test SplitSplitting `X_train` into training data (90%) and test data (10%) using `sklearn.model_selection.train_test_split`<jupyter_code>X_train, X_eval, y_train, y_eval = train_test_split(X_train, y_train, test_size=0.1, random_state=42)
X_train.shape, X_eval.shape, y_train.shape, y_eval.shape<jupyter_output><empty_output><jupyter_text>5: Creating, Training and Fitting the model<jupyter_code># 1: Building the model
model = Sequential()
model.add(Flatten())
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dense(10, activation=tf.nn.sigmoid))

# 2. Compile the model
model.compile(optimizer="adam",
             loss="sparse_categorical_crossentropy",
             metrics=["accuracy"])

# 3. Train the model
history = model.fit(X_train, y_train,
         validation_data=(X_eval, y_eval),
         epochs=100)<jupyter_output>Epoch 1/100
1182/1182 [==============================] - 4s 2ms/step - loss: 0.7730 - accuracy: 0.7876 - val_loss: 0.2402 - val_accuracy: 0.9283
Epoch 2/100
1182/1182 [==============================] - 2s 2ms/step - loss: 0.1972 - accuracy: 0.9416 - val_loss: 0.1821 - val_accuracy: 0.9426
Epoch 3/100
1182/1182 [==============================] - 2s 2ms/step - loss: 0.1417 - accuracy: 0.9576 - val_loss: 0.1412 - val_accuracy: 0.9569
Epoch 4/100
1182/1182 [==============================] - 3s 2ms/step - loss: 0.1095 - accuracy: 0.9666 - val_loss: 0.1232 - val_accuracy: 0.9614
Epoch 5/100
1182/1182 [==============================] - 2s 2ms/step - loss: 0.0862 - accuracy: 0.9727 - val_loss: 0.1128 - val_accuracy: 0.9655
Epoch 6/100
1182/1182 [==============================] - 2s 2ms/step - loss: 0.0697 - accuracy: 0.9793 - val_loss: 0.1012 - val_accuracy: 0.9695
Epoch 7/100
1182/1182 [==============================] - 2s 2ms/step - loss: 0.0559 - accuracy: 0.9831 - val_loss: 0.1046 - val_ac[...]<jupyter_text>6: Visualize Train and Validation Results<jupyter_code>history_df = pd.DataFrame(history.history)
history_df.loc[:, ["loss", "accuracy"]].plot()
history_df.loc[:, ["val_loss", "val_accuracy"]].plot()
# Summary of the scores
history_df.describe()<jupyter_output><empty_output><jupyter_text>Since the validation accuracy is over ~ 97.9, the same model will be used to submit the result.  7. Submit Results<jupyter_code>results = model.predict(test)
results = np.argmax(results, axis=1)

submission = pd.DataFrame({"ImageId": [i for i in range(1, 28001)] , "Label": list(results)})

submission.to_csv("mnist_submission.csv", index=False)<jupyter_output><empty_output><jupyter_text>8. Random ValidationA random sample of the test set is taken to validate with the predictions made by the model<jupyter_code>plt.imshow(test[10].reshape(28, 28))
submission.iloc[10]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15081070521586248, 'fraction_numerical': 0.061926157452627466, 'mean_word_length': 5.614987080103359, 'pattern_counts': {'":': 2, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '403015', 'license': 'MIT', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1728, 'n_words': 457, 'path': 'mnist-digits-recognizer/deep_nn_using_tensorflow.ipynb', 'repo_name': 'tejeshreddy/kaggle-challenges', 'size': '5119'}