{"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":"none","dataSources":[{"sourceId":16017,"databundleVersionId":708189,"sourceType":"competition"},{"sourceId":12821670,"sourceType":"datasetVersion","datasetId":8108034}],"dockerImageVersionId":31089,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:53.154601Z","iopub.execute_input":"2025-08-21T01:03:53.154907Z","iopub.status.idle":"2025-08-21T01:03:53.163433Z","shell.execute_reply.started":"2025-08-21T01:03:53.154887Z","shell.execute_reply":"2025-08-21T01:03:53.162337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nprint(os.listdir(\"/kaggle/working/\"))  # List all files in /kaggle/working/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:53.165354Z","iopub.execute_input":"2025-08-21T01:03:53.165687Z","iopub.status.idle":"2025-08-21T01:03:53.193055Z","shell.execute_reply.started":"2025-08-21T01:03:53.165662Z","shell.execute_reply":"2025-08-21T01:03:53.191954Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    print(dirname)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:53.194314Z","iopub.execute_input":"2025-08-21T01:03:53.194625Z","iopub.status.idle":"2025-08-21T01:03:53.235796Z","shell.execute_reply.started":"2025-08-21T01:03:53.194602Z","shell.execute_reply":"2025-08-21T01:03:53.234541Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Import packages and data\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:53.238284Z","iopub.execute_input":"2025-08-21T01:03:53.238585Z","iopub.status.idle":"2025-08-21T01:03:53.259308Z","shell.execute_reply.started":"2025-08-21T01:03:53.238563Z","shell.execute_reply":"2025-08-21T01:03:53.258216Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:53.260576Z","iopub.execute_input":"2025-08-21T01:03:53.261406Z","iopub.status.idle":"2025-08-21T01:03:56.922242Z","shell.execute_reply.started":"2025-08-21T01:03:53.261378Z","shell.execute_reply":"2025-08-21T01:03:56.920913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df= pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:56.923792Z","iopub.execute_input":"2025-08-21T01:03:56.924231Z","iopub.status.idle":"2025-08-21T01:03:57.218329Z","shell.execute_reply.started":"2025-08-21T01:03:56.924206Z","shell.execute_reply":"2025-08-21T01:03:57.216892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:57.219514Z","iopub.execute_input":"2025-08-21T01:03:57.219872Z","iopub.status.idle":"2025-08-21T01:03:57.238610Z","shell.execute_reply.started":"2025-08-21T01:03:57.219811Z","shell.execute_reply":"2025-08-21T01:03:57.237265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Image preprocessing \nlabels = df['label'].values\nimages = df.drop(columns=['label'], axis=1).values\n\nprint(\"Images Shape: \", images.shape)\nprint(\"Label Shape: \", labels.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:57.239759Z","iopub.execute_input":"2025-08-21T01:03:57.240112Z","iopub.status.idle":"2025-08-21T01:03:57.480460Z","shell.execute_reply.started":"2025-08-21T01:03:57.240088Z","shell.execute_reply":"2025-08-21T01:03:57.479334Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Test Image preprocessing \ntest_id = test_df['id'].values\ntest_images = test_df.drop(columns=['id'], axis=1).values\n\nprint(\"Test Images Shape: \", test_images.shape)\nprint(\"Test ids Shape: \", test_id.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:57.481530Z","iopub.execute_input":"2025-08-21T01:03:57.481812Z","iopub.status.idle":"2025-08-21T01:03:57.495696Z","shell.execute_reply.started":"2025-08-21T01:03:57.481780Z","shell.execute_reply":"2025-08-21T01:03:57.494703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Normalizing to range [0, 1] - training data\nX = np.array(images) / 255 \ny = np.array(labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:57.499505Z","iopub.execute_input":"2025-08-21T01:03:57.500573Z","iopub.status.idle":"2025-08-21T01:03:57.953294Z","shell.execute_reply.started":"2025-08-21T01:03:57.500533Z","shell.execute_reply":"2025-08-21T01:03:57.951859Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert labels to categorical (one-hot encoding)\ndef onehot(y):\n    return np.eye(10)[y]\n\ny = onehot(y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:57.954467Z","iopub.execute_input":"2025-08-21T01:03:57.955658Z","iopub.status.idle":"2025-08-21T01:03:57.962865Z","shell.execute_reply.started":"2025-08-21T01:03:57.955617Z","shell.execute_reply":"2025-08-21T01:03:57.961808Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Images Shape: \", X.shape)\nprint(\"Label Shape: \", y.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:57.964177Z","iopub.execute_input":"2025-08-21T01:03:57.964710Z","iopub.status.idle":"2025-08-21T01:03:57.993616Z","shell.execute_reply.started":"2025-08-21T01:03:57.964680Z","shell.execute_reply":"2025-08-21T01:03:57.992249Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Split into training and test sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:57.994713Z","iopub.execute_input":"2025-08-21T01:03:57.995133Z","iopub.status.idle":"2025-08-21T01:03:58.659246Z","shell.execute_reply.started":"2025-08-21T01:03:57.995100Z","shell.execute_reply":"2025-08-21T01:03:58.658238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Build the neural network model\nfrom keras.models import Sequential\nfrom keras.layers import Dense","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:58.660331Z","iopub.execute_input":"2025-08-21T01:03:58.660630Z","iopub.status.idle":"2025-08-21T01:03:58.665511Z","shell.execute_reply.started":"2025-08-21T01:03:58.660607Z","shell.execute_reply":"2025-08-21T01:03:58.664579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(10, input_dim=X_train.shape[1], activation='relu')) # 10 neurons, input dimension = number of features (pixels)\nmodel.add(Dense(10, activation='relu')) # Hidden layer with 10 neurons\nmodel.add(Dense(10, activation='softmax')) # Output layer with 10 neurons (for 10 digits)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:58.666899Z","iopub.execute_input":"2025-08-21T01:03:58.667241Z","iopub.status.idle":"2025-08-21T01:03:58.733228Z","shell.execute_reply.started":"2025-08-21T01:03:58.667210Z","shell.execute_reply":"2025-08-21T01:03:58.732076Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#compile the model\nmodel.compile(\n    optimizer='adam',            # or SGD, RMSprop, etc.\n    loss='categorical_crossentropy',  # or 'binary_crossentropy', depending on your task\n    metrics=['accuracy']\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:58.734548Z","iopub.execute_input":"2025-08-21T01:03:58.734820Z","iopub.status.idle":"2025-08-21T01:03:58.748846Z","shell.execute_reply.started":"2025-08-21T01:03:58.734800Z","shell.execute_reply":"2025-08-21T01:03:58.747385Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#need to train\nmodel.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:03:58.750875Z","iopub.execute_input":"2025-08-21T01:03:58.751749Z","iopub.status.idle":"2025-08-21T01:04:51.243859Z","shell.execute_reply.started":"2025-08-21T01:03:58.751708Z","shell.execute_reply":"2025-08-21T01:04:51.242920Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate the model on the test set \nloss, accuracy = model.evaluate(X_test, y_test, verbose=0) \nprint(f\"Test accuracy: {accuracy:.2f}\") ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:04:51.245007Z","iopub.execute_input":"2025-08-21T01:04:51.245256Z","iopub.status.idle":"2025-08-21T01:04:52.085347Z","shell.execute_reply.started":"2025-08-21T01:04:51.245238Z","shell.execute_reply":"2025-08-21T01:04:52.084294Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make predictions on the test set \npredictions = model.predict(X_test) \npredicted_classes = np.argmax(predictions, axis=1) \ntrue_classes = np.argmax(y_test, axis=1) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:04:52.086693Z","iopub.execute_input":"2025-08-21T01:04:52.087208Z","iopub.status.idle":"2025-08-21T01:04:52.906076Z","shell.execute_reply.started":"2025-08-21T01:04:52.087181Z","shell.execute_reply":"2025-08-21T01:04:52.905097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Confusion matrix for test set\nconf_matrix = confusion_matrix(true_classes, predicted_classes) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:04:52.907086Z","iopub.execute_input":"2025-08-21T01:04:52.907414Z","iopub.status.idle":"2025-08-21T01:04:52.917574Z","shell.execute_reply.started":"2025-08-21T01:04:52.907387Z","shell.execute_reply":"2025-08-21T01:04:52.916288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot the confusion matrix of test data\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nplt.figure(figsize=(6, 4))\nsns.heatmap(conf_matrix, annot=True, fmt=\"d\", cmap=\"Blues\", xticklabels=range(10), yticklabels=range(10))\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:04:52.918777Z","iopub.execute_input":"2025-08-21T01:04:52.919086Z","iopub.status.idle":"2025-08-21T01:04:53.368608Z","shell.execute_reply.started":"2025-08-21T01:04:52.919058Z","shell.execute_reply":"2025-08-21T01:04:53.367499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Classification report for test data\nfrom sklearn.metrics import classification_report\n\nprint(classification_report(true_classes, predicted_classes, target_names=[str(i) for i in range(10)]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:04:53.369890Z","iopub.execute_input":"2025-08-21T01:04:53.370272Z","iopub.status.idle":"2025-08-21T01:04:53.409499Z","shell.execute_reply.started":"2025-08-21T01:04:53.370239Z","shell.execute_reply":"2025-08-21T01:04:53.408422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make predictions on the test set for submission \ntest_predictions = model.predict(test_images) \npredicted_classes = np.argmax(test_predictions, axis=1) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:04:53.410601Z","iopub.execute_input":"2025-08-21T01:04:53.410939Z","iopub.status.idle":"2025-08-21T01:04:53.970616Z","shell.execute_reply.started":"2025-08-21T01:04:53.410914Z","shell.execute_reply":"2025-08-21T01:04:53.969895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df[\"label\"] = predicted_classes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:04:53.971571Z","iopub.execute_input":"2025-08-21T01:04:53.971848Z","iopub.status.idle":"2025-08-21T01:04:53.978503Z","shell.execute_reply.started":"2025-08-21T01:04:53.971805Z","shell.execute_reply":"2025-08-21T01:04:53.977264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df_new = test_df[[\"id\", \"label\"]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:04:53.979521Z","iopub.execute_input":"2025-08-21T01:04:53.979799Z","iopub.status.idle":"2025-08-21T01:04:54.006364Z","shell.execute_reply.started":"2025-08-21T01:04:53.979778Z","shell.execute_reply":"2025-08-21T01:04:54.003061Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df_new.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:04:54.007554Z","iopub.execute_input":"2025-08-21T01:04:54.008448Z","iopub.status.idle":"2025-08-21T01:04:54.034659Z","shell.execute_reply.started":"2025-08-21T01:04:54.008398Z","shell.execute_reply":"2025-08-21T01:04:54.033578Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save to CSV\ntest_df_new.to_csv(\"/kaggle/working/submission.csv\", index=False)\n\nprint(\"Predictions saved to submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T01:04:54.035715Z","iopub.execute_input":"2025-08-21T01:04:54.036094Z","iopub.status.idle":"2025-08-21T01:04:54.063314Z","shell.execute_reply.started":"2025-08-21T01:04:54.036063Z","shell.execute_reply":"2025-08-21T01:04:54.062287Z"}},"outputs":[],"execution_count":null}]}