{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# MNIST Digit Classification Notebook\n**Author:**  Amanda Judy Andrade\n\nThe MNIST digit classification python project enables machines to recognize handwritten digits.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"# Importing Required Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport seaborn as sns\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport tensorflow as tf\n\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras import metrics\nfrom tensorflow.keras import backend as K","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:43:51.965367Z","iopub.execute_input":"2022-05-26T07:43:51.965838Z","iopub.status.idle":"2022-05-26T07:43:59.910059Z","shell.execute_reply.started":"2022-05-26T07:43:51.965750Z","shell.execute_reply":"2022-05-26T07:43:59.909246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reading the folder architecture of Kaggle to get the dataset path.\n","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:43:59.911667Z","iopub.execute_input":"2022-05-26T07:43:59.912820Z","iopub.status.idle":"2022-05-26T07:43:59.920725Z","shell.execute_reply.started":"2022-05-26T07:43:59.912771Z","shell.execute_reply":"2022-05-26T07:43:59.919926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Reading the Train and Test Datasets.","metadata":{}},{"cell_type":"code","source":"mnist_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nmnist_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:44:37.632160Z","iopub.execute_input":"2022-05-26T07:44:37.633117Z","iopub.status.idle":"2022-05-26T07:44:43.690041Z","shell.execute_reply.started":"2022-05-26T07:44:37.633073Z","shell.execute_reply":"2022-05-26T07:44:43.689010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(mnist_train.shape, mnist_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:44:52.405823Z","iopub.execute_input":"2022-05-26T07:44:52.406236Z","iopub.status.idle":"2022-05-26T07:44:52.412405Z","shell.execute_reply.started":"2022-05-26T07:44:52.406200Z","shell.execute_reply":"2022-05-26T07:44:52.410950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnist_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:45:03.429924Z","iopub.execute_input":"2022-05-26T07:45:03.430352Z","iopub.status.idle":"2022-05-26T07:45:03.463386Z","shell.execute_reply.started":"2022-05-26T07:45:03.430294Z","shell.execute_reply":"2022-05-26T07:45:03.461879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnist_train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:45:14.672344Z","iopub.execute_input":"2022-05-26T07:45:14.672730Z","iopub.status.idle":"2022-05-26T07:45:17.223911Z","shell.execute_reply.started":"2022-05-26T07:45:14.672699Z","shell.execute_reply":"2022-05-26T07:45:17.222886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Cleaning and Normalization","metadata":{}},{"cell_type":"code","source":"mnist_train.isna().any().any()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:45:25.465138Z","iopub.execute_input":"2022-05-26T07:45:25.465553Z","iopub.status.idle":"2022-05-26T07:45:25.487168Z","shell.execute_reply.started":"2022-05-26T07:45:25.465519Z","shell.execute_reply":"2022-05-26T07:45:25.486426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Data Normalization","metadata":{}},{"cell_type":"code","source":"# dividing the data into the input and output features to train make the model learn based on what to take in and what to throw out.\nmnist_train_data = mnist_train.loc[:, \"pixel0\":]\nmnist_train_label = mnist_train.loc[:, \"label\"]\n\n# Notmailzing the images array to be in the range of 0-1 by dividing them by the max possible value. \n# Here is it 255 as we have 255 value range for pixels of an image. \nmnist_train_data = mnist_train_data/255.0\nmnist_test = mnist_test/255.0","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:46:39.162114Z","iopub.execute_input":"2022-05-26T07:46:39.162545Z","iopub.status.idle":"2022-05-26T07:46:39.272053Z","shell.execute_reply.started":"2022-05-26T07:46:39.162511Z","shell.execute_reply":"2022-05-26T07:46:39.271333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Single Digit Visualization using Arrays","metadata":{}},{"cell_type":"code","source":"# Let's make some beautiful plots.\ndigit_array = mnist_train.loc[3, \"pixel0\":]\narr = np.array(digit_array) \n\n#.reshape(a, (28,28))\nimage_array = np.reshape(arr, (28,28))\n\ndigit_img = plt.imshow(image_array, cmap=plt.cm.binary)\nplt.colorbar(digit_img)\nprint(\"IMAGE LABEL: {}\".format(mnist_train.loc[3, \"label\"]))","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:46:51.169597Z","iopub.execute_input":"2022-05-26T07:46:51.170033Z","iopub.status.idle":"2022-05-26T07:46:51.413901Z","shell.execute_reply.started":"2022-05-26T07:46:51.170000Z","shell.execute_reply":"2022-05-26T07:46:51.413118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## PCA Implementation on MNIST Digits\n\n1) Using manual approach:\n\na) Compute standardization of data","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nstandardized_scalar = StandardScaler()\nstandardized_data = standardized_scalar.fit_transform(mnist_train_data)\nstandardized_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:48:04.454808Z","iopub.execute_input":"2022-05-26T07:48:04.455403Z","iopub.status.idle":"2022-05-26T07:48:05.058254Z","shell.execute_reply.started":"2022-05-26T07:48:04.455356Z","shell.execute_reply":"2022-05-26T07:48:05.057104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"b) Calculate covariance matrix S(dxd)","metadata":{}},{"cell_type":"code","source":"cov_matrix = np.matmul(standardized_data.T, standardized_data)\ncov_matrix.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:48:45.058476Z","iopub.execute_input":"2022-05-26T07:48:45.058843Z","iopub.status.idle":"2022-05-26T07:48:45.600071Z","shell.execute_reply.started":"2022-05-26T07:48:45.058814Z","shell.execute_reply":"2022-05-26T07:48:45.596432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"c) Calculate Eigen values and eigen vectors","metadata":{}},{"cell_type":"code","source":"from scipy.linalg import eigh\n\nlambdas, vectors = eigh(cov_matrix, eigvals=(782, 783))\nvectors.shape\n","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:49:16.837866Z","iopub.execute_input":"2022-05-26T07:49:16.838281Z","iopub.status.idle":"2022-05-26T07:49:16.909045Z","shell.execute_reply.started":"2022-05-26T07:49:16.838247Z","shell.execute_reply":"2022-05-26T07:49:16.907988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectors = vectors.T\nvectors.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:49:51.123061Z","iopub.execute_input":"2022-05-26T07:49:51.123612Z","iopub.status.idle":"2022-05-26T07:49:51.130337Z","shell.execute_reply.started":"2022-05-26T07:49:51.123553Z","shell.execute_reply":"2022-05-26T07:49:51.129399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"d) Calculate unit vectors U1=V1 and new coordinates","metadata":{}},{"cell_type":"code","source":"new_coordinates = np.matmul(vectors, standardized_data.T)\nprint(new_coordinates.shape)\nnew_coordinates = np.vstack((new_coordinates, mnist_train_label)).T","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:50:01.676067Z","iopub.execute_input":"2022-05-26T07:50:01.676508Z","iopub.status.idle":"2022-05-26T07:50:01.744225Z","shell.execute_reply.started":"2022-05-26T07:50:01.676475Z","shell.execute_reply":"2022-05-26T07:50:01.743167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_new = pd.DataFrame(new_coordinates, columns=[\"f1\", \"f2\", \"labels\"])\ndf_new.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:50:40.043266Z","iopub.execute_input":"2022-05-26T07:50:40.043661Z","iopub.status.idle":"2022-05-26T07:50:40.056221Z","shell.execute_reply.started":"2022-05-26T07:50:40.043630Z","shell.execute_reply":"2022-05-26T07:50:40.055235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"e) Plot FacetGrid using Seaborn","metadata":{}},{"cell_type":"code","source":"sns.FacetGrid(df_new, hue=\"labels\", size=6).map(plt.scatter, \"f1\", \"f2\").add_legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:51:15.021412Z","iopub.execute_input":"2022-05-26T07:51:15.021817Z","iopub.status.idle":"2022-05-26T07:51:15.931480Z","shell.execute_reply.started":"2022-05-26T07:51:15.021784Z","shell.execute_reply":"2022-05-26T07:51:15.930674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"2) Using Sci-kit Learn library:","metadata":{}},{"cell_type":"code","source":"from sklearn import decomposition\n\npca = decomposition.PCA()\npca.n_components = 2\npca_data = pca.fit_transform(standardized_data)\npca_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:51:36.541196Z","iopub.execute_input":"2022-05-26T07:51:36.541764Z","iopub.status.idle":"2022-05-26T07:51:40.789912Z","shell.execute_reply.started":"2022-05-26T07:51:36.541717Z","shell.execute_reply":"2022-05-26T07:51:40.788615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca_data = np.vstack((pca_data.T, mnist_train_label)).T\ndf_PCA = pd.DataFrame(new_coordinates, columns=[\"f1\", \"f2\", \"labels\"])\ndf_PCA.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:52:32.307222Z","iopub.execute_input":"2022-05-26T07:52:32.309698Z","iopub.status.idle":"2022-05-26T07:52:32.333003Z","shell.execute_reply.started":"2022-05-26T07:52:32.309609Z","shell.execute_reply":"2022-05-26T07:52:32.331610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.FacetGrid(df_new, hue=\"labels\", size=12).map(plt.scatter, \"f1\", \"f2\").add_legend()\nplt.savefig(\"PCA_FacetGrid.png\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:52:45.545629Z","iopub.execute_input":"2022-05-26T07:52:45.546052Z","iopub.status.idle":"2022-05-26T07:52:47.036951Z","shell.execute_reply.started":"2022-05-26T07:52:45.546018Z","shell.execute_reply":"2022-05-26T07:52:47.036200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### PCA Dimension Reduction","metadata":{}},{"cell_type":"code","source":"pca.n_components = 784\npca_data = pca.fit_transform(standardized_data)\npercent_variance_retained = pca.explained_variance_ / np.sum(pca.explained_variance_)\n\ncum_variance_retained = np.cumsum(percent_variance_retained)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:53:20.582459Z","iopub.execute_input":"2022-05-26T07:53:20.582830Z","iopub.status.idle":"2022-05-26T07:53:26.447069Z","shell.execute_reply.started":"2022-05-26T07:53:20.582801Z","shell.execute_reply":"2022-05-26T07:53:26.445972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(1, figsize=(10, 6))\nplt.clf()\nplt.plot(cum_variance_retained, linewidth=2)\nplt.axis(\"tight\")\nplt.grid()\nplt.xlabel(\"number of compoments\")\nplt.ylabel(\"cumulative variance retained\")\nplt.savefig(\"pca_cumulative_variance.png\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:53:33.286964Z","iopub.execute_input":"2022-05-26T07:53:33.287590Z","iopub.status.idle":"2022-05-26T07:53:33.587481Z","shell.execute_reply.started":"2022-05-26T07:53:33.287537Z","shell.execute_reply":"2022-05-26T07:53:33.586094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Manipulation","metadata":{}},{"cell_type":"code","source":"# Let's build a count plot to see the count of all the labels.\nsns.countplot(mnist_train.label)\nprint(list(mnist_train.label.value_counts().sort_index()))","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:53:45.040089Z","iopub.execute_input":"2022-05-26T07:53:45.040507Z","iopub.status.idle":"2022-05-26T07:53:45.208398Z","shell.execute_reply.started":"2022-05-26T07:53:45.040472Z","shell.execute_reply":"2022-05-26T07:53:45.207409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converting dataframe into arrays\nmnist_train_data = np.array(mnist_train_data)\nmnist_train_label = np.array(mnist_train_label)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:54:15.631480Z","iopub.execute_input":"2022-05-26T07:54:15.631928Z","iopub.status.idle":"2022-05-26T07:54:15.745751Z","shell.execute_reply.started":"2022-05-26T07:54:15.631894Z","shell.execute_reply":"2022-05-26T07:54:15.744611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshaping the input shapes to get it in the shape which the model expects to recieve later.\nmnist_train_data = mnist_train_data.reshape(mnist_train_data.shape[0], 28, 28, 1)\nprint(mnist_train_data.shape, mnist_train_label.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:54:25.306483Z","iopub.execute_input":"2022-05-26T07:54:25.306881Z","iopub.status.idle":"2022-05-26T07:54:25.314947Z","shell.execute_reply.started":"2022-05-26T07:54:25.306849Z","shell.execute_reply":"2022-05-26T07:54:25.312888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model Building Process\n> Training a neural network with one input layer, one hidden layer and one output layer for learning the digits in images.","metadata":{}},{"cell_type":"code","source":"# But first import some cool libraries before getting our hands dirty!! \n# TensorFlow is Google's open source AI framework and we are using is here to build model.\n# Keras is built on top of Tensorflow and gives us\n# NO MORE GEEKY STUFF, Know more about them here:  https://www.tensorflow.org     https://keras.io\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Lambda, Flatten, BatchNormalization\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D, AvgPool2D\nfrom tensorflow.keras.optimizers import Adadelta\nfrom keras.utils.np_utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.callbacks import LearningRateScheduler","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:57:22.349548Z","iopub.execute_input":"2022-05-26T07:57:22.350070Z","iopub.status.idle":"2022-05-26T07:57:22.360296Z","shell.execute_reply.started":"2022-05-26T07:57:22.350036Z","shell.execute_reply":"2022-05-26T07:57:22.359382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Encoding Train Labels","metadata":{}},{"cell_type":"code","source":"# Encoding the labels and making them as the class value and finally converting them as categorical values.\nnclasses = mnist_train_label.max() - mnist_train_label.min() + 1\nmnist_train_label = to_categorical(mnist_train_label, num_classes = nclasses)\nprint(\"Shape of ytrain after encoding: \", mnist_train_label.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:57:25.411703Z","iopub.execute_input":"2022-05-26T07:57:25.412098Z","iopub.status.idle":"2022-05-26T07:57:25.419839Z","shell.execute_reply.started":"2022-05-26T07:57:25.412065Z","shell.execute_reply":"2022-05-26T07:57:25.418900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Model Building","metadata":{}},{"cell_type":"code","source":"def build_model(input_shape=(28, 28, 1)):\n    model = Sequential()\n    model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape = input_shape))\n    model.add(BatchNormalization())\n    model.add(Conv2D(32, kernel_size = 3, activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.4))\n\n    model.add(Conv2D(64, kernel_size = 3, activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(64, kernel_size = 3, activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.4))\n\n    model.add(Conv2D(128, kernel_size = 4, activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Flatten())\n    model.add(Dropout(0.4))\n    model.add(Dense(10, activation='softmax'))\n    return model\n\ndef compile_model(model, optimizer='adam', loss='categorical_crossentropy'):\n    model.compile(optimizer=optimizer, loss=loss, metrics=[\"accuracy\"])\n    \n    \ndef train_model(model, train, test, epochs, split):\n    history = model.fit(train, test, shuffle=True, epochs=epochs, validation_split=split)\n    return history","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:58:15.715991Z","iopub.execute_input":"2022-05-26T07:58:15.716538Z","iopub.status.idle":"2022-05-26T07:58:15.731265Z","shell.execute_reply.started":"2022-05-26T07:58:15.716502Z","shell.execute_reply":"2022-05-26T07:58:15.730277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training the model using the above function built to build, compile and train the model\ncnn_model = build_model((28, 28, 1))\ncompile_model(cnn_model, 'adam', 'categorical_crossentropy')\n\n# train the model for as many epochs as you want but I found training it above 80 will not help us and eventually increase overfitting.\nmodel_history = train_model(cnn_model, mnist_train_data, mnist_train_label, 80, 0.2)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T07:59:05.493640Z","iopub.execute_input":"2022-05-26T07:59:05.494258Z","iopub.status.idle":"2022-05-26T09:16:25.315122Z","shell.execute_reply.started":"2022-05-26T07:59:05.494220Z","shell.execute_reply":"2022-05-26T09:16:25.313421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model Performance","metadata":{}},{"cell_type":"code","source":"def plot_model_performance(metric, validations_metric):\n    plt.plot(model_history.history[metric],label = str('Training ' + metric))\n    plt.plot(model_history.history[validations_metric],label = str('Validation ' + metric))\n    plt.legend()\n    plt.savefig(str(metric + '_plot.png'))","metadata":{"execution":{"iopub.status.busy":"2022-05-26T09:16:25.318758Z","iopub.execute_input":"2022-05-26T09:16:25.319729Z","iopub.status.idle":"2022-05-26T09:16:25.328282Z","shell.execute_reply.started":"2022-05-26T09:16:25.319631Z","shell.execute_reply":"2022-05-26T09:16:25.327358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model_performance('accuracy', 'val_accuracy')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T09:16:25.329417Z","iopub.execute_input":"2022-05-26T09:16:25.329744Z","iopub.status.idle":"2022-05-26T09:16:25.588290Z","shell.execute_reply.started":"2022-05-26T09:16:25.329714Z","shell.execute_reply":"2022-05-26T09:16:25.587318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model_performance('loss', 'val_loss')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T09:16:25.590232Z","iopub.execute_input":"2022-05-26T09:16:25.590598Z","iopub.status.idle":"2022-05-26T09:16:25.813891Z","shell.execute_reply.started":"2022-05-26T09:16:25.590569Z","shell.execute_reply":"2022-05-26T09:16:25.812916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Transforming Testing Data","metadata":{}},{"cell_type":"code","source":"# reshaping the test arrays as we did to train images above somewhere.\nmnist_test_arr = np.array(mnist_test)\nmnist_test_arr = mnist_test_arr.reshape(mnist_test_arr.shape[0], 28, 28, 1)\nprint(mnist_test_arr.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T09:16:25.815120Z","iopub.execute_input":"2022-05-26T09:16:25.815540Z","iopub.status.idle":"2022-05-26T09:16:26.529343Z","shell.execute_reply.started":"2022-05-26T09:16:25.815507Z","shell.execute_reply":"2022-05-26T09:16:26.528395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Prediction & Submission","metadata":{}},{"cell_type":"code","source":"# Now, since the model is trained, it's time to find the results for the unseen test images.\npredictions = cnn_model.predict(mnist_test_arr)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T09:16:26.530668Z","iopub.execute_input":"2022-05-26T09:16:26.530978Z","iopub.status.idle":"2022-05-26T09:16:38.223931Z","shell.execute_reply.started":"2022-05-26T09:16:26.530949Z","shell.execute_reply":"2022-05-26T09:16:38.222979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Finally, making the final submissions assuming that we have to submit it in any comptition. P)\npredictions_test = []\n\nfor i in predictions:\n    predictions_test.append(np.argmax(i))","metadata":{"execution":{"iopub.status.busy":"2022-05-26T09:16:38.225255Z","iopub.execute_input":"2022-05-26T09:16:38.226146Z","iopub.status.idle":"2022-05-26T09:16:38.307620Z","shell.execute_reply.started":"2022-05-26T09:16:38.226112Z","shell.execute_reply":"2022-05-26T09:16:38.306894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission =  pd.DataFrame({\n        \"ImageId\": mnist_test.index+1,\n        \"Label\": predictions_test\n    })\n\nsubmission.to_csv('my_submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-26T09:16:38.308807Z","iopub.execute_input":"2022-05-26T09:16:38.309755Z","iopub.status.idle":"2022-05-26T09:16:38.394666Z","shell.execute_reply.started":"2022-05-26T09:16:38.309710Z","shell.execute_reply":"2022-05-26T09:16:38.393944Z"},"trusted":true},"execution_count":null,"outputs":[]}]}