{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"none","dataSources":[{"sourceId":16017,"databundleVersionId":708189,"sourceType":"competition"}],"dockerImageVersionId":31192,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"09bb3568-390d-4fcc-8b7b-003ff0850be8","cell_type":"markdown","source":"<div align='center'><h1>Kannada MNIST</div>","metadata":{}},{"id":"7ef9fb72-c048-465b-8375-ad5a698fbebe","cell_type":"code","source":"import tensorflow as tf\nimport google.protobuf\n\nprint(\"TensorFlow version:\", tf.__version__)\nprint(\"Protobuf version:\", google.protobuf.__version__)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:24:53.496322Z","iopub.execute_input":"2025-12-26T13:24:53.496818Z","iopub.status.idle":"2025-12-26T13:25:18.540749Z","shell.execute_reply.started":"2025-12-26T13:24:53.496786Z","shell.execute_reply":"2025-12-26T13:25:18.539776Z"}},"outputs":[],"execution_count":null},{"id":"8ea0a810-f85e-4b52-94f3-43c0184bc87d","cell_type":"markdown","source":"<h3>Library</h3>","metadata":{}},{"id":"6490c247-f214-4406-b51f-4c3f2e742c03","cell_type":"code","source":"#Importing Libraries\n\nimport os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix\n!pip install protobuf==3.20.* --quiet\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:18.541785Z","iopub.execute_input":"2025-12-26T13:25:18.542468Z","iopub.status.idle":"2025-12-26T13:25:24.80553Z","shell.execute_reply.started":"2025-12-26T13:25:18.542442Z","shell.execute_reply":"2025-12-26T13:25:24.804354Z"}},"outputs":[],"execution_count":null},{"id":"a2252510-7d3b-4349-96c6-4fe4133297f0","cell_type":"markdown","source":"<h3>Loading the Dataset</h3>","metadata":{}},{"id":"250f0f27-ac2a-4567-852f-d1066ba74e14","cell_type":"code","source":"# Loading data\n\ntrainka= pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntestka= pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:24.808747Z","iopub.execute_input":"2025-12-26T13:25:24.809103Z","iopub.status.idle":"2025-12-26T13:25:29.196823Z","shell.execute_reply.started":"2025-12-26T13:25:24.809069Z","shell.execute_reply":"2025-12-26T13:25:29.195828Z"}},"outputs":[],"execution_count":null},{"id":"fad89e65-23ca-4508-b18c-5435f98b765f","cell_type":"markdown","source":"<h4>Dataset Inspection</h4>","metadata":{}},{"id":"b1213359-b7fb-4c28-a376-98a0bf7945d9","cell_type":"code","source":"trainka.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:29.198033Z","iopub.execute_input":"2025-12-26T13:25:29.19896Z","iopub.status.idle":"2025-12-26T13:25:29.234403Z","shell.execute_reply.started":"2025-12-26T13:25:29.198921Z","shell.execute_reply":"2025-12-26T13:25:29.233296Z"}},"outputs":[],"execution_count":null},{"id":"8e9f9769-10c1-4fb6-9f1e-828e36c743df","cell_type":"code","source":"testka.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:29.235408Z","iopub.execute_input":"2025-12-26T13:25:29.235732Z","iopub.status.idle":"2025-12-26T13:25:29.276104Z","shell.execute_reply.started":"2025-12-26T13:25:29.235702Z","shell.execute_reply":"2025-12-26T13:25:29.274641Z"}},"outputs":[],"execution_count":null},{"id":"515d80a8-ca9b-475e-8e8c-322ea88558f8","cell_type":"code","source":"trainka.describe()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:29.277428Z","iopub.execute_input":"2025-12-26T13:25:29.277843Z","iopub.status.idle":"2025-12-26T13:25:31.646138Z","shell.execute_reply.started":"2025-12-26T13:25:29.277814Z","shell.execute_reply":"2025-12-26T13:25:31.644973Z"}},"outputs":[],"execution_count":null},{"id":"80f0adf7-9c27-42e7-aa05-3dda75b740a1","cell_type":"code","source":"trainka.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:31.647203Z","iopub.execute_input":"2025-12-26T13:25:31.647501Z","iopub.status.idle":"2025-12-26T13:25:31.695271Z","shell.execute_reply.started":"2025-12-26T13:25:31.647477Z","shell.execute_reply":"2025-12-26T13:25:31.692766Z"}},"outputs":[],"execution_count":null},{"id":"efe6a37d-7268-4c0e-8490-06c0903e87c3","cell_type":"code","source":"trainka.nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:31.696574Z","iopub.execute_input":"2025-12-26T13:25:31.69708Z","iopub.status.idle":"2025-12-26T13:25:32.159939Z","shell.execute_reply.started":"2025-12-26T13:25:31.69684Z","shell.execute_reply":"2025-12-26T13:25:32.158471Z"}},"outputs":[],"execution_count":null},{"id":"ff37e0c7-1f1e-42b4-ab79-b76905975857","cell_type":"code","source":"testka.nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:32.163793Z","iopub.execute_input":"2025-12-26T13:25:32.164234Z","iopub.status.idle":"2025-12-26T13:25:32.258963Z","shell.execute_reply.started":"2025-12-26T13:25:32.164206Z","shell.execute_reply":"2025-12-26T13:25:32.258019Z"}},"outputs":[],"execution_count":null},{"id":"4dba629f-6452-42d4-a20e-d2c6d9cf04b1","cell_type":"markdown","source":"<h3>Data Processing</h3>","metadata":{}},{"id":"4f8761c0-60a5-47ee-8a98-017471bb87e4","cell_type":"markdown","source":"<h4>Normalize pixel values to the range [0, 1]</h4>","metadata":{}},{"id":"857fd7f4-3247-4191-b22a-ca8317f4ada3","cell_type":"code","source":"X = trainka.drop(columns=[\"label\"]).to_numpy(dtype=\"float32\") / 255.0\ny = trainka[\"label\"].to_numpy(dtype=\"int64\")\n\nX_test = testka.drop(columns=[\"id\"]).to_numpy(dtype=\"float32\") / 255.0\ntest_ids = testka[\"id\"].to_numpy()\n\nprint(X.min(), X.max())  # should be 0.0 1.0\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:32.259838Z","iopub.execute_input":"2025-12-26T13:25:32.260119Z","iopub.status.idle":"2025-12-26T13:25:32.670271Z","shell.execute_reply.started":"2025-12-26T13:25:32.260098Z","shell.execute_reply":"2025-12-26T13:25:32.669261Z"}},"outputs":[],"execution_count":null},{"id":"727e1bca-3218-4e32-938c-e77294d98bce","cell_type":"markdown","source":"<h4>Train, Val, Split</h4>","metadata":{}},{"id":"39ad6347-9796-4a48-a8a7-2697414f98dd","cell_type":"code","source":"# split into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(\n    X, y, test_size=0.20, random_state=42, stratify=y\n)\n\nprint(X_train.shape, X_val.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:32.671308Z","iopub.execute_input":"2025-12-26T13:25:32.671649Z","iopub.status.idle":"2025-12-26T13:25:33.241939Z","shell.execute_reply.started":"2025-12-26T13:25:32.671615Z","shell.execute_reply":"2025-12-26T13:25:33.240706Z"}},"outputs":[],"execution_count":null},{"id":"7adaa952-4fd9-4eee-9501-a0c772a75df1","cell_type":"markdown","source":"<h3>Build NN</h3>","metadata":{}},{"id":"8effc378-6208-4f5b-aba3-0ac4e1271724","cell_type":"markdown","source":"At least one hidden layer: No CNN and other derivatives. Use ReLU as the activation function for the hidden layers and softmax for the output layer. ","metadata":{}},{"id":"1adc9689-11de-4d0c-b375-dbec7c359f57","cell_type":"code","source":"model = keras.Sequential([\n    layers.Input(shape=(784,)),                 # input: 784 pixels (28x28)\n\n    layers.Dense(256, activation=\"relu\"),       # hidden layer 1  ReLU activation \n    layers.Dense(128, activation=\"relu\"),       # hidden layer 2  ReLU activation \n    layers.Dense(10, activation=\"softmax\")      # output layer softmax for 10 classes\n])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:33.243258Z","iopub.execute_input":"2025-12-26T13:25:33.243518Z","iopub.status.idle":"2025-12-26T13:25:33.355433Z","shell.execute_reply.started":"2025-12-26T13:25:33.243499Z","shell.execute_reply":"2025-12-26T13:25:33.353918Z"}},"outputs":[],"execution_count":null},{"id":"02ed18f7-d548-4aad-901c-548e416b2370","cell_type":"code","source":"model.summary() # looking at frame","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:33.356544Z","iopub.execute_input":"2025-12-26T13:25:33.356828Z","iopub.status.idle":"2025-12-26T13:25:33.379877Z","shell.execute_reply.started":"2025-12-26T13:25:33.356806Z","shell.execute_reply":"2025-12-26T13:25:33.37833Z"}},"outputs":[],"execution_count":null},{"id":"39fb7690-3348-4137-9d00-a748b2ded6d5","cell_type":"markdown","source":"<h3>Train Model</h3>","metadata":{}},{"id":"4dbe58a0-2043-4161-a1a7-a86a9ac557b3","cell_type":"markdown","source":"<h4>Compile the model</h4>","metadata":{}},{"id":"4ad504c4-b773-41c7-a021-a40aa803b101","cell_type":"code","source":"\nmodel.compile(\n    optimizer=keras.optimizers.Adam(learning_rate=1e-3),\n    loss=\"sparse_categorical_crossentropy\",\n    metrics=[\"accuracy\"]\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:33.381259Z","iopub.execute_input":"2025-12-26T13:25:33.381617Z","iopub.status.idle":"2025-12-26T13:25:33.404578Z","shell.execute_reply.started":"2025-12-26T13:25:33.381595Z","shell.execute_reply":"2025-12-26T13:25:33.403369Z"}},"outputs":[],"execution_count":null},{"id":"07614b78-7a58-487c-b0d1-0baaca889474","cell_type":"markdown","source":"<h4>Train and monitor(loss and accuracy)</h4>","metadata":{}},{"id":"a3fe134e-5256-4466-80c4-f2b6fb6a6bdb","cell_type":"code","source":"callbacks = [\n    keras.callbacks.EarlyStopping(monitor=\"val_loss\", patience=3, restore_best_weights=True),\n    keras.callbacks.ReduceLROnPlateau(monitor=\"val_loss\", factor=0.5, patience=2, min_lr=1e-5)\n]\n\nhistory = model.fit(\n    X_train, y_train,\n    validation_data=(X_val, y_val),\n    epochs=20,\n    batch_size=256,\n    callbacks=callbacks,\n    verbose=1\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:33.40634Z","iopub.execute_input":"2025-12-26T13:25:33.406719Z","iopub.status.idle":"2025-12-26T13:25:59.685978Z","shell.execute_reply.started":"2025-12-26T13:25:33.406669Z","shell.execute_reply":"2025-12-26T13:25:59.682816Z"}},"outputs":[],"execution_count":null},{"id":"7efabbab-ab3c-46b7-8bee-e722f723004e","cell_type":"markdown","source":"<h4>Evaluate for Loss and Accuracy</h4>","metadata":{}},{"id":"f133b1d1-13a0-44d7-abce-1bc0989a62e4","cell_type":"code","source":"val_loss, val_acc = model.evaluate(X_val, y_val, verbose=0)\nprint(\"Validation loss:\", round(val_loss, 4))\nprint(\"Validation accuracy:\", round(val_acc, 4))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:25:59.687926Z","iopub.execute_input":"2025-12-26T13:25:59.688343Z","iopub.status.idle":"2025-12-26T13:26:00.892178Z","shell.execute_reply.started":"2025-12-26T13:25:59.688307Z","shell.execute_reply":"2025-12-26T13:26:00.890711Z"}},"outputs":[],"execution_count":null},{"id":"6f1fc146-2f1a-4e66-b02f-2b4f35ef5867","cell_type":"markdown","source":"","metadata":{}},{"id":"912291b5-4697-4484-9be9-e78b246003dd","cell_type":"markdown","source":"<h4>Classification Matrix and Confusion Matrix</h4>","metadata":{}},{"id":"108d50d3-973b-4b0c-a51a-f1168525b9ce","cell_type":"code","source":"y_val_pred = np.argmax(model.predict(X_val, verbose=0), axis=1)\nprint(\"\\nClassification report (validation):\")\nprint(classification_report(y_val, y_val_pred))\n\ncm = confusion_matrix(y_val, y_val_pred)\nprint(\"Confusion matrix:\\n\", cm)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:26:00.89326Z","iopub.execute_input":"2025-12-26T13:26:00.893536Z","iopub.status.idle":"2025-12-26T13:26:02.020444Z","shell.execute_reply.started":"2025-12-26T13:26:00.893516Z","shell.execute_reply":"2025-12-26T13:26:02.019356Z"}},"outputs":[],"execution_count":null},{"id":"aee2811b-137f-4db7-a884-13f07b402339","cell_type":"markdown","source":"","metadata":{}},{"id":"26d74015-1219-4b6e-bf1a-610e316a6032","cell_type":"markdown","source":"<h4>Loss and Accuracy Plots</h4>","metadata":{}},{"id":"86d27a28-9669-4f84-80d1-9a6d6b5fde8d","cell_type":"code","source":"#Plot training curves (loss + accuracy)\nplt.figure()\nplt.plot(history.history[\"loss\"], label=\"train_loss\")\nplt.plot(history.history[\"val_loss\"], label=\"val_loss\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.legend()\nplt.title(\"Loss During Training\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:26:02.021648Z","iopub.execute_input":"2025-12-26T13:26:02.022045Z","iopub.status.idle":"2025-12-26T13:26:02.373625Z","shell.execute_reply.started":"2025-12-26T13:26:02.022012Z","shell.execute_reply":"2025-12-26T13:26:02.372307Z"}},"outputs":[],"execution_count":null},{"id":"d6f6ecb8-867c-449f-8511-af190ee6fe22","cell_type":"code","source":"plt.figure()\nplt.plot(history.history[\"accuracy\"], label=\"train_acc\")\nplt.plot(history.history[\"val_accuracy\"], label=\"val_acc\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\nplt.legend()\nplt.title(\"Accuracy During Training\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:26:02.375593Z","iopub.execute_input":"2025-12-26T13:26:02.376826Z","iopub.status.idle":"2025-12-26T13:26:02.647611Z","shell.execute_reply.started":"2025-12-26T13:26:02.376789Z","shell.execute_reply":"2025-12-26T13:26:02.646285Z"}},"outputs":[],"execution_count":null},{"id":"f1429d9d-e691-4517-87fd-9e080d6f8685","cell_type":"markdown","source":"<h3>Kaggle Submission</h3>","metadata":{}},{"id":"33b51615-caa1-4761-8f86-d4f64bb7d44f","cell_type":"code","source":"# predict probabilities\ntest_probs = model.predict(X_test, verbose=0)\ntest_pred  = np.argmax(test_probs, axis=1)\n\n# build submission in required format\nKannada = pd.DataFrame({\n    \"id\": test_ids,\n    \"label\": test_pred\n})\n\nKannada.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T13:26:02.648577Z","iopub.execute_input":"2025-12-26T13:26:02.648936Z","iopub.status.idle":"2025-12-26T13:26:03.219537Z","shell.execute_reply.started":"2025-12-26T13:26:02.648896Z","shell.execute_reply":"2025-12-26T13:26:03.218227Z"}},"outputs":[],"execution_count":null},{"id":"ff8bf634-c767-403f-90be-bf945c6894ee","cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"id":"02fd94d6-6d83-4908-9879-92daaf075fe5","cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}