{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","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":31193,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## OverView\n\nThe Digit Recognizer competition uses the MNIST handwritten digit dataset, considered the classic beginner benchmark for computer vision.\n\n- It contains grayscale images of handwritten digits (0–9)\n\n- Each image is 28×28 pixels\n\n- Each pixel value represents intensity from 0 (white) to 255 (black)\n\nThis competition is designed as a learning playground rather than a competitive leaderboard challenge.","metadata":{}},{"cell_type":"markdown","source":"## Objective\nThe primary goal is to correctly identify which digit (0–9) appears in each provided image.\n\nThis means:\n\n- you are given thousands of pixel features per image\n\n- you must learn mapping from pixel patterns → digit identity\n\n- then predict labels for unseen digit images","metadata":{}},{"cell_type":"markdown","source":"## Load the Dataset\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n\n# Load the CSV files\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# Display first few rows of training data\ntrain.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:01:15.414165Z","iopub.execute_input":"2025-12-09T05:01:15.414813Z","iopub.status.idle":"2025-12-09T05:01:19.166262Z","shell.execute_reply.started":"2025-12-09T05:01:15.414787Z","shell.execute_reply":"2025-12-09T05:01:19.16557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape, test.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:01:26.535368Z","iopub.execute_input":"2025-12-09T05:01:26.535607Z","iopub.status.idle":"2025-12-09T05:01:26.540931Z","shell.execute_reply.started":"2025-12-09T05:01:26.53559Z","shell.execute_reply":"2025-12-09T05:01:26.540174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.columns[0]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:01:37.024362Z","iopub.execute_input":"2025-12-09T05:01:37.025101Z","iopub.status.idle":"2025-12-09T05:01:37.029759Z","shell.execute_reply.started":"2025-12-09T05:01:37.025055Z","shell.execute_reply":"2025-12-09T05:01:37.02914Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.iloc[0, 1:10]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:01:46.222378Z","iopub.execute_input":"2025-12-09T05:01:46.223058Z","iopub.status.idle":"2025-12-09T05:01:46.228771Z","shell.execute_reply.started":"2025-12-09T05:01:46.223037Z","shell.execute_reply":"2025-12-09T05:01:46.228247Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Separate Features and Labels\n","metadata":{}},{"cell_type":"code","source":"# Separate features and labels from training data\nX = train.drop('label', axis=1)  # features\ny = train['label']               # labels\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:03:17.77526Z","iopub.execute_input":"2025-12-09T05:03:17.775945Z","iopub.status.idle":"2025-12-09T05:03:17.867368Z","shell.execute_reply.started":"2025-12-09T05:03:17.775917Z","shell.execute_reply":"2025-12-09T05:03:17.866703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.shape, y.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:03:24.430763Z","iopub.execute_input":"2025-12-09T05:03:24.431336Z","iopub.status.idle":"2025-12-09T05:03:24.436409Z","shell.execute_reply.started":"2025-12-09T05:03:24.431312Z","shell.execute_reply":"2025-12-09T05:03:24.435594Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Normalize Pixel Values\n\n**Why Normalize?**\n\nPixel intensities range from 0 to 255.\n\nNeural networks train better when input values are small and consistent, typically 0 to 1.\n\nBenefits:\n\n- Faster convergence\n\n- Higher accuracy\n\n- Prevents exploding gradients\n\n- Reduces noise effect","metadata":{}},{"cell_type":"code","source":"# Normalize pixel values to range 0-1\nX = X / 255.0\ntest = test / 255.0\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:04:32.735956Z","iopub.execute_input":"2025-12-09T05:04:32.736381Z","iopub.status.idle":"2025-12-09T05:04:32.836516Z","shell.execute_reply.started":"2025-12-09T05:04:32.736354Z","shell.execute_reply":"2025-12-09T05:04:32.83583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.describe().T.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:04:38.089866Z","iopub.execute_input":"2025-12-09T05:04:38.090428Z","iopub.status.idle":"2025-12-09T05:04:40.252864Z","shell.execute_reply.started":"2025-12-09T05:04:38.090387Z","shell.execute_reply":"2025-12-09T05:04:40.252196Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Reshape Data into Image Format\nRight now:\n\n- Images are flattened into 784 columns (1D format)\n\nNeural networks (CNNs) expect:\n\n- 2D spatial structure like actual images","metadata":{}},{"cell_type":"code","source":"X = X.values.reshape(-1, 28, 28, 1)\ntest = test.values.reshape(-1, 28, 28, 1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:05:27.085122Z","iopub.execute_input":"2025-12-09T05:05:27.085786Z","iopub.status.idle":"2025-12-09T05:05:27.089881Z","shell.execute_reply.started":"2025-12-09T05:05:27.085757Z","shell.execute_reply":"2025-12-09T05:05:27.089134Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.imshow(X[0].reshape(28, 28), cmap='gray')\nplt.title(\"First Image After Reshape\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:05:33.113378Z","iopub.execute_input":"2025-12-09T05:05:33.114101Z","iopub.status.idle":"2025-12-09T05:05:33.343953Z","shell.execute_reply.started":"2025-12-09T05:05:33.114054Z","shell.execute_reply":"2025-12-09T05:05:33.343335Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Train–Validation Split","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(\n    X, y, test_size=0.1, random_state=42\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:07:07.786259Z","iopub.execute_input":"2025-12-09T05:07:07.786818Z","iopub.status.idle":"2025-12-09T05:07:08.766184Z","shell.execute_reply.started":"2025-12-09T05:07:07.786791Z","shell.execute_reply":"2025-12-09T05:07:08.765353Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape, X_val.shape\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:07:14.415038Z","iopub.execute_input":"2025-12-09T05:07:14.415791Z","iopub.status.idle":"2025-12-09T05:07:14.421038Z","shell.execute_reply.started":"2025-12-09T05:07:14.415768Z","shell.execute_reply":"2025-12-09T05:07:14.420287Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Build the Model (CNN Architecture)","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n\nmodel = Sequential([\n    Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),\n    MaxPooling2D(2,2),\n    \n    Conv2D(64, (3,3), activation='relu'),\n    MaxPooling2D(2,2),\n    \n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.3),\n    Dense(10, activation='softmax')\n])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:07:43.69112Z","iopub.execute_input":"2025-12-09T05:07:43.691422Z","iopub.status.idle":"2025-12-09T05:08:02.554959Z","shell.execute_reply.started":"2025-12-09T05:07:43.691403Z","shell.execute_reply":"2025-12-09T05:08:02.554316Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:08:17.471152Z","iopub.execute_input":"2025-12-09T05:08:17.472074Z","iopub.status.idle":"2025-12-09T05:08:17.485325Z","shell.execute_reply.started":"2025-12-09T05:08:17.472049Z","shell.execute_reply":"2025-12-09T05:08:17.484559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train, y_train,\n    epochs=10,\n    batch_size=64,\n    validation_data=(X_val, y_val)\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:08:32.846299Z","iopub.execute_input":"2025-12-09T05:08:32.846858Z","iopub.status.idle":"2025-12-09T05:08:59.712816Z","shell.execute_reply.started":"2025-12-09T05:08:32.846835Z","shell.execute_reply":"2025-12-09T05:08:59.712169Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_loss, val_acc = model.evaluate(X_val, y_val)\nprint(f\"Validation Accuracy: {val_acc}\")\nprint(f\"Validation Loss: {val_loss}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:09:02.975713Z","iopub.execute_input":"2025-12-09T05:09:02.976487Z","iopub.status.idle":"2025-12-09T05:09:04.333732Z","shell.execute_reply.started":"2025-12-09T05:09:02.97646Z","shell.execute_reply":"2025-12-09T05:09:04.333063Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Predict on Test Data","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:09:30.043146Z","iopub.execute_input":"2025-12-09T05:09:30.043816Z","iopub.status.idle":"2025-12-09T05:09:32.30094Z","shell.execute_reply.started":"2025-12-09T05:09:30.043792Z","shell.execute_reply":"2025-12-09T05:09:32.300103Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_labels = predictions.argmax(axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:09:46.047053Z","iopub.execute_input":"2025-12-09T05:09:46.047737Z","iopub.status.idle":"2025-12-09T05:09:46.052653Z","shell.execute_reply.started":"2025-12-09T05:09:46.047711Z","shell.execute_reply":"2025-12-09T05:09:46.052028Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\nsubmission = pd.DataFrame({\n    \"ImageId\": list(range(1, len(pred_labels) + 1)),\n    \"Label\": pred_labels\n})\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:10:18.652272Z","iopub.execute_input":"2025-12-09T05:10:18.652601Z","iopub.status.idle":"2025-12-09T05:10:18.665359Z","shell.execute_reply.started":"2025-12-09T05:10:18.65258Z","shell.execute_reply":"2025-12-09T05:10:18.664407Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-09T05:10:25.50629Z","iopub.execute_input":"2025-12-09T05:10:25.506843Z","iopub.status.idle":"2025-12-09T05:10:25.534769Z","shell.execute_reply.started":"2025-12-09T05:10:25.506821Z","shell.execute_reply":"2025-12-09T05:10:25.534057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}