{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":30698,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix\nfrom sklearn.ensemble import AdaBoostClassifier\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Loading data\ntrain = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\n\n# Preparing data\nX = train.drop(columns=['label']).values / 255.0  # Normalize the pixel values to the range of [0, 1].\ny = train['label'].values\n\n# Split the dataset into training and validation sets.\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Define Adaboost model\nadaboost_model = AdaBoostClassifier(n_estimators=100, random_state=42)\n\n# Training model\nadaboost_model.fit(X_train, y_train)\n\n# Evaluate the model on the validation set\nval_preds = adaboost_model.predict(X_val)\n\n# Calculate metrics\nval_labels = np.unique(y_val)  # Get the label categories in the validation set.\naccuracy = accuracy_score(y_val, val_preds)\nprecision = precision_score(y_val, val_preds, average='weighted', labels=val_labels)\nrecall = recall_score(y_val, val_preds, average='weighted', labels=val_labels)\nf1 = f1_score(y_val, val_preds, average='weighted', labels=val_labels)\nconf_matrix = confusion_matrix(y_val, val_preds)\n\n# Plot the confusion matrix.\nplt.figure(figsize=(10, 8))\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=val_labels, yticklabels=val_labels)\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.title('Confusion Matrix')\nplt.show()\n\n# Print metrics.\nprint(\"Accuracy:\", accuracy)\nprint(\"Precision:\", precision)\nprint(\"Recall:\", recall)\nprint(\"F1 Score:\", f1)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-25T10:18:37.524868Z","iopub.execute_input":"2024-04-25T10:18:37.525292Z","iopub.status.idle":"2024-04-25T10:20:35.850135Z","shell.execute_reply.started":"2024-04-25T10:18:37.525260Z","shell.execute_reply":"2024-04-25T10:20:35.849342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.ensemble import AdaBoostClassifier\n\n# Read the test data\ntest = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\n\n# Extract features\nX_test = test.drop(columns=['id']).values / 255.0  # Normalize the pixel values to the range of [0, 1].\n\n\n\n# Use the model to make predictions about the test set\ntest_preds = adaboost_model.predict(X_test)\n\n# Save the prediction results to a CSV file\nsubmission = pd.DataFrame(data={\n    \"id\": test['id'],\n    \"label\": test_preds\n})\nsubmission.to_csv(\"submission.csv\", index=None)\n\n# View the predictions\nsubmission.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-25T10:20:35.852275Z","iopub.execute_input":"2024-04-25T10:20:35.852896Z","iopub.status.idle":"2024-04-25T10:20:36.851654Z","shell.execute_reply.started":"2024-04-25T10:20:35.852867Z","shell.execute_reply":"2024-04-25T10:20:36.850772Z"},"trusted":true},"execution_count":null,"outputs":[]}]}