{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "8dd26cbe-3e3c-4536-a591-677490e61d0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt  # Add this line\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.metrics import accuracy_score, classification_report, confusion_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "48c04b30-2d31-4245-8e72-3a1cde1a4eff",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('diabetes.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "5251a156-469e-4052-8ec1-da4ecac183d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     PatientId  Pregnancies  Glucose  BloodPressure  SkinThickness  Insulin  \\\n",
      "0            1            6      148             72             35        0   \n",
      "1            2            1       85             66             29        0   \n",
      "2            3            8      183             64              0        0   \n",
      "3            4            1       89             66             23       94   \n",
      "4            5            0      137             40             35      168   \n",
      "..         ...          ...      ...            ...            ...      ...   \n",
      "763        764           10      101             76             48      180   \n",
      "764        765            2      122             70             27        0   \n",
      "765        766            5      121             72             23      112   \n",
      "766        767            1      126             60              0        0   \n",
      "767        768            1       93             70             31        0   \n",
      "\n",
      "      BMI  DiabetesPedigreeFunction  Age  Outcome  \n",
      "0    33.6                     0.627   50        1  \n",
      "1    26.6                     0.351   31        0  \n",
      "2    23.3                     0.672   32        1  \n",
      "3    28.1                     0.167   21        0  \n",
      "4    43.1                     2.288   33        1  \n",
      "..    ...                       ...  ...      ...  \n",
      "763  32.9                     0.171   63        0  \n",
      "764  36.8                     0.340   27        0  \n",
      "765  26.2                     0.245   30        0  \n",
      "766  30.1                     0.349   47        1  \n",
      "767  30.4                     0.315   23        0  \n",
      "\n",
      "[768 rows x 10 columns]\n"
     ]
    }
   ],
   "source": [
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "af7ce335-53f7-448b-8d01-e888539418ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df.drop('Outcome', axis=1)  # Features (all columns except the target)\n",
    "y = df['Outcome']   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3717806a-17be-4c39-a128-e6e757743e2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "bc50c896-9040-4c99-b2ed-8b8724283fb3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-6 {color: black;}#sk-container-id-6 pre{padding: 0;}#sk-container-id-6 div.sk-toggleable {background-color: white;}#sk-container-id-6 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-6 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-6 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-6 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-6 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-6 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-6 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-6 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-6 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-6 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-6 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-6 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-6 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-6 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-6 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-6 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-6 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-6 div.sk-item {position: relative;z-index: 1;}#sk-container-id-6 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-6 div.sk-item::before, #sk-container-id-6 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-6 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-6 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-6 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-6 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-6 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-6 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-6 div.sk-label-container {text-align: center;}#sk-container-id-6 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-6 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-6\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>DecisionTreeClassifier(random_state=42)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-6\" type=\"checkbox\" checked><label for=\"sk-estimator-id-6\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">DecisionTreeClassifier</label><div class=\"sk-toggleable__content\"><pre>DecisionTreeClassifier(random_state=42)</pre></div></div></div></div></div>"
      ],
      "text/plain": [
       "DecisionTreeClassifier(random_state=42)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Train a decision tree classifier\n",
    "model = DecisionTreeClassifier(random_state=42)\n",
    "model.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "696f2083-2c1d-4d57-b2d1-c58d3366c26a",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred = model.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "638c9296-7213-4db7-838a-41ba93583846",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.7467532467532467\n"
     ]
    }
   ],
   "source": [
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(\"Accuracy:\", accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "e8fda619-ab5b-4322-999b-7f14d73994a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training Accuracy: 1.00\n",
      "Testing Accuracy: 0.75\n"
     ]
    }
   ],
   "source": [
    "# Accuracy on training data (how well model learned)\n",
    "train_accuracy = model.score(X_train, y_train)\n",
    "\n",
    "# Accuracy on testing data (how well model performs on unseen data)\n",
    "test_accuracy = model.score(X_test, y_test)\n",
    "\n",
    "print(f\"Training Accuracy: {train_accuracy:.2f}\")  # e.g., 0.85 means 85%\n",
    "print(f\"Testing Accuracy: {test_accuracy:.2f}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb96d4e1-fdfb-458c-9bfc-7b228d50c56c",
   "metadata": {},
   "source": [
    "Here is Comparison of \n",
    "Decision Tree,\n",
    "Random Forest,\n",
    "Naïve Bayes,\n",
    "SVM (Linear),\n",
    "SVM (RBF)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c04e8431-7312-4040-931c-adc2d8875480",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3df19501-f9e3-4f79-9fef-8569353d5031",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('diabetes.csv')\n",
    "X = df.drop('Outcome', axis=1)\n",
    "y = df['Outcome']\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4f892682-b735-43d5-84c5-b4618645e9f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "models = {\n",
    "    'Decision Tree': DecisionTreeClassifier(random_state=42),\n",
    "    'Random Forest': RandomForestClassifier(random_state=42),\n",
    "    'Naïve Bayes': GaussianNB(),\n",
    "    'SVM (Linear)': SVC(kernel='linear', random_state=42),\n",
    "    'SVM (RBF)': SVC(kernel='rbf', random_state=42)\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cfbc9ac7-8ca2-4ba5-9d41-df2054def665",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           Model  Training Accuracy  Test Accuracy  Precision    Recall  \\\n",
      "0  Decision Tree           1.000000       0.746753   0.637931  0.672727   \n",
      "1  Random Forest           1.000000       0.753247   0.644068  0.690909   \n",
      "2    Naïve Bayes           0.752443       0.759740   0.650000  0.709091   \n",
      "3   SVM (Linear)           0.773616       0.766234   0.672727  0.672727   \n",
      "4      SVM (RBF)           0.744300       0.746753   0.750000  0.436364   \n",
      "\n",
      "   F1-score  \n",
      "0  0.654867  \n",
      "1  0.666667  \n",
      "2  0.678261  \n",
      "3  0.672727  \n",
      "4  0.551724  \n"
     ]
    }
   ],
   "source": [
    "results = []\n",
    "\n",
    "for name, model in models.items():\n",
    "    # Train model\n",
    "    model.fit(X_train, y_train)\n",
    "    \n",
    "    # Predict on train and test data\n",
    "    y_train_pred = model.predict(X_train)\n",
    "    y_test_pred = model.predict(X_test)\n",
    "    \n",
    "    # Calculate metrics\n",
    "    train_acc = accuracy_score(y_train, y_train_pred)\n",
    "    test_acc = accuracy_score(y_test, y_test_pred)\n",
    "    precision = precision_score(y_test, y_test_pred)\n",
    "    recall = recall_score(y_test, y_test_pred)\n",
    "    f1 = f1_score(y_test, y_test_pred)\n",
    "    \n",
    "    # Store results\n",
    "    results.append({\n",
    "        'Model': name,\n",
    "        'Training Accuracy': train_acc,\n",
    "        'Test Accuracy': test_acc,\n",
    "        'Precision': precision,\n",
    "        'Recall': recall,\n",
    "        'F1-score': f1\n",
    "    })\n",
    "\n",
    "# Convert to DataFrame for a nice table\n",
    "results_df = pd.DataFrame(results)\n",
    "print(results_df)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a738dcc8-ea0f-47d3-b6e0-614cecf7d6bd",
   "metadata": {},
   "source": [
    "1. Which algorithm performed best on the test data? Why might that be the case?\n",
    "\n",
    "- SVM (Linear) has the highest test accuracy (76.6%) and a balanced F1-score (0.673).\n",
    "- Naïve Bayes also has decent test accuracy (76.0%) with the best recall (0.709) and F1-score (0.678).\n",
    "- So, SVM (Linear) performs best overall by test accuracy, possibly because the linear kernel fits the data well without overfitting.\n",
    "- Naïve Bayes performs well despite its simple assumptions, likely because the dataset features somewhat satisfy independence.\n",
    "\n",
    "---\n",
    "\n",
    "2. Did any algorithm show signs of overfitting (high training accuracy, low test accuracy)?\n",
    "\n",
    "- Decision Tree and Random Forest have perfect training accuracy (1.0) but noticeably lower test accuracy (~74.7% and 75.3%).\n",
    "- This suggests overfitting: they fit the training data perfectly but don’t generalize as well to unseen data.\n",
    "- SVMs and Naïve Bayes have similar training and test accuracies, indicating better generalization.\n",
    "\n",
    "---\n",
    "\n",
    "3. How does Naïve Bayes handle correlated features compared to Decision Tree or SVM?\n",
    "\n",
    "- Despite Naïve Bayes assuming feature independence, it achieved good results (Test Accuracy 76%, F1 0.678).\n",
    "- This suggests that feature correlations in this dataset might not heavily degrade Naïve Bayes' performance.\n",
    "- Decision Trees and SVMs don’t assume independence, so they model feature interactions better, but here Naïve Bayes still performs competitively.\n",
    "\n",
    "---\n",
    "\n",
    "4. Why might Random Forest outperform a single Decision Tree?\n",
    "\n",
    "- Random Forest slightly outperforms the Decision Tree on test accuracy (75.3% vs 74.7%) and F1-score (0.667 vs 0.655).\n",
    "- This is because Random Forest combines many trees and averages results, reducing overfitting and variance compared to a single tree.\n",
    "- However, in this case, the improvement is small, possibly due to dataset size or feature characteristics.\n",
    "\n",
    "---\n",
    "\n",
    "5. If you had to deploy a model for predicting diabetes, which one would you choose, and why?\n",
    "\n",
    "- SVM (Linear): best test accuracy and balanced metrics, good generalization.\n",
    "- Naïve Bayes: fast, simple, good recall (important for health predictions to reduce false negatives).\n",
    "- Random Forest: good balance of performance and robustness, but slight overfitting signs.\n",
    "\n",
    "Recommendation: Use SVM (Linear) for its accuracy and generalization. If interpretability or speed is critical, Naïve Bayes is a solid alternative.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "facdff69-a041-4ddc-9892-070549805db0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "59711b17-fbc4-4268-a7df-1f782d28e63e",
   "metadata": {},
   "source": [
    "Among all the models we tested, the SVM with a linear kernel did the best on the test data, with the highest accuracy of about 76.6% and a good balance between precision and recall (F1-score 0.673). This means the SVM (Linear) fits the data well without overfitting.\n",
    "\n",
    "Naïve Bayes also did pretty well, scoring 76% accuracy and the highest recall (0.709), which is important in medical diagnosis to catch as many positive cases as possible. Even though Naïve Bayes assumes that features are independent, it still worked well, suggesting this assumption wasn’t a big problem here.\n",
    "\n",
    "Both Decision Tree and Random Forest had perfect accuracy on the training data but performed worse on the test data, showing signs of overfitting. Random Forest did a bit better than a single Decision Tree because it combines many trees to make better predictions.\n",
    "\n",
    "The SVM with the RBF kernel had high precision but low recall, meaning it missed a lot of positive cases, which isn’t ideal for health-related predictions.\n",
    "\n",
    "Overall, I would choose the SVM (Linear) model for deployment because it generalizes well and performs best overall. If you need a simpler or faster model, Naïve Bayes is a good alternative. This shows how important it is to consider all metrics, not just accuracy, especially in healthcare.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c933446d-6f23-47dd-81c2-1a2c84fabdc3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "anaconda-panel-2023.05-py310",
   "language": "python",
   "name": "conda-env-anaconda-panel-2023.05-py310-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
