{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6aa06cfb-e327-4cd0-b26f-fcea3ed64cb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import ( \n",
    "    accuracy_score,\n",
    "    classification_report,\n",
    "    confusion_matrix,\n",
    "    precision_score,\n",
    "    recall_score,f1_score )\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import precision_recall_fscore_support\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.svm import SVC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e80746fa-4c33-4ac1-9b1e-3dc74c410e4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PatientId</th>\n",
       "      <th>Pregnancies</th>\n",
       "      <th>Glucose</th>\n",
       "      <th>BloodPressure</th>\n",
       "      <th>SkinThickness</th>\n",
       "      <th>Insulin</th>\n",
       "      <th>BMI</th>\n",
       "      <th>DiabetesPedigreeFunction</th>\n",
       "      <th>Age</th>\n",
       "      <th>Outcome</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>148</td>\n",
       "      <td>72</td>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "      <td>33.6</td>\n",
       "      <td>0.627</td>\n",
       "      <td>50</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>26.6</td>\n",
       "      <td>0.351</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>183</td>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>23.3</td>\n",
       "      <td>0.672</td>\n",
       "      <td>32</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>89</td>\n",
       "      <td>66</td>\n",
       "      <td>23</td>\n",
       "      <td>94</td>\n",
       "      <td>28.1</td>\n",
       "      <td>0.167</td>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>137</td>\n",
       "      <td>40</td>\n",
       "      <td>35</td>\n",
       "      <td>168</td>\n",
       "      <td>43.1</td>\n",
       "      <td>2.288</td>\n",
       "      <td>33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   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",
       "    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  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"diabetes.csv\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2c8bfc2a-8034-41cd-bf8a-2cd74490f83f",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = df.drop('Outcome', axis = 1)\n",
    "b = df['Outcome']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "25ce9d41-991b-4228-8b44-b3ebd1753774",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    A,\n",
    "    b,\n",
    "    test_size=0.2,\n",
    "    random_state=101\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9e90ec4d-7f2a-4927-8de1-35630050a44a",
   "metadata": {},
   "outputs": [],
   "source": [
    "models = {\n",
    "    \"Decision Tree\": DecisionTreeClassifier(random_state=101),\n",
    "    \"Random Forest\": RandomForestClassifier(n_estimators=100, random_state=101),\n",
    "    \"Naive Bayes\": GaussianNB(),\n",
    "    \"SVM (Linear Kernel)\": SVC(kernel=\"linear\"),\n",
    "    \"SVM (RBF Kernel)\": SVC(kernel=\"rbf\")\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9fc92613-cf7a-4703-8737-160d3a549a5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training Accuracy: 1.0\n",
      "Testing Accuracy: 0.7012987012987013\n",
      "\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.77      0.79      0.78       103\n",
      "           1       0.55      0.53      0.54        51\n",
      "\n",
      "    accuracy                           0.70       154\n",
      "   macro avg       0.66      0.66      0.66       154\n",
      "weighted avg       0.70      0.70      0.70       154\n",
      "\n",
      "Confusion Matrix:\n",
      "[[81 22]\n",
      " [24 27]]\n",
      "Training Accuracy: 1.0\n",
      "Testing Accuracy: 0.7662337662337663\n",
      "\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.82      0.83      0.83       103\n",
      "           1       0.65      0.63      0.64        51\n",
      "\n",
      "    accuracy                           0.77       154\n",
      "   macro avg       0.74      0.73      0.73       154\n",
      "weighted avg       0.76      0.77      0.77       154\n",
      "\n",
      "Confusion Matrix:\n",
      "[[86 17]\n",
      " [19 32]]\n",
      "Training Accuracy: 0.750814332247557\n",
      "Testing Accuracy: 0.7532467532467533\n",
      "\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.82      0.82      0.82       103\n",
      "           1       0.63      0.63      0.63        51\n",
      "\n",
      "    accuracy                           0.75       154\n",
      "   macro avg       0.72      0.72      0.72       154\n",
      "weighted avg       0.75      0.75      0.75       154\n",
      "\n",
      "Confusion Matrix:\n",
      "[[84 19]\n",
      " [19 32]]\n",
      "Training Accuracy: 0.7736156351791531\n",
      "Testing Accuracy: 0.7792207792207793\n",
      "\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.82      0.86      0.84       103\n",
      "           1       0.69      0.61      0.65        51\n",
      "\n",
      "    accuracy                           0.78       154\n",
      "   macro avg       0.75      0.74      0.74       154\n",
      "weighted avg       0.77      0.78      0.78       154\n",
      "\n",
      "Confusion Matrix:\n",
      "[[89 14]\n",
      " [20 31]]\n",
      "Training Accuracy: 0.7182410423452769\n",
      "Testing Accuracy: 0.7272727272727273\n",
      "\n",
      "Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.73      0.95      0.82       103\n",
      "           1       0.74      0.27      0.40        51\n",
      "\n",
      "    accuracy                           0.73       154\n",
      "   macro avg       0.73      0.61      0.61       154\n",
      "weighted avg       0.73      0.73      0.68       154\n",
      "\n",
      "Confusion Matrix:\n",
      "[[98  5]\n",
      " [37 14]]\n"
     ]
    }
   ],
   "source": [
    "summary = []\n",
    "\n",
    "for model_name, clf in models.items():\n",
    "\n",
    "    clf.fit(X_train, y_train)\n",
    "\n",
    "    train_pred = clf.predict(X_train)\n",
    "    test_pred = clf.predict(X_test)\n",
    "\n",
    "    train_acc = accuracy_score(y_train, train_pred)\n",
    "    test_acc = accuracy_score(y_test, test_pred)\n",
    "\n",
    "    precision, recall, f1, _ = precision_recall_fscore_support(\n",
    "        y_test, test_pred, average=\"weighted\"\n",
    "    )\n",
    "\n",
    "    summary.append([\n",
    "        model_name,\n",
    "        train_acc,\n",
    "        test_acc,\n",
    "        precision,\n",
    "        recall,\n",
    "        f1\n",
    "    ])\n",
    "       \n",
    "    print(\"Training Accuracy:\", train_acc)\n",
    "    print(\"Testing Accuracy:\", test_acc)\n",
    "\n",
    "    print(\"\\nClassification Report:\")\n",
    "    print(classification_report(y_test, test_pred))\n",
    "\n",
    "    print(\"Confusion Matrix:\")\n",
    "    print(confusion_matrix(y_test, test_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4654aaca-2ca2-47e1-9db8-f6dd7d720b0a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "anaconda-2025.12-py312",
   "language": "python",
   "name": "conda-env-anaconda-2025.12-py312-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.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
