{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:17.359871Z","iopub.execute_input":"2025-07-26T12:29:17.360258Z","iopub.status.idle":"2025-07-26T12:29:17.368255Z","shell.execute_reply.started":"2025-07-26T12:29:17.360228Z","shell.execute_reply":"2025-07-26T12:29:17.367219Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Importing libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.preprocessing import StandardScaler\nfrom keras.utils import to_categorical \nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nimport tensorflow as tf\nfrom tensorflow.keras import Sequential,callbacks\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:41:04.547196Z","iopub.execute_input":"2025-07-26T12:41:04.547551Z","iopub.status.idle":"2025-07-26T12:41:04.553159Z","shell.execute_reply.started":"2025-07-26T12:41:04.547517Z","shell.execute_reply":"2025-07-26T12:41:04.551935Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Loading data","metadata":{}},{"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\")\nss_df=pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:17.388784Z","iopub.execute_input":"2025-07-26T12:29:17.389223Z","iopub.status.idle":"2025-07-26T12:29:20.822202Z","shell.execute_reply.started":"2025-07-26T12:29:17.389182Z","shell.execute_reply":"2025-07-26T12:29:20.820980Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:20.823823Z","iopub.execute_input":"2025-07-26T12:29:20.824134Z","iopub.status.idle":"2025-07-26T12:29:20.839703Z","shell.execute_reply.started":"2025-07-26T12:29:20.824090Z","shell.execute_reply":"2025-07-26T12:29:20.838494Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:20.841282Z","iopub.execute_input":"2025-07-26T12:29:20.841592Z","iopub.status.idle":"2025-07-26T12:29:20.855882Z","shell.execute_reply.started":"2025-07-26T12:29:20.841560Z","shell.execute_reply":"2025-07-26T12:29:20.854673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:20.856992Z","iopub.execute_input":"2025-07-26T12:29:20.857390Z","iopub.status.idle":"2025-07-26T12:29:20.880701Z","shell.execute_reply.started":"2025-07-26T12:29:20.857354Z","shell.execute_reply":"2025-07-26T12:29:20.879507Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:20.881796Z","iopub.execute_input":"2025-07-26T12:29:20.882221Z","iopub.status.idle":"2025-07-26T12:29:20.898560Z","shell.execute_reply.started":"2025-07-26T12:29:20.882180Z","shell.execute_reply":"2025-07-26T12:29:20.897216Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ss_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:20.899532Z","iopub.execute_input":"2025-07-26T12:29:20.899866Z","iopub.status.idle":"2025-07-26T12:29:20.919334Z","shell.execute_reply.started":"2025-07-26T12:29:20.899812Z","shell.execute_reply":"2025-07-26T12:29:20.918287Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data Preparation","metadata":{}},{"cell_type":"code","source":"img_rows, img_cols = 28, 28\nnum_classes = 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:20.922695Z","iopub.execute_input":"2025-07-26T12:29:20.923090Z","iopub.status.idle":"2025-07-26T12:29:20.935741Z","shell.execute_reply.started":"2025-07-26T12:29:20.923052Z","shell.execute_reply":"2025-07-26T12:29:20.934792Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"The number of missing values in the training dataset is \",train_df.isna().any().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:20.937823Z","iopub.execute_input":"2025-07-26T12:29:20.938255Z","iopub.status.idle":"2025-07-26T12:29:20.961775Z","shell.execute_reply.started":"2025-07-26T12:29:20.938214Z","shell.execute_reply":"2025-07-26T12:29:20.960698Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"The number of missing values in the testing dataset is \",test_df.isna().any().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:20.962792Z","iopub.execute_input":"2025-07-26T12:29:20.963067Z","iopub.status.idle":"2025-07-26T12:29:20.974192Z","shell.execute_reply.started":"2025-07-26T12:29:20.963043Z","shell.execute_reply":"2025-07-26T12:29:20.973084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"label\"].value_counts().sort_index()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:20.975441Z","iopub.execute_input":"2025-07-26T12:29:20.975821Z","iopub.status.idle":"2025-07-26T12:29:20.991524Z","shell.execute_reply.started":"2025-07-26T12:29:20.975782Z","shell.execute_reply":"2025-07-26T12:29:20.990189Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x=\"label\",data=train_df)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:20.992519Z","iopub.execute_input":"2025-07-26T12:29:20.992890Z","iopub.status.idle":"2025-07-26T12:29:21.172316Z","shell.execute_reply.started":"2025-07-26T12:29:20.992861Z","shell.execute_reply":"2025-07-26T12:29:21.171217Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:21.173278Z","iopub.execute_input":"2025-07-26T12:29:21.173552Z","iopub.status.idle":"2025-07-26T12:29:21.210735Z","shell.execute_reply.started":"2025-07-26T12:29:21.173527Z","shell.execute_reply":"2025-07-26T12:29:21.209703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train = train_df[\"label\"]\nX_train = train_df.drop(labels = [\"label\"],axis = 1) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:21.211784Z","iopub.execute_input":"2025-07-26T12:29:21.212093Z","iopub.status.idle":"2025-07-26T12:29:21.323386Z","shell.execute_reply.started":"2025-07-26T12:29:21.212058Z","shell.execute_reply":"2025-07-26T12:29:21.322453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = X_train / 255.0\ntest_df = test_df / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:21.324333Z","iopub.execute_input":"2025-07-26T12:29:21.324598Z","iopub.status.idle":"2025-07-26T12:29:21.442526Z","shell.execute_reply.started":"2025-07-26T12:29:21.324578Z","shell.execute_reply":"2025-07-26T12:29:21.441378Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest_df = test_df.values.reshape(-1,28,28,1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:21.443433Z","iopub.execute_input":"2025-07-26T12:29:21.443687Z","iopub.status.idle":"2025-07-26T12:29:21.448397Z","shell.execute_reply.started":"2025-07-26T12:29:21.443667Z","shell.execute_reply":"2025-07-26T12:29:21.447173Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train = to_categorical(y_train, num_classes = 10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:21.449392Z","iopub.execute_input":"2025-07-26T12:29:21.449704Z","iopub.status.idle":"2025-07-26T12:29:21.466945Z","shell.execute_reply.started":"2025-07-26T12:29:21.449669Z","shell.execute_reply":"2025-07-26T12:29:21.465704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:21.468029Z","iopub.execute_input":"2025-07-26T12:29:21.468350Z","iopub.status.idle":"2025-07-26T12:29:21.486959Z","shell.execute_reply.started":"2025-07-26T12:29:21.468325Z","shell.execute_reply":"2025-07-26T12:29:21.485993Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.2, random_state=42)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:21.487984Z","iopub.execute_input":"2025-07-26T12:29:21.488309Z","iopub.status.idle":"2025-07-26T12:29:21.925136Z","shell.execute_reply.started":"2025-07-26T12:29:21.488282Z","shell.execute_reply":"2025-07-26T12:29:21.924046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(X_train[1][:,:,0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:21.926046Z","iopub.execute_input":"2025-07-26T12:29:21.926349Z","iopub.status.idle":"2025-07-26T12:29:22.116018Z","shell.execute_reply.started":"2025-07-26T12:29:21.926326Z","shell.execute_reply":"2025-07-26T12:29:22.114906Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Model Architecture CNN","metadata":{}},{"cell_type":"code","source":"model=Sequential()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:22.117382Z","iopub.execute_input":"2025-07-26T12:29:22.117763Z","iopub.status.idle":"2025-07-26T12:29:22.123190Z","shell.execute_reply.started":"2025-07-26T12:29:22.117725Z","shell.execute_reply":"2025-07-26T12:29:22.122097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.add(Conv2D(12,kernel_size=3,activation='relu',input_shape=(img_rows,img_cols,1)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:22.124441Z","iopub.execute_input":"2025-07-26T12:29:22.124823Z","iopub.status.idle":"2025-07-26T12:29:22.151651Z","shell.execute_reply.started":"2025-07-26T12:29:22.124773Z","shell.execute_reply":"2025-07-26T12:29:22.150593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.add(Conv2D(20,kernel_size=3,activation='relu'))\nmodel.add(Conv2D(20,kernel_size=3,activation='relu'))\nmodel.add(Flatten())\nmodel.add(Dense(100,activation='relu'))\nmodel.add(Dense(num_classes,activation='softmax'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:22.155202Z","iopub.execute_input":"2025-07-26T12:29:22.155511Z","iopub.status.idle":"2025-07-26T12:29:22.216252Z","shell.execute_reply.started":"2025-07-26T12:29:22.155483Z","shell.execute_reply":"2025-07-26T12:29:22.215150Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"early_stopping = callbacks.EarlyStopping(\n    min_delta=0.001, # minimium amount of change to count as an improvement\n    patience=10, # how many epochs to wait before stopping\n    restore_best_weights=True,\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:22.217544Z","iopub.execute_input":"2025-07-26T12:29:22.217901Z","iopub.status.idle":"2025-07-26T12:29:22.222289Z","shell.execute_reply.started":"2025-07-26T12:29:22.217873Z","shell.execute_reply":"2025-07-26T12:29:22.221151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Model Compiler\nmodel.compile(\n    loss=\"categorical_crossentropy\",\n    optimizer='adam',\n    metrics=['accuracy']\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:22.223442Z","iopub.execute_input":"2025-07-26T12:29:22.223798Z","iopub.status.idle":"2025-07-26T12:29:22.242043Z","shell.execute_reply.started":"2025-07-26T12:29:22.223762Z","shell.execute_reply":"2025-07-26T12:29:22.241033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history=model.fit(X_train,y_train,\n    validation_data=(X_val,y_val),\n    batch_size=256,\n    epochs=500,\n    validation_split=0.2,\n    callbacks=[early_stopping]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:29:22.243060Z","iopub.execute_input":"2025-07-26T12:29:22.243454Z","iopub.status.idle":"2025-07-26T12:36:18.472031Z","shell.execute_reply.started":"2025-07-26T12:29:22.243408Z","shell.execute_reply":"2025-07-26T12:36:18.471014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['loss', 'val_loss']].plot();\nprint(\"Minimum validation loss: {}\".format(history_df['val_loss'].min()))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:37:47.238831Z","iopub.execute_input":"2025-07-26T12:37:47.239245Z","iopub.status.idle":"2025-07-26T12:37:47.486240Z","shell.execute_reply.started":"2025-07-26T12:37:47.239213Z","shell.execute_reply":"2025-07-26T12:37:47.485233Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['accuracy', 'val_accuracy']].plot();\nprint(\"Minimum validation accuracy: {}\".format(history_df['val_accuracy'].min()))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:37:53.464968Z","iopub.execute_input":"2025-07-26T12:37:53.465370Z","iopub.status.idle":"2025-07-26T12:37:53.704688Z","shell.execute_reply.started":"2025-07-26T12:37:53.465337Z","shell.execute_reply":"2025-07-26T12:37:53.703579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_confusion_matrix(cm, classes,\n                          normalize=False,\n                          title='Confusion matrix',\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n                 horizontalalignment=\"center\",\n                 color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n\n# Predict the values from the validation dataset\ny_pred = model.predict(X_val)\n# Convert predictions classes to one hot vectors \ny_pred_classes = np.argmax(y_pred,axis = 1) \n# Convert validation observations to one hot vectors\ny_true = np.argmax(y_val,axis = 1) \n# compute the confusion matrix\nconfusion_mtx = confusion_matrix(y_true, y_pred_classes) \n# plot the confusion matrix\nplot_confusion_matrix(confusion_mtx, classes = range(10)) \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:41:08.437638Z","iopub.execute_input":"2025-07-26T12:41:08.438078Z","iopub.status.idle":"2025-07-26T12:41:11.258830Z","shell.execute_reply.started":"2025-07-26T12:41:08.438048Z","shell.execute_reply":"2025-07-26T12:41:11.257660Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = model.predict(test_df)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:42:03.651916Z","iopub.execute_input":"2025-07-26T12:42:03.652303Z","iopub.status.idle":"2025-07-26T12:42:11.023112Z","shell.execute_reply.started":"2025-07-26T12:42:03.652273Z","shell.execute_reply":"2025-07-26T12:42:11.022005Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"cnn_mnist.csv\",index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:43:21.375078Z","iopub.execute_input":"2025-07-26T12:43:21.375457Z","iopub.status.idle":"2025-07-26T12:43:21.402389Z","shell.execute_reply.started":"2025-07-26T12:43:21.375431Z","shell.execute_reply":"2025-07-26T12:43:21.401311Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-26T12:43:23.569642Z","iopub.execute_input":"2025-07-26T12:43:23.569973Z","iopub.status.idle":"2025-07-26T12:43:23.579976Z","shell.execute_reply.started":"2025-07-26T12:43:23.569946Z","shell.execute_reply":"2025-07-26T12:43:23.578866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}