{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823}],"dockerImageVersionId":31287,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## **Importing necessary libraries and tools along with finding the kaggle directory paths.**","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Input\nfrom tensorflow.keras.losses import SparseCategoricalCrossentropy\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.regularizers import L2\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-03-10T11:00:39.015944Z","iopub.execute_input":"2026-03-10T11:00:39.016621Z","iopub.status.idle":"2026-03-10T11:00:39.417371Z","shell.execute_reply.started":"2026-03-10T11:00:39.016590Z","shell.execute_reply":"2026-03-10T11:00:39.416681Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Taking a look at the data**","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/competitions/digit-recognizer/train.csv')\ntrain.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-10T11:00:40.830767Z","iopub.execute_input":"2026-03-10T11:00:40.831561Z","iopub.status.idle":"2026-03-10T11:00:45.151930Z","shell.execute_reply.started":"2026-03-10T11:00:40.831532Z","shell.execute_reply":"2026-03-10T11:00:45.151186Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train.drop(labels=[\"label\"], axis=1) / 255.0 \ny = train[\"label\"] # The test dataset doesn't have a label column\n# Reshaping is necessary for the Data Augmentation part\nX_reshaped = X.values.reshape(-1, 28, 28, 1)\nX_train, X_val, y_train, y_val = train_test_split(X_reshaped, y, test_size=0.1, random_state=55)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-10T11:00:46.768464Z","iopub.execute_input":"2026-03-10T11:00:46.769145Z","iopub.status.idle":"2026-03-10T11:00:47.334698Z","shell.execute_reply.started":"2026-03-10T11:00:46.769113Z","shell.execute_reply":"2026-03-10T11:00:47.334010Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Data Augmentation and Model Builidng**","metadata":{}},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range=10,  \n    zoom_range=0.1,     \n    width_shift_range=0.1, \n    height_shift_range=0.1\n)\ndatagen.fit(X_train)\nmodel = Sequential([\n    Input(shape=(28, 28, 1)), # OG code used to be Flatten(input_shape=(28, 28, 1)) but in newer versions of Tensorflow, a cleaner version exists\n    Flatten(),\n    Dense(units=512, activation='relu', kernel_regularizer=L2(0.001)),\n    Dense(units=256, activation='relu', kernel_regularizer=L2(0.001)),\n    Dense(units=10, activation='linear')])\n\nmodel.compile(optimizer=Adam(learning_rate=1e-3), \n              loss=SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=64),\n                    epochs=35, \n                    validation_data=(X_val, y_val))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-10T11:12:47.455014Z","iopub.execute_input":"2026-03-10T11:12:47.455527Z","iopub.status.idle":"2026-03-10T11:19:27.074767Z","shell.execute_reply.started":"2026-03-10T11:12:47.455500Z","shell.execute_reply":"2026-03-10T11:19:27.073980Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Plotting a confusion matrix to see where exactly is the model going wrong**","metadata":{}},{"cell_type":"code","source":"val_logits = model.predict(X_val)\nval_preds = np.argmax(val_logits, axis=1)\ncm = confusion_matrix(y_val, val_preds)\nplt.figure(figsize=(10, 8))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', \n            xticklabels=range(10), yticklabels=range(10))\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.title('Confusion Matrix for Digit Recognizer')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-10T11:19:37.459856Z","iopub.execute_input":"2026-03-10T11:19:37.460701Z","iopub.status.idle":"2026-03-10T11:19:38.404817Z","shell.execute_reply.started":"2026-03-10T11:19:37.460669Z","shell.execute_reply":"2026-03-10T11:19:38.404107Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **We observe that the model is getting confused mainly in the 3/8 and 2/7 numbers**","metadata":{}},{"cell_type":"code","source":"def plot_specific_errors(true_label, pred_label, X_val, y_val, val_preds):\n    error_indices = np.where((y_val == true_label) & (val_preds == pred_label))[0]\n    if len(error_indices) == 0:\n        print(f\"No errors found where True={true_label} and Pred={pred_label}\")\n        return\n    plt.figure(figsize=(10, 4))\n    for i, idx in enumerate(error_indices[:5]): # Show first 5 examples\n        plt.subplot(1, 5, i + 1)\n        # Reshape the flattened array back to 28x28 for display\n        plt.imshow(X_val[idx].reshape(28, 28), cmap='gray')\n        plt.title(f\"T:{true_label} P:{pred_label}\")\n        plt.axis('off')\n    plt.show()\n\nprint(\"Investigating 8 vs 3 confusion:\")\nplot_specific_errors(8, 3, X_val, y_val.values, val_preds)\n\nprint(\"Investigating 2 vs 7 confusion:\")\nplot_specific_errors(2, 7, X_val, y_val.values, val_preds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-10T11:10:50.332560Z","iopub.execute_input":"2026-03-10T11:10:50.333572Z","iopub.status.idle":"2026-03-10T11:10:50.724833Z","shell.execute_reply.started":"2026-03-10T11:10:50.333528Z","shell.execute_reply":"2026-03-10T11:10:50.723873Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Submission**","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/competitions/digit-recognizer/test.csv')\nX_test = (test / 255.0).values.reshape(-1, 28, 28, 1)\nlogits_test = model.predict(X_test)\ntest_predictions = np.argmax(logits_test, axis=1)\nimage_ids = np.arange(1, len(test_predictions) + 1)\n\nsubmission = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(test_predictions) + 1),\n    \"Label\": test_predictions\n})\n\nsubmission.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-08T10:49:56.099177Z","iopub.execute_input":"2026-03-08T10:49:56.099429Z","iopub.status.idle":"2026-03-08T10:49:59.791704Z","shell.execute_reply.started":"2026-03-08T10:49:56.099407Z","shell.execute_reply":"2026-03-08T10:49:59.791033Z"}},"outputs":[],"execution_count":null}]}