{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1 align=\"center\"> MNIST Handwritten Digit Classification (Multi Class Classification)</h1>\n\n<img \n    src=\"https://www.kaggle.com/competitions/3004/images/header\"\n    alt=\"\" \n    width=\"300\" \n    height=\"200\" \n    style=\"display: block; margin: 0 auto; border-radius:15px\" \n/>\n\n- MNIST (\"Modified National Institute of Standards and Technology\") is the de facto “hello world” dataset of computer vision. Since its release in 1999, this classic dataset of handwritten images has served as the basis for benchmarking classification algorithms.\n\n- Image classification is a fundamental task in deep learning. It introduces concepts like feature extraction, convolutional neural networks (CNNs), and overfitting.\n  \n- MNIST is a dataset of 70,000 grayscale images of handwritten digits (0 to 9).\n\n- Each image is 28x28 pixels.\n\n- Goal: Build a model to correctly classify these images into the correct digit.\n","metadata":{}},{"cell_type":"markdown","source":"## Basics of Neural Networks & CNNs\n\n### What is a Neural Network?\n\n- Inspired by the brain’s neurons, a neural network consists of layers of nodes (“neurons”).\n- Each neuron applies a weighted sum of inputs + bias and passes it through an activation function.\n\n### What is a Convolutional Neural Network (CNN)?\n- CNNs are designed to process images.\n- They use convolutional layers to detect patterns like edges, shapes, and textures.\n- CNN layers preserve spatial relationships, unlike fully connected layers.\n\n### Key CNN components:\n\n1. Convolutional layers: Apply filters/kernels sliding over the image to create feature maps.\n2. Pooling layers: Reduce spatial size (e.g., max pooling) to lower computation and control overfitting.\n3. Fully connected layers: At the end, they combine extracted features to classify.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%config InlineBackend.figure_format='svg'\n\nimport tensorflow as tf\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n\ntf.config.list_physical_devices('GPU')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:32:12.637733Z","iopub.execute_input":"2025-08-22T06:32:12.637969Z","iopub.status.idle":"2025-08-22T06:32:32.276080Z","shell.execute_reply.started":"2025-08-22T06:32:12.637946Z","shell.execute_reply":"2025-08-22T06:32:32.275417Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---\n\n# Step 1: Exploratory Data Analysis (EDA)\n\n---","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\nprint(f\"Train data shape: {train_data.shape}\")\nprint(f\"Test data shape: {test_data.shape}\")\n\nprint(f\"Train data null count: {train_data.isnull().sum().sum()}\")\nprint(f\"Train data null count: {test_data.isnull().sum().sum()}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:32:32.276868Z","iopub.execute_input":"2025-08-22T06:32:32.277533Z","iopub.status.idle":"2025-08-22T06:32:37.003466Z","shell.execute_reply.started":"2025-08-22T06:32:32.277499Z","shell.execute_reply":"2025-08-22T06:32:37.002744Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:32:37.004945Z","iopub.execute_input":"2025-08-22T06:32:37.005176Z","iopub.status.idle":"2025-08-22T06:32:37.031744Z","shell.execute_reply.started":"2025-08-22T06:32:37.005157Z","shell.execute_reply":"2025-08-22T06:32:37.031001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Pick a sample\nsample = train_data.iloc[0, 1:].values.reshape(28, 28)\n\nplt.figure(figsize=(3,3))\nplt.imshow(sample, cmap='gray')\nplt.title(f\"Label: {train_data.iloc[0, 0]}\")\nplt.axis('off')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:32:37.032532Z","iopub.execute_input":"2025-08-22T06:32:37.032815Z","iopub.status.idle":"2025-08-22T06:32:37.197893Z","shell.execute_reply.started":"2025-08-22T06:32:37.032788Z","shell.execute_reply":"2025-08-22T06:32:37.197244Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Set grid size\nrows, cols = 5, 5\nfig, axes = plt.subplots(rows, cols, figsize=(10,10))\n\n# Flattened pixel data and labels\nimages = train_data.drop(\"label\", axis=1).values\nlabels = train_data[\"label\"].values\n\n# Loop through grid\nfor i in range(rows * cols):\n    ax = axes[i // cols, i % cols]\n    ax.imshow(images[i].reshape(28, 28), cmap='gray')\n    ax.set_title(f\"Label: {labels[i]}\")\n    ax.axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:32:37.198586Z","iopub.execute_input":"2025-08-22T06:32:37.198846Z","iopub.status.idle":"2025-08-22T06:32:38.636862Z","shell.execute_reply.started":"2025-08-22T06:32:37.198821Z","shell.execute_reply":"2025-08-22T06:32:38.636202Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---\n\n# Step 2: Data Preprocessing\n\n---\n","metadata":{}},{"cell_type":"code","source":"# Separate features and labels\nX = train_data.drop(\"label\", axis=1).values\ny = train_data[\"label\"].values\n\n# Normalize pixel values\nX = X / 255.0\ntest_X = test_data.values / 255.0\n\n# Reshape for CNN input\nX = X.reshape(-1, 28, 28, 1)\ntest_X = test_X.reshape(-1, 28, 28, 1)\n\n# One-hot encode labels\ny_cat = to_categorical(y)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:32:38.637800Z","iopub.execute_input":"2025-08-22T06:32:38.638087Z","iopub.status.idle":"2025-08-22T06:32:38.895366Z","shell.execute_reply.started":"2025-08-22T06:32:38.638063Z","shell.execute_reply":"2025-08-22T06:32:38.894703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y_cat, test_size=0.1, random_state=42)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:32:38.896317Z","iopub.execute_input":"2025-08-22T06:32:38.896541Z","iopub.status.idle":"2025-08-22T06:32:39.278938Z","shell.execute_reply.started":"2025-08-22T06:32:38.896522Z","shell.execute_reply":"2025-08-22T06:32:39.278142Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---\n\n# Step 3: Model Training & Evaluation\n\n---","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),\n    MaxPooling2D((2,2)),\n    Conv2D(64, (3,3), activation='relu'),\n    MaxPooling2D((2,2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\nmodel.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n\nmodel.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:32:39.279677Z","iopub.execute_input":"2025-08-22T06:32:39.279872Z","iopub.status.idle":"2025-08-22T06:32:40.776104Z","shell.execute_reply.started":"2025-08-22T06:32:39.279855Z","shell.execute_reply":"2025-08-22T06:32:40.775403Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train,\n    y_train,\n    epochs=20,\n    batch_size=64,\n    validation_data=(X_val, y_val)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:32:40.777937Z","iopub.execute_input":"2025-08-22T06:32:40.778531Z","iopub.status.idle":"2025-08-22T06:33:23.992955Z","shell.execute_reply.started":"2025-08-22T06:32:40.778511Z","shell.execute_reply":"2025-08-22T06:33:23.992392Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot training history\nplt.figure(figsize=(10,4))\n\nplt.subplot(1,2,1)\nplt.plot(history.history['accuracy'], label='train')\nplt.plot(history.history['val_accuracy'], label='val')\nplt.title('Accuracy')\nplt.legend()\n\nplt.subplot(1,2,2)\nplt.plot(history.history['loss'], label='train')\nplt.plot(history.history['val_loss'], label='val')\nplt.title('Loss')\nplt.legend()\n\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:33:23.994077Z","iopub.execute_input":"2025-08-22T06:33:23.994330Z","iopub.status.idle":"2025-08-22T06:33:24.264086Z","shell.execute_reply.started":"2025-08-22T06:33:23.994312Z","shell.execute_reply":"2025-08-22T06:33:24.263289Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---\n\n# Step 4: Test Data Prediction¶\n\n---","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test_X)\npredicted_labels = np.argmax(predictions, axis=1)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:33:24.264948Z","iopub.execute_input":"2025-08-22T06:33:24.265365Z","iopub.status.idle":"2025-08-22T06:33:26.629769Z","shell.execute_reply.started":"2025-08-22T06:33:24.265336Z","shell.execute_reply":"2025-08-22T06:33:26.629179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:33:26.630503Z","iopub.execute_input":"2025-08-22T06:33:26.630807Z","iopub.status.idle":"2025-08-22T06:33:26.651407Z","shell.execute_reply.started":"2025-08-22T06:33:26.630779Z","shell.execute_reply":"2025-08-22T06:33:26.650739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission[\"Label\"] = predicted_labels\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-22T06:33:26.652101Z","iopub.execute_input":"2025-08-22T06:33:26.652382Z","iopub.status.idle":"2025-08-22T06:33:26.685312Z","shell.execute_reply.started":"2025-08-22T06:33:26.652362Z","shell.execute_reply":"2025-08-22T06:33:26.684512Z"}},"outputs":[],"execution_count":null}]}