{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<div style=\"border: 2px solid #4CAF50; padding: 10px; border-radius: 10px; background-color: #e8f5e9; text-align: center; font-size: 18px;\">\n    🎯 <b>Don't Forget to Upvote!</b> 🚀 <br>\n    ❤️ Your support keeps me motivated! 🎉\n</div>\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport random\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"9b4b9c38-faa8-41bf-b16a-cdd22cd3abc4","_cell_guid":"219d4455-141f-4a78-985c-8c1017d8f4a9","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:09.952533Z","iopub.execute_input":"2025-02-01T15:02:09.952759Z","iopub.status.idle":"2025-02-01T15:02:09.961099Z","shell.execute_reply.started":"2025-02-01T15:02:09.952739Z","shell.execute_reply":"2025-02-01T15:02:09.960415Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data adjustment","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"_uuid":"b48050d8-0df9-4f6e-8d79-e1ccd080a600","_cell_guid":"57108e9b-2499-45ed-8b37-d10257dd3dff","trusted":true,"execution":{"iopub.status.busy":"2025-02-01T15:02:09.962257Z","iopub.execute_input":"2025-02-01T15:02:09.962534Z","iopub.status.idle":"2025-02-01T15:02:12.821193Z","shell.execute_reply.started":"2025-02-01T15:02:09.962513Z","shell.execute_reply":"2025-02-01T15:02:12.820502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"_uuid":"04438c0b-c732-41a9-acfc-003e93ab9a8b","_cell_guid":"8a79863b-6e5e-422f-8157-c9fe92f072c5","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:12.822531Z","iopub.execute_input":"2025-02-01T15:02:12.822828Z","iopub.status.idle":"2025-02-01T15:02:12.836135Z","shell.execute_reply.started":"2025-02-01T15:02:12.822791Z","shell.execute_reply":"2025-02-01T15:02:12.835233Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"_uuid":"5a648765-a3db-4ce8-91fc-59dae74e8683","_cell_guid":"0f8aff16-7bd9-4fa1-9f3f-33571061432b","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:12.837657Z","iopub.execute_input":"2025-02-01T15:02:12.837980Z","iopub.status.idle":"2025-02-01T15:02:12.856961Z","shell.execute_reply.started":"2025-02-01T15:02:12.837947Z","shell.execute_reply":"2025-02-01T15:02:12.856240Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train = train[\"label\"]\nx_train = train.drop(\"label\" , axis = 1)","metadata":{"_uuid":"fe56d896-f8cf-47b7-aa1c-6cadf2b1b534","_cell_guid":"b95ae665-1983-4a95-977d-92be635c1a9b","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:12.857565Z","iopub.execute_input":"2025-02-01T15:02:12.857754Z","iopub.status.idle":"2025-02-01T15:02:12.946066Z","shell.execute_reply.started":"2025-02-01T15:02:12.857737Z","shell.execute_reply":"2025-02-01T15:02:12.945073Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Count of different numbers\nsns.countplot(x = y_train.values , palette = \"muted\")\nplt.title(\"Count of different numbers\")\nplt.show()","metadata":{"_uuid":"40fee788-c4dc-40ea-ac1d-4b679342eb21","_cell_guid":"e2d087d8-0f6d-4ab2-b443-7987860c7c8a","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:12.947092Z","iopub.execute_input":"2025-02-01T15:02:12.947372Z","iopub.status.idle":"2025-02-01T15:02:13.113949Z","shell.execute_reply.started":"2025-02-01T15:02:12.947351Z","shell.execute_reply":"2025-02-01T15:02:13.113233Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#some images\nj=1\nfor i in range(0,9):\n    plt.subplot(3,3,j)\n    number = random.randint(0,x_train.shape[0])\n    array = x_train.iloc[number].values.reshape(28,28)\n    plt.imshow(array, cmap = \"gray\" )\n    plt.xlabel(y_train[number])\n    plt.axis(\"off\")\n    j+=1","metadata":{"_uuid":"b98c5b09-fb15-42de-aa61-60c381e10a4c","_cell_guid":"a6a12042-118d-4ce1-ab3f-2174d747be8f","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:13.115714Z","iopub.execute_input":"2025-02-01T15:02:13.115932Z","iopub.status.idle":"2025-02-01T15:02:13.360797Z","shell.execute_reply.started":"2025-02-01T15:02:13.115913Z","shell.execute_reply":"2025-02-01T15:02:13.359903Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#label encoding for softmax activation\nfrom keras.utils import to_categorical\ny_train = to_categorical(y_train , num_classes = 10)","metadata":{"_uuid":"fcd6a043-123a-48ba-9bcc-41dd63797d11","_cell_guid":"b0419721-c2ba-48a4-8eba-7b1de5643966","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:13.362553Z","iopub.execute_input":"2025-02-01T15:02:13.362850Z","iopub.status.idle":"2025-02-01T15:02:13.367336Z","shell.execute_reply.started":"2025-02-01T15:02:13.362824Z","shell.execute_reply":"2025-02-01T15:02:13.366442Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#normalization\nx_train = x_train/255\ntest = test/255","metadata":{"_uuid":"9d97b113-7548-4c72-abf7-e45a0e47a80a","_cell_guid":"6b415063-563c-46ff-b040-e4f0a92a0c9a","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:13.368479Z","iopub.execute_input":"2025-02-01T15:02:13.368829Z","iopub.status.idle":"2025-02-01T15:02:13.503861Z","shell.execute_reply.started":"2025-02-01T15:02:13.368792Z","shell.execute_reply":"2025-02-01T15:02:13.502886Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Convolutional Neural Networks (CNNs) expect a 4D tensor as input because they process images with multiple dimensions:\n\n- Number of samples (batch size): The total number of images.\n- Height: The height (vertical size) of each image.\n- Width: The width (horizontal size) of each image.\n- Channels: The number of color channels (e.g., 1 for grayscale, 3 for RGB).","metadata":{"_uuid":"023cbc73-6ecb-4ea8-8d97-0b84845b4d53","_cell_guid":"af2d3422-7260-4710-a181-e2710f193c84","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"#reshaping\nx_train = x_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\nprint(\"x_train shape: \" , x_train.shape)\nprint(\"test shape: \",test.shape)","metadata":{"_uuid":"aa0287a8-7eb7-402c-b8f5-def3de96e131","_cell_guid":"e8d975a4-0679-4677-a700-3b07e46711e4","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:13.504953Z","iopub.execute_input":"2025-02-01T15:02:13.505323Z","iopub.status.idle":"2025-02-01T15:02:13.511298Z","shell.execute_reply.started":"2025-02-01T15:02:13.505287Z","shell.execute_reply":"2025-02-01T15:02:13.510341Z"},"_kg_hide-output":true,"_kg_hide-input":true,"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Prediction","metadata":{"_uuid":"01a94fac-fa1e-4418-9cec-28e5cc5060e0","_cell_guid":"2bb2df6b-9fee-4dc8-928f-729b22e4980a","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"#split train data\nfrom sklearn.model_selection import train_test_split\nx_train , x_val , y_train , y_val = train_test_split(x_train , y_train , test_size = 0.1 , random_state = 2)","metadata":{"_uuid":"6e454747-361c-4ea9-ad5a-62cf0aaa9863","_cell_guid":"2ae23fad-a648-4c3e-8925-933b1e663ac0","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:13.512306Z","iopub.execute_input":"2025-02-01T15:02:13.512552Z","iopub.status.idle":"2025-02-01T15:02:13.895354Z","shell.execute_reply.started":"2025-02-01T15:02:13.512530Z","shell.execute_reply":"2025-02-01T15:02:13.894332Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.layers import Dense , Conv2D , MaxPool2D , Dropout , Flatten\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nmodel = Sequential()\n\nmodel.add(Conv2D(activation = \"relu\",input_shape = (28,28,1),kernel_size = (5,5) , filters = 8 , padding = \"same\"))\nmodel.add(MaxPool2D(pool_size = (2,2)))\nmodel.add(Conv2D(padding = \"same\",activation = \"relu\",filters = 32 , kernel_size = (5,5)))\nmodel.add(MaxPool2D(pool_size= (2,2)))\nmodel.add(Conv2D(activation = \"relu\",padding = \"same\"  , filters = 64, kernel_size = (4,4)))\nmodel.add(MaxPool2D(pool_size= (2,2),strides = (2,2)))\nmodel.add(Conv2D(activation = \"relu\",padding = \"same\"  , filters = 256, kernel_size = (3,3)))\n#model.add(MaxPool2D(pool_size= (3,3)))\nmodel.add(Flatten())\nmodel.add(Dense(activation = \"relu\" , units = 128))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(activation = \"softmax\" , units = 10))","metadata":{"_uuid":"d32e63d5-a856-4381-98d4-22d9aaa6d5b6","_cell_guid":"34e1937a-61d9-4a37-95c2-36e2ca9ff47f","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:13.896313Z","iopub.execute_input":"2025-02-01T15:02:13.896631Z","iopub.status.idle":"2025-02-01T15:02:13.989304Z","shell.execute_reply.started":"2025-02-01T15:02:13.896601Z","shell.execute_reply":"2025-02-01T15:02:13.988676Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"adamoptimizer = Adam(learning_rate = 0.001,beta_1 = 0.9 , beta_2 = 0.999)\nmodel.compile(optimizer = adamoptimizer , loss = \"categorical_crossentropy\" , metrics = [\"accuracy\"])","metadata":{"_uuid":"b7d08f9f-6ffd-4b82-b88c-5f722deed16e","_cell_guid":"ea18600c-52b1-405c-8538-8a9eca18a7ab","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:13.990016Z","iopub.execute_input":"2025-02-01T15:02:13.990260Z","iopub.status.idle":"2025-02-01T15:02:13.998052Z","shell.execute_reply.started":"2025-02-01T15:02:13.990241Z","shell.execute_reply":"2025-02-01T15:02:13.997223Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range = 30,\n    width_shift_range = 0.2,\n    height_shift_range = 0.2,\n    shear_range = 0.3,\n    zoom_range = 0.2\n    )","metadata":{"_uuid":"68873b20-ca6d-4e5c-94ae-1c66364724e3","_cell_guid":"1c8ea897-b0fc-44d1-b495-070b64023617","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:13.999004Z","iopub.execute_input":"2025-02-01T15:02:13.999340Z","iopub.status.idle":"2025-02-01T15:02:14.012025Z","shell.execute_reply.started":"2025-02-01T15:02:13.999317Z","shell.execute_reply":"2025-02-01T15:02:14.011191Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_val.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-01T15:02:14.013021Z","iopub.execute_input":"2025-02-01T15:02:14.013320Z","iopub.status.idle":"2025-02-01T15:02:14.026539Z","shell.execute_reply.started":"2025-02-01T15:02:14.013283Z","shell.execute_reply":"2025-02-01T15:02:14.025578Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(datagen.flow(x_train , y_train , batch_size = 250) ,\n                    epochs = 20 , \n                    validation_data = (x_val , y_val),\n                    steps_per_epoch = x_train.shape[0] // 250\n                )","metadata":{"_uuid":"df718ba3-17bf-4877-a0d6-f96ce39ee45e","_cell_guid":"eed234e2-f614-4c5c-943a-022693a70fe0","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:02:14.027583Z","iopub.execute_input":"2025-02-01T15:02:14.027910Z","iopub.status.idle":"2025-02-01T15:04:24.601056Z","shell.execute_reply.started":"2025-02-01T15:02:14.027877Z","shell.execute_reply":"2025-02-01T15:04:24.600377Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history.history[\"accuracy\"]","metadata":{"_uuid":"cc44c375-6eff-4d4c-9e4e-747c7a87403e","_cell_guid":"9b1141e2-d3d5-4027-abee-2e91bc2d23a4","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-02-01T15:04:24.601982Z","iopub.execute_input":"2025-02-01T15:04:24.602308Z","iopub.status.idle":"2025-02-01T15:04:24.607557Z","shell.execute_reply.started":"2025-02-01T15:04:24.602276Z","shell.execute_reply":"2025-02-01T15:04:24.606677Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#prediction with validation data\npredictions = model.predict(x_val)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-01T15:04:24.608454Z","iopub.execute_input":"2025-02-01T15:04:24.608681Z","iopub.status.idle":"2025-02-01T15:04:25.269504Z","shell.execute_reply.started":"2025-02-01T15:04:24.608661Z","shell.execute_reply":"2025-02-01T15:04:25.268794Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#converting\npredictions = [i.argmax() for i in predictions]\ny_val_not_categorical = [i.argmax() for i in y_val]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-01T15:04:25.271985Z","iopub.execute_input":"2025-02-01T15:04:25.272278Z","iopub.status.idle":"2025-02-01T15:04:25.282016Z","shell.execute_reply.started":"2025-02-01T15:04:25.272257Z","shell.execute_reply":"2025-02-01T15:04:25.281243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"wrongpredictionindexes = []\ntruepredictionindexes = []\nfor i in range(0,len(y_val_not_categorical)):\n    if(predictions[i] == y_val_not_categorical[i]):\n        truepredictionindexes.append(i)\n    else:\n        wrongpredictionindexes.append(i)\n        ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-01T15:04:25.283139Z","iopub.execute_input":"2025-02-01T15:04:25.283456Z","iopub.status.idle":"2025-02-01T15:04:25.290985Z","shell.execute_reply.started":"2025-02-01T15:04:25.283433Z","shell.execute_reply":"2025-02-01T15:04:25.290084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#some wrong predicted images\nj=1\nplt.figure(figsize=(10,10))\nfor i in wrongpredictionindexes[10:19]:\n    plt.subplot(3,3,j)\n    plt.imshow(x_val[i],cmap = \"gray\")\n    plt.title(f\"real value : {y_val_not_categorical[i]} predicted : {predictions[i]}\")\n    plt.axis(\"off\")\n    j+=1\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-01T15:04:44.645815Z","iopub.execute_input":"2025-02-01T15:04:44.646226Z","iopub.status.idle":"2025-02-01T15:04:45.606644Z","shell.execute_reply.started":"2025-02-01T15:04:44.646190Z","shell.execute_reply":"2025-02-01T15:04:45.605720Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Train and validation accuracy change graph\ntrain_acc = history.history[\"accuracy\"]\nval_acc = history.history[\"val_accuracy\"]\nplt.figure(figsize=(15, 5), facecolor='lightgray')\nsns.lineplot(train_acc , label = \"train accuracy\")\nsns.lineplot(val_acc , color = \"orange\" , label = \"validation accuracy\")\nplt.grid(True)\nplt.legend()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-01T15:04:25.954110Z","iopub.execute_input":"2025-02-01T15:04:25.954469Z","iopub.status.idle":"2025-02-01T15:04:26.197657Z","shell.execute_reply.started":"2025-02-01T15:04:25.954435Z","shell.execute_reply":"2025-02-01T15:04:26.196869Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#prediction with test data\ntest_predictions = [i.argmax() for i in model.predict(test)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-01T15:11:06.004357Z","iopub.execute_input":"2025-02-01T15:11:06.004665Z","iopub.status.idle":"2025-02-01T15:11:07.747693Z","shell.execute_reply.started":"2025-02-01T15:11:06.004643Z","shell.execute_reply":"2025-02-01T15:11:07.746895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsample_submission[\"Label\"] = test_predictions\nsample_submission.to_csv(\"submission.csv\" , index = False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-01T15:12:29.560973Z","iopub.execute_input":"2025-02-01T15:12:29.561349Z","iopub.status.idle":"2025-02-01T15:12:29.636426Z","shell.execute_reply.started":"2025-02-01T15:12:29.561321Z","shell.execute_reply":"2025-02-01T15:12:29.635496Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"border: 3px solid #4CAF50; padding: 15px; border-radius: 10px; background-color: #E8F5E9; text-align: center; font-size: 18px;\">\n    🎉 <b>That’s a wrap! 🚀</b> <br>  \n    🔍 Keep exploring, keep learning, and keep innovating! 💡 <br>  \n    📢 Have thoughts or feedback? Drop a comment below! 💬 <br>  \n    🌟 Wishing you success on your Kaggle journey! ✨  \n</div>\n","metadata":{}}]}