{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-08T11:28:32.027889Z","iopub.execute_input":"2022-09-08T11:28:32.028562Z","iopub.status.idle":"2022-09-08T11:28:32.058324Z","shell.execute_reply.started":"2022-09-08T11:28:32.028479Z","shell.execute_reply":"2022-09-08T11:28:32.057500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:32.059869Z","iopub.execute_input":"2022-09-08T11:28:32.060316Z","iopub.status.idle":"2022-09-08T11:28:39.722740Z","shell.execute_reply.started":"2022-09-08T11:28:32.060285Z","shell.execute_reply":"2022-09-08T11:28:39.721732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nsubmission_df = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:39.724112Z","iopub.execute_input":"2022-09-08T11:28:39.724882Z","iopub.status.idle":"2022-09-08T11:28:43.604511Z","shell.execute_reply.started":"2022-09-08T11:28:39.724843Z","shell.execute_reply":"2022-09-08T11:28:43.603491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:43.607040Z","iopub.execute_input":"2022-09-08T11:28:43.607523Z","iopub.status.idle":"2022-09-08T11:28:43.634549Z","shell.execute_reply.started":"2022-09-08T11:28:43.607494Z","shell.execute_reply":"2022-09-08T11:28:43.633211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:43.636133Z","iopub.execute_input":"2022-09-08T11:28:43.636482Z","iopub.status.idle":"2022-09-08T11:28:43.643537Z","shell.execute_reply.started":"2022-09-08T11:28:43.636453Z","shell.execute_reply":"2022-09-08T11:28:43.642368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:43.645316Z","iopub.execute_input":"2022-09-08T11:28:43.645766Z","iopub.status.idle":"2022-09-08T11:28:43.702730Z","shell.execute_reply.started":"2022-09-08T11:28:43.645738Z","shell.execute_reply":"2022-09-08T11:28:43.701518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isna().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:43.703840Z","iopub.execute_input":"2022-09-08T11:28:43.704150Z","iopub.status.idle":"2022-09-08T11:28:43.734322Z","shell.execute_reply.started":"2022-09-08T11:28:43.704121Z","shell.execute_reply":"2022-09-08T11:28:43.733605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's see the class distribution balanced or not\n\nplt.figure(figsize=(8,7))\nsns.countplot(x=\"label\", data=train_df)\nplt.title(\"Label distribution\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:43.735503Z","iopub.execute_input":"2022-09-08T11:28:43.735810Z","iopub.status.idle":"2022-09-08T11:28:43.925557Z","shell.execute_reply.started":"2022-09-08T11:28:43.735784Z","shell.execute_reply":"2022-09-08T11:28:43.924691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.values","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:43.929202Z","iopub.execute_input":"2022-09-08T11:28:43.929498Z","iopub.status.idle":"2022-09-08T11:28:43.937027Z","shell.execute_reply.started":"2022-09-08T11:28:43.929473Z","shell.execute_reply":"2022-09-08T11:28:43.936300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.values[0]","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:43.938292Z","iopub.execute_input":"2022-09-08T11:28:43.939427Z","iopub.status.idle":"2022-09-08T11:28:43.952873Z","shell.execute_reply.started":"2022-09-08T11:28:43.939377Z","shell.execute_reply":"2022-09-08T11:28:43.951936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.values[:, :1]","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:43.954883Z","iopub.execute_input":"2022-09-08T11:28:43.955498Z","iopub.status.idle":"2022-09-08T11:28:43.967261Z","shell.execute_reply.started":"2022-09-08T11:28:43.955465Z","shell.execute_reply":"2022-09-08T11:28:43.966103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.values[:, 1:]","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:43.968493Z","iopub.execute_input":"2022-09-08T11:28:43.969024Z","iopub.status.idle":"2022-09-08T11:28:43.979782Z","shell.execute_reply.started":"2022-09-08T11:28:43.968982Z","shell.execute_reply":"2022-09-08T11:28:43.979088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Divide the dataset into features and target\n\nX = train_df.values[:, 1:]\ny = train_df.values[:, :1]","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:43.980803Z","iopub.execute_input":"2022-09-08T11:28:43.981190Z","iopub.status.idle":"2022-09-08T11:28:43.990703Z","shell.execute_reply.started":"2022-09-08T11:28:43.981166Z","shell.execute_reply":"2022-09-08T11:28:43.989673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:43.991846Z","iopub.execute_input":"2022-09-08T11:28:43.992263Z","iopub.status.idle":"2022-09-08T11:28:44.002348Z","shell.execute_reply.started":"2022-09-08T11:28:43.992236Z","shell.execute_reply":"2022-09-08T11:28:44.001680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.003434Z","iopub.execute_input":"2022-09-08T11:28:44.003871Z","iopub.status.idle":"2022-09-08T11:28:44.016422Z","shell.execute_reply.started":"2022-09-08T11:28:44.003845Z","shell.execute_reply":"2022-09-08T11:28:44.015609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scaling all pixel value for bring the same range between 0 to 1\n\nX_scale = X / 255.","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.017838Z","iopub.execute_input":"2022-09-08T11:28:44.018411Z","iopub.status.idle":"2022-09-08T11:28:44.085508Z","shell.execute_reply.started":"2022-09-08T11:28:44.018381Z","shell.execute_reply":"2022-09-08T11:28:44.084813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Max value: \", X_scale.max())\nprint(\"Max value: \", X_scale.min())","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.086443Z","iopub.execute_input":"2022-09-08T11:28:44.086718Z","iopub.status.idle":"2022-09-08T11:28:44.120197Z","shell.execute_reply.started":"2022-09-08T11:28:44.086695Z","shell.execute_reply":"2022-09-08T11:28:44.119540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_scale.shape[1])\nprint(np.sqrt(X_scale.shape[1]))\nprint(int(np.sqrt(X_scale.shape[1])))","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.121241Z","iopub.execute_input":"2022-09-08T11:28:44.121595Z","iopub.status.idle":"2022-09-08T11:28:44.125728Z","shell.execute_reply.started":"2022-09-08T11:28:44.121573Z","shell.execute_reply":"2022-09-08T11:28:44.125153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dim = int(np.sqrt(X_scale.shape[1]))\ndim","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.126843Z","iopub.execute_input":"2022-09-08T11:28:44.127634Z","iopub.status.idle":"2022-09-08T11:28:44.141309Z","shell.execute_reply.started":"2022-09-08T11:28:44.127605Z","shell.execute_reply":"2022-09-08T11:28:44.140581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = X_scale.shape[0]\nN","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.142856Z","iopub.execute_input":"2022-09-08T11:28:44.143814Z","iopub.status.idle":"2022-09-08T11:28:44.152639Z","shell.execute_reply.started":"2022-09-08T11:28:44.143774Z","shell.execute_reply":"2022-09-08T11:28:44.151713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_scale = X_scale.reshape((N, dim, dim, 1)) # Make 28 x 28 image","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.153943Z","iopub.execute_input":"2022-09-08T11:28:44.154423Z","iopub.status.idle":"2022-09-08T11:28:44.162538Z","shell.execute_reply.started":"2022-09-08T11:28:44.154394Z","shell.execute_reply":"2022-09-08T11:28:44.161841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_scale","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.163719Z","iopub.execute_input":"2022-09-08T11:28:44.164163Z","iopub.status.idle":"2022-09-08T11:28:44.180094Z","shell.execute_reply.started":"2022-09-08T11:28:44.164136Z","shell.execute_reply":"2022-09-08T11:28:44.179182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_scale[0]","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.181226Z","iopub.execute_input":"2022-09-08T11:28:44.182462Z","iopub.status.idle":"2022-09-08T11:28:44.204239Z","shell.execute_reply.started":"2022-09-08T11:28:44.182425Z","shell.execute_reply":"2022-09-08T11:28:44.202973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = to_categorical(y=y)","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.209608Z","iopub.execute_input":"2022-09-08T11:28:44.210352Z","iopub.status.idle":"2022-09-08T11:28:44.215142Z","shell.execute_reply.started":"2022-09-08T11:28:44.210317Z","shell.execute_reply":"2022-09-08T11:28:44.214397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.217488Z","iopub.execute_input":"2022-09-08T11:28:44.218275Z","iopub.status.idle":"2022-09-08T11:28:44.228075Z","shell.execute_reply.started":"2022-09-08T11:28:44.218240Z","shell.execute_reply":"2022-09-08T11:28:44.227202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"X.shape={X_scale.shape}, Y.shape={y.shape}\")","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.230395Z","iopub.execute_input":"2022-09-08T11:28:44.230897Z","iopub.status.idle":"2022-09-08T11:28:44.238179Z","shell.execute_reply.started":"2022-09-08T11:28:44.230871Z","shell.execute_reply":"2022-09-08T11:28:44.237277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let's create Model","metadata":{}},{"cell_type":"code","source":"# x_train, x_test, y_train, y_test = train_test_split(X_scale, y, train_size=0.8, random_state=2)\n\nx_train, x_test, y_train, y_test = train_test_split(X_scale, y, test_size=0.2, random_state=2)","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.239468Z","iopub.execute_input":"2022-09-08T11:28:44.240436Z","iopub.status.idle":"2022-09-08T11:28:44.477229Z","shell.execute_reply.started":"2022-09-08T11:28:44.240402Z","shell.execute_reply":"2022-09-08T11:28:44.476264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.478230Z","iopub.execute_input":"2022-09-08T11:28:44.478473Z","iopub.status.idle":"2022-09-08T11:28:44.484524Z","shell.execute_reply.started":"2022-09-08T11:28:44.478451Z","shell.execute_reply":"2022-09-08T11:28:44.483506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(layer=Conv2D(filters=16, kernel_size=(3,3), padding=\"same\", strides=(1,1), activation=\"relu\", input_shape=(28,28,1)))\nmodel.add(layer=Conv2D(filters=32, kernel_size=(3,3), padding=\"same\", strides=(1,1), activation=\"relu\"))\nmodel.add(layer=MaxPooling2D(pool_size=(2,2), strides=(2,2))) # pooled features size = 14 x 14 x 32\nmodel.add(layer=Conv2D(filters=64, kernel_size=(3,3), padding=\"same\", strides=(1,1), activation=\"relu\"))\nmodel.add(layer=MaxPooling2D(pool_size=(2,2), strides=(2,2))) # pooled features size = 7 x 7 x 64\nmodel.add(layer=Dropout(rate=0.3)) # to reduce the overfit condition\nmodel.add(layer=Flatten()) # to convert multidimantation to one dimantation for Dense layers\nmodel.add(layer=Dense(units=256, activation=\"relu\"))\nmodel.add(layer=Dropout(rate=0.1)) # to reduce the overfit condition\nmodel.add(layer=Dense(units=128, activation=\"relu\"))\nmodel.add(layer=Dense(units=10, activation=\"softmax\")) # For multi-class classification","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.485869Z","iopub.execute_input":"2022-09-08T11:28:44.486106Z","iopub.status.idle":"2022-09-08T11:28:44.658604Z","shell.execute_reply.started":"2022-09-08T11:28:44.486084Z","shell.execute_reply":"2022-09-08T11:28:44.657699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.659791Z","iopub.execute_input":"2022-09-08T11:28:44.660984Z","iopub.status.idle":"2022-09-08T11:28:44.667776Z","shell.execute_reply.started":"2022-09-08T11:28:44.660936Z","shell.execute_reply":"2022-09-08T11:28:44.666538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tf.keras.utils.plot_model(model=model, show_shapes=True, show_dtype=True,expand_nested=True)\n\ntf.keras.utils.plot_model(model=model, show_shapes=True, show_dtype=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:44.668809Z","iopub.execute_input":"2022-09-08T11:28:44.669470Z","iopub.status.idle":"2022-09-08T11:28:45.769538Z","shell.execute_reply.started":"2022-09-08T11:28:44.669442Z","shell.execute_reply":"2022-09-08T11:28:45.768509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import optimizers\nfrom tensorflow.keras import losses","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:45.771237Z","iopub.execute_input":"2022-09-08T11:28:45.772224Z","iopub.status.idle":"2022-09-08T11:28:45.777427Z","shell.execute_reply.started":"2022-09-08T11:28:45.772185Z","shell.execute_reply":"2022-09-08T11:28:45.776386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.compile(optimizer=\"Adam\", loss=\"binary_crossentropy\", metrics=['accuracy'])\n# model.compile(optimizer=\"Adam\", loss=\"categorical_crossentropy\", metrics=['accuracy'])\n\nmodel.compile(optimizer=optimizers.Adam(), loss=losses.BinaryCrossentropy(), metrics=['accuracy'])\n\n# model.compile(optimizer=optimizers.Adam(), loss=losses.CategoricalCrossentropy(), metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:45.778374Z","iopub.execute_input":"2022-09-08T11:28:45.778627Z","iopub.status.idle":"2022-09-08T11:28:45.799065Z","shell.execute_reply.started":"2022-09-08T11:28:45.778598Z","shell.execute_reply":"2022-09-08T11:28:45.798332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x=x_train, y=y_train, epochs=15, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:28:45.800098Z","iopub.execute_input":"2022-09-08T11:28:45.800666Z","iopub.status.idle":"2022-09-08T11:34:00.328272Z","shell.execute_reply.started":"2022-09-08T11:28:45.800614Z","shell.execute_reply":"2022-09-08T11:34:00.327114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(x=x_test, y=y_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:00.329811Z","iopub.execute_input":"2022-09-08T11:34:00.330299Z","iopub.status.idle":"2022-09-08T11:34:02.523267Z","shell.execute_reply.started":"2022-09-08T11:34:00.330272Z","shell.execute_reply":"2022-09-08T11:34:02.522551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Test loss:', score[0])\nprint('Test accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:02.524184Z","iopub.execute_input":"2022-09-08T11:34:02.524434Z","iopub.status.idle":"2022-09-08T11:34:02.529991Z","shell.execute_reply.started":"2022-09-08T11:34:02.524411Z","shell.execute_reply":"2022-09-08T11:34:02.529077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(x=x_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:02.531387Z","iopub.execute_input":"2022-09-08T11:34:02.531697Z","iopub.status.idle":"2022-09-08T11:34:04.347062Z","shell.execute_reply.started":"2022-09-08T11:34:02.531620Z","shell.execute_reply":"2022-09-08T11:34:04.346050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:04.348921Z","iopub.execute_input":"2022-09-08T11:34:04.349775Z","iopub.status.idle":"2022-09-08T11:34:04.357280Z","shell.execute_reply.started":"2022-09-08T11:34:04.349741Z","shell.execute_reply":"2022-09-08T11:34:04.356506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_labels = np.argmax(y_pred, axis=1)\ny_pred_labels","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:04.358599Z","iopub.execute_input":"2022-09-08T11:34:04.359196Z","iopub.status.idle":"2022-09-08T11:34:04.370480Z","shell.execute_reply.started":"2022-09-08T11:34:04.359160Z","shell.execute_reply":"2022-09-08T11:34:04.369801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:04.371462Z","iopub.execute_input":"2022-09-08T11:34:04.372031Z","iopub.status.idle":"2022-09-08T11:34:04.382784Z","shell.execute_reply.started":"2022-09-08T11:34:04.371973Z","shell.execute_reply":"2022-09-08T11:34:04.381935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_labels = np.argmax(y_test, axis=1)\ny_test_labels","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:04.383916Z","iopub.execute_input":"2022-09-08T11:34:04.384493Z","iopub.status.idle":"2022-09-08T11:34:04.395286Z","shell.execute_reply.started":"2022-09-08T11:34:04.384467Z","shell.execute_reply":"2022-09-08T11:34:04.394625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_test[0].reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:04.396342Z","iopub.execute_input":"2022-09-08T11:34:04.397468Z","iopub.status.idle":"2022-09-08T11:34:04.540040Z","shell.execute_reply.started":"2022-09-08T11:34:04.397421Z","shell.execute_reply":"2022-09-08T11:34:04.539266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compare our result\n\nfig, axis = plt.subplots(5, 4, figsize=(18,25))\n\nfor i, ax in enumerate(axis.flat):\n    ax.imshow(x_test[i].reshape(28,28), cmap=\"binary\")\n    ax.set(title=\"Predicted digit: {0}\\nActual digit: {1}\".format(y_pred_labels[i], y_test_labels[i]))","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:04.541069Z","iopub.execute_input":"2022-09-08T11:34:04.541879Z","iopub.status.idle":"2022-09-08T11:34:06.771733Z","shell.execute_reply.started":"2022-09-08T11:34:04.541848Z","shell.execute_reply":"2022-09-08T11:34:06.770711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:06.772953Z","iopub.execute_input":"2022-09-08T11:34:06.773244Z","iopub.status.idle":"2022-09-08T11:34:06.777715Z","shell.execute_reply.started":"2022-09-08T11:34:06.773217Z","shell.execute_reply":"2022-09-08T11:34:06.776854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_true=y_test_labels, y_pred=y_pred_labels)\n\nplt.figure(figsize=(12, 8))\nsns.heatmap(data=cm, annot=True, fmt='.0f')\nplt.xlabel(\"Predicted Digits\")\nplt.ylabel(\"True Digits\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:06.778795Z","iopub.execute_input":"2022-09-08T11:34:06.779048Z","iopub.status.idle":"2022-09-08T11:34:07.236592Z","shell.execute_reply.started":"2022-09-08T11:34:06.779024Z","shell.execute_reply":"2022-09-08T11:34:07.235445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let's test our model with the test datasets","metadata":{}},{"cell_type":"code","source":"test_x = test_df.values / 255.","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:07.237964Z","iopub.execute_input":"2022-09-08T11:34:07.238329Z","iopub.status.idle":"2022-09-08T11:34:07.281909Z","shell.execute_reply.started":"2022-09-08T11:34:07.238297Z","shell.execute_reply":"2022-09-08T11:34:07.280848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:07.283119Z","iopub.execute_input":"2022-09-08T11:34:07.283481Z","iopub.status.idle":"2022-09-08T11:34:07.290291Z","shell.execute_reply.started":"2022-09-08T11:34:07.283449Z","shell.execute_reply":"2022-09-08T11:34:07.289684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:07.291434Z","iopub.execute_input":"2022-09-08T11:34:07.291875Z","iopub.status.idle":"2022-09-08T11:34:07.303884Z","shell.execute_reply.started":"2022-09-08T11:34:07.291848Z","shell.execute_reply":"2022-09-08T11:34:07.303097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x = test_x.reshape(-1, dim, dim, 1)\ntest_x.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:07.305116Z","iopub.execute_input":"2022-09-08T11:34:07.305680Z","iopub.status.idle":"2022-09-08T11:34:07.314965Z","shell.execute_reply.started":"2022-09-08T11:34:07.305625Z","shell.execute_reply":"2022-09-08T11:34:07.313750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = model.predict(x=test_x)","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:07.316001Z","iopub.execute_input":"2022-09-08T11:34:07.316872Z","iopub.status.idle":"2022-09-08T11:34:13.426021Z","shell.execute_reply.started":"2022-09-08T11:34:07.316841Z","shell.execute_reply":"2022-09-08T11:34:13.424592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:13.427458Z","iopub.execute_input":"2022-09-08T11:34:13.427868Z","iopub.status.idle":"2022-09-08T11:34:13.435708Z","shell.execute_reply.started":"2022-09-08T11:34:13.427831Z","shell.execute_reply":"2022-09-08T11:34:13.434820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred_labels = np.argmax(test_pred, axis=1)\ntest_pred_labels","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:13.436879Z","iopub.execute_input":"2022-09-08T11:34:13.437356Z","iopub.status.idle":"2022-09-08T11:34:13.454394Z","shell.execute_reply.started":"2022-09-08T11:34:13.437328Z","shell.execute_reply":"2022-09-08T11:34:13.453703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred_labels.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:13.455375Z","iopub.execute_input":"2022-09-08T11:34:13.455904Z","iopub.status.idle":"2022-09-08T11:34:13.465840Z","shell.execute_reply.started":"2022-09-08T11:34:13.455875Z","shell.execute_reply":"2022-09-08T11:34:13.464902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:13.466973Z","iopub.execute_input":"2022-09-08T11:34:13.467256Z","iopub.status.idle":"2022-09-08T11:34:13.483510Z","shell.execute_reply.started":"2022-09-08T11:34:13.467231Z","shell.execute_reply":"2022-09-08T11:34:13.482751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df['Label'].shape","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:13.484637Z","iopub.execute_input":"2022-09-08T11:34:13.485477Z","iopub.status.idle":"2022-09-08T11:34:13.493227Z","shell.execute_reply.started":"2022-09-08T11:34:13.485439Z","shell.execute_reply":"2022-09-08T11:34:13.492201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df['Label'] = test_pred_labels","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:13.494387Z","iopub.execute_input":"2022-09-08T11:34:13.495373Z","iopub.status.idle":"2022-09-08T11:34:13.502330Z","shell.execute_reply.started":"2022-09-08T11:34:13.495339Z","shell.execute_reply":"2022-09-08T11:34:13.501687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:13.503392Z","iopub.execute_input":"2022-09-08T11:34:13.503667Z","iopub.status.idle":"2022-09-08T11:34:13.520514Z","shell.execute_reply.started":"2022-09-08T11:34:13.503621Z","shell.execute_reply":"2022-09-08T11:34:13.519166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-08T11:34:13.522203Z","iopub.execute_input":"2022-09-08T11:34:13.522889Z","iopub.status.idle":"2022-09-08T11:34:13.558472Z","shell.execute_reply.started":"2022-09-08T11:34:13.522862Z","shell.execute_reply":"2022-09-08T11:34:13.557711Z"},"trusted":true},"execution_count":null,"outputs":[]}]}