{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823}],"dockerImageVersionId":31329,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true},"colab":{"provenance":[]}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Link Of Dataset :- https://www.kaggle.com/competitions/digit-recognizer/data\n\n","metadata":{"id":"RdN1PxRFe70b"}},{"cell_type":"markdown","source":"# 1. Import Library & Data\n","metadata":{"id":"_88Gr3ZaIVYB"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport random\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers, models\n\nfrom sklearn.model_selection import train_test_split\n","metadata":{"trusted":true,"id":"gH5q-AiXIVYG","execution":{"iopub.status.busy":"2026-06-17T12:17:01.607129Z","iopub.execute_input":"2026-06-17T12:17:01.607878Z","iopub.status.idle":"2026-06-17T12:17:17.038370Z","shell.execute_reply.started":"2026-06-17T12:17:01.607845Z","shell.execute_reply":"2026-06-17T12:17:17.037768Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df=pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/train.csv\")\ntest_df=pd.read_csv(\"/kaggle/input/competitions/digit-recognizer/test.csv\")\ntrain_df.shape,test_df.shape\n","metadata":{"trusted":true,"id":"rHdNDhCSIVYK","outputId":"14453e7e-eda0-41bd-df3e-9771f89acbcc","execution":{"iopub.status.busy":"2026-06-17T12:17:27.365682Z","iopub.execute_input":"2026-06-17T12:17:27.365993Z","iopub.status.idle":"2026-06-17T12:17:30.428726Z","shell.execute_reply.started":"2026-06-17T12:17:27.365969Z","shell.execute_reply":"2026-06-17T12:17:30.427943Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 2. Overview of the Data","metadata":{"id":"WqzyKu7CIVYL"}},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"id":"SmCzFrjHIVYM","outputId":"eca68886-8077-4fe8-8889-811b798887fc","execution":{"iopub.status.busy":"2026-06-17T12:17:30.429670Z","iopub.execute_input":"2026-06-17T12:17:30.429982Z","iopub.status.idle":"2026-06-17T12:17:30.460735Z","shell.execute_reply.started":"2026-06-17T12:17:30.429958Z","shell.execute_reply":"2026-06-17T12:17:30.460003Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"id":"NAGU3_TLIVYN","outputId":"087d90a8-e279-4428-bbbf-94a92bc55530","execution":{"iopub.status.busy":"2026-06-17T12:17:33.933583Z","iopub.execute_input":"2026-06-17T12:17:33.934028Z","iopub.status.idle":"2026-06-17T12:17:33.946244Z","shell.execute_reply.started":"2026-06-17T12:17:33.933996Z","shell.execute_reply":"2026-06-17T12:17:33.945581Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Generate Random Number to Show Image","metadata":{"id":"fwyoks1yIVYP"}},{"cell_type":"code","source":"number=random.randint(0,42000)\nnumber","metadata":{"trusted":true,"id":"OClo4vVBIVYQ","outputId":"48e25087-56a8-45ec-e491-52743c0c9e3f","execution":{"iopub.status.busy":"2026-06-17T12:17:38.802013Z","iopub.execute_input":"2026-06-17T12:17:38.802272Z","iopub.status.idle":"2026-06-17T12:17:38.807490Z","shell.execute_reply.started":"2026-06-17T12:17:38.802251Z","shell.execute_reply":"2026-06-17T12:17:38.806665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(1,1))\nplt.imshow(train_df.iloc[number,1:].values.reshape(28,28),cmap=plt.cm.binary) \n","metadata":{"trusted":true,"id":"4yZs8wDGIVYR","outputId":"81838cdd-760f-4eb3-b133-44ff27ff544b","execution":{"iopub.status.busy":"2026-06-17T12:17:40.260255Z","iopub.execute_input":"2026-06-17T12:17:40.260988Z","iopub.status.idle":"2026-06-17T12:17:40.384849Z","shell.execute_reply.started":"2026-06-17T12:17:40.260958Z","shell.execute_reply":"2026-06-17T12:17:40.384086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12,12))\nfor i in range(10):\n    plt.subplot(10,10,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.imshow(train_df.iloc[i,1:].values.reshape(28,28),cmap=plt.cm.binary)\n    plt.xlabel(train_df.iloc[i,0])","metadata":{"trusted":true,"id":"txTfuIPoIVYR","outputId":"a4b5240b-329e-42e4-e8fd-62562ba598ed","execution":{"iopub.status.busy":"2026-06-17T12:18:09.067411Z","iopub.execute_input":"2026-06-17T12:18:09.068241Z","iopub.status.idle":"2026-06-17T12:18:09.296796Z","shell.execute_reply.started":"2026-06-17T12:18:09.068213Z","shell.execute_reply":"2026-06-17T12:18:09.295886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_df=train_df.iloc[: , 1:].values/256\nx_test_df=test_df.iloc[: , 0:].values/256","metadata":{"trusted":true,"id":"ljA3fMuXIVYT","execution":{"iopub.status.busy":"2026-06-17T12:18:11.374879Z","iopub.execute_input":"2026-06-17T12:18:11.375480Z","iopub.status.idle":"2026-06-17T12:18:11.543219Z","shell.execute_reply.started":"2026-06-17T12:18:11.375450Z","shell.execute_reply":"2026-06-17T12:18:11.542587Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_df=train_df.iloc[: , 0].values\ny_train_df[2]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-06-17T12:19:40.126795Z","iopub.execute_input":"2026-06-17T12:19:40.127063Z","iopub.status.idle":"2026-06-17T12:19:40.132830Z","shell.execute_reply.started":"2026-06-17T12:19:40.127041Z","shell.execute_reply":"2026-06-17T12:19:40.132059Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 3. Train and Validation Split","metadata":{"id":"NFTriijaIVYW"}},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(x_train_df,y_train_df,test_size=0.2,random_state=42)","metadata":{"trusted":true,"id":"RsrBDDG8IVYW","execution":{"iopub.status.busy":"2026-06-17T12:19:42.818422Z","iopub.execute_input":"2026-06-17T12:19:42.818810Z","iopub.status.idle":"2026-06-17T12:19:43.218715Z","shell.execute_reply.started":"2026-06-17T12:19:42.818776Z","shell.execute_reply":"2026-06-17T12:19:43.217766Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 5. CNN","metadata":{"id":"s3_As0cqIVYX"}},{"cell_type":"code","source":"cnn_model = models.Sequential([\n     layers.Conv2D(filters=32,kernel_size=(3,3),activation='relu',input_shape=(28,28,1)),\n    layers.MaxPooling2D((2,2)),\n    layers.Conv2D(filters=64,kernel_size=(3,3),activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    layers.Flatten(), \n    layers.Dense(64,activation='relu'),              \n    layers.Dense(32,activation='relu'),\n    layers.Dense(16,activation='relu'),\n    layers.Dense(10,activation='softmax'),\n])\n                  ","metadata":{"trusted":true,"id":"bynnxpe_IVYX","outputId":"8eae6343-eda4-4dfc-d663-f3f0d17080d9","execution":{"iopub.status.busy":"2026-06-17T12:40:42.661450Z","iopub.execute_input":"2026-06-17T12:40:42.662182Z","iopub.status.idle":"2026-06-17T12:40:45.011889Z","shell.execute_reply.started":"2026-06-17T12:40:42.662150Z","shell.execute_reply":"2026-06-17T12:40:45.011283Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cnn_model.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\",metrics=['accuracy'])","metadata":{"trusted":true,"id":"r8I1bqcxIVYY","execution":{"iopub.status.busy":"2026-06-17T12:41:41.555956Z","iopub.execute_input":"2026-06-17T12:41:41.556734Z","iopub.status.idle":"2026-06-17T12:41:41.564611Z","shell.execute_reply.started":"2026-06-17T12:41:41.556704Z","shell.execute_reply":"2026-06-17T12:41:41.563813Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = cnn_model.fit(x_train.reshape(-1,28,28,1),\n                       y_train,\n                       epochs=10, batch_size=32,validation_data=(x_test.reshape(-1,28,28,1),y_test))","metadata":{"trusted":true,"id":"SfAqqZfkIVYZ","outputId":"8fe58762-4c78-4546-908f-c6c0e844383f","execution":{"iopub.status.busy":"2026-06-17T12:46:32.766855Z","iopub.execute_input":"2026-06-17T12:46:32.767550Z","iopub.status.idle":"2026-06-17T12:47:11.828894Z","shell.execute_reply.started":"2026-06-17T12:46:32.767502Z","shell.execute_reply":"2026-06-17T12:47:11.828070Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.head()","metadata":{"trusted":true,"id":"zF9oI8WPIVYZ","outputId":"d2a3352c-4962-4ab9-9cf7-10ec4a7f8062","execution":{"iopub.status.busy":"2026-06-17T12:47:35.915174Z","iopub.execute_input":"2026-06-17T12:47:35.915985Z","iopub.status.idle":"2026-06-17T12:47:35.926932Z","shell.execute_reply.started":"2026-06-17T12:47:35.915940Z","shell.execute_reply":"2026-06-17T12:47:35.926223Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history[\"accuracy\"],label=\"Training Acc\")\nplt.plot(history.history[\"val_accuracy\"],label=\"Training Acc\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Accuracy\")\nplt.legend()\nplt.show","metadata":{"trusted":true,"id":"87NpaXGkIVYa","outputId":"009e62da-56e9-4bcf-e8aa-cc8064b316ae","execution":{"iopub.status.busy":"2026-06-17T12:50:21.018047Z","iopub.execute_input":"2026-06-17T12:50:21.018870Z","iopub.status.idle":"2026-06-17T12:50:21.141745Z","shell.execute_reply.started":"2026-06-17T12:50:21.018830Z","shell.execute_reply":"2026-06-17T12:50:21.141015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_loss , test_acc = cnn_model.evaluate(x_test.reshape(-1,28,28,1),y_test)\nprint(test_acc)","metadata":{"trusted":true,"id":"37jwd70bIVYb","outputId":"e1f75873-194e-40e5-9fb6-cf1bef7ed1b1","execution":{"iopub.status.busy":"2026-06-17T12:52:24.780407Z","iopub.execute_input":"2026-06-17T12:52:24.781167Z","iopub.status.idle":"2026-06-17T12:52:25.479212Z","shell.execute_reply.started":"2026-06-17T12:52:24.781135Z","shell.execute_reply":"2026-06-17T12:52:25.478587Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_predict = cnn_model.predict(x_test.reshape(-1,28,28,1))\ny_classes =[np.argmax(i) for i in y_predict]\ny_classes[:20]\n","metadata":{"trusted":true,"id":"QRcc1ZQGIVYc","outputId":"fdc03d87-38c7-4eb4-8390-2320de694b8a","execution":{"iopub.status.busy":"2026-06-17T12:55:52.870685Z","iopub.execute_input":"2026-06-17T12:55:52.871533Z","iopub.status.idle":"2026-06-17T12:55:54.190223Z","shell.execute_reply.started":"2026-06-17T12:55:52.871485Z","shell.execute_reply":"2026-06-17T12:55:54.189550Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"id":"E7-cDMP3IVYd","outputId":"8b34ad61-62a9-45ee-b31b-fd75840c06f3"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"id":"v7p18MgEIVYd","outputId":"4581cf3a-910e-4076-f33f-3b45eb1cfd49"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"id":"DtmY6ssPIVYf","outputId":"b135cade-8f57-4500-8efa-27fc4d19a749"},"outputs":[],"execution_count":null}]}