{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d1fa59c6-bf48-49e5-9af5-e145bd5eb4a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.decomposition import PCA\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0e9043d4-5efd-4baa-957d-e98e99817521",
   "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": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"diabetes.csv\")\n",
    "df.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c41bde3a-a528-4dbc-ad2d-3d3716545663",
   "metadata": {},
   "outputs": [],
   "source": [
    "cols_with_zero = ['Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI']\n",
    "\n",
    "for col in cols_with_zero:\n",
    "    df[col] = df[col].replace(0, np.nan)\n",
    "    df[col].fillna(df[col].median(), inplace=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4123f35a-4623-41c1-8de0-b23744a2dd6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df.drop(\"Outcome\", axis=1)\n",
    "y = df[\"Outcome\"]\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X, y, test_size=0.2, random_state=42\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "760580bc-e6e5-4c27-b64d-7bd42d46c78b",
   "metadata": {},
   "outputs": [],
   "source": [
    "rf = RandomForestClassifier(n_estimators=100, random_state=42)\n",
    "rf.fit(X_train, y_train)\n",
    "\n",
    "y_pred_rf = rf.predict(X_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e3ecc3ff-422b-48c1-bd76-d2c2446bf2ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "def evaluate_model(name, y_true, y_pred):\n",
    "    print(f\"--- {name} ---\")\n",
    "    print(\"Accuracy :\", accuracy_score(y_true, y_pred))\n",
    "    print(\"Precision:\", precision_score(y_true, y_pred))\n",
    "    print(\"Recall   :\", recall_score(y_true, y_pred))\n",
    "    print(\"F1 Score :\", f1_score(y_true, y_pred))\n",
    "    print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "578f5e8f-003c-483e-a91c-790a0f54112d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- Random Forest (All Features) ---\n",
      "Accuracy : 0.7467532467532467\n",
      "Precision: 0.6481481481481481\n",
      "Recall   : 0.6363636363636364\n",
      "F1 Score : 0.6422018348623852\n",
      "\n"
     ]
    }
   ],
   "source": [
    "evaluate_model(\"Random Forest (All Features)\", y_test, y_pred_rf)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c3fd1f59-c1a0-498a-8309-6e1e57eb6f85",
   "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>Feature</th>\n",
       "      <th>Importance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Glucose</td>\n",
       "      <td>0.267096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>BMI</td>\n",
       "      <td>0.144299</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Age</td>\n",
       "      <td>0.131702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>DiabetesPedigreeFunction</td>\n",
       "      <td>0.102785</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>PatientId</td>\n",
       "      <td>0.087289</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Insulin</td>\n",
       "      <td>0.076877</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>BloodPressure</td>\n",
       "      <td>0.069971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SkinThickness</td>\n",
       "      <td>0.062592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Pregnancies</td>\n",
       "      <td>0.057390</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    Feature  Importance\n",
       "2                   Glucose    0.267096\n",
       "6                       BMI    0.144299\n",
       "8                       Age    0.131702\n",
       "7  DiabetesPedigreeFunction    0.102785\n",
       "0                 PatientId    0.087289\n",
       "5                   Insulin    0.076877\n",
       "3             BloodPressure    0.069971\n",
       "4             SkinThickness    0.062592\n",
       "1               Pregnancies    0.057390"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "importances = rf.feature_importances_\n",
    "feature_names = X.columns\n",
    "\n",
    "feat_imp = pd.DataFrame({\n",
    "    'Feature': feature_names,\n",
    "    'Importance': importances\n",
    "}).sort_values(by='Importance', ascending=False)\n",
    "\n",
    "feat_imp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "47eb02fe-0549-47b0-b568-6398bfe616ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- Random Forest (Top Features) ---\n",
      "Accuracy : 0.7467532467532467\n",
      "Precision: 0.6379310344827587\n",
      "Recall   : 0.6727272727272727\n",
      "F1 Score : 0.6548672566371682\n",
      "\n"
     ]
    }
   ],
   "source": [
    "top_features = feat_imp.head(5)['Feature'].tolist()\n",
    "\n",
    "X_train_fs = X_train[top_features]\n",
    "X_test_fs = X_test[top_features]\n",
    "\n",
    "rf_fs = RandomForestClassifier(n_estimators=100, random_state=42)\n",
    "rf_fs.fit(X_train_fs, y_train)\n",
    "\n",
    "y_pred_rf_fs = rf_fs.predict(X_test_fs)\n",
    "\n",
    "evaluate_model(\"Random Forest (Top Features)\", y_test, y_pred_rf_fs)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "0d6ce433-6520-4766-aa05-a9e4500ec3f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- Random Forest (PCA) ---\n",
      "Accuracy : 0.7207792207792207\n",
      "Precision: 0.6071428571428571\n",
      "Recall   : 0.6181818181818182\n",
      "F1 Score : 0.6126126126126126\n",
      "\n"
     ]
    }
   ],
   "source": [
    "scaler = StandardScaler()\n",
    "X_scaled = scaler.fit_transform(X)\n",
    "\n",
    "pca = PCA(n_components=5)\n",
    "X_pca = pca.fit_transform(X_scaled)\n",
    "\n",
    "X_train_pca, X_test_pca, y_train_pca, y_test_pca = train_test_split(\n",
    "    X_pca, y, test_size=0.2, random_state=42\n",
    ")\n",
    "\n",
    "rf_pca = RandomForestClassifier(n_estimators=100, random_state=42)\n",
    "rf_pca.fit(X_train_pca, y_train_pca)\n",
    "\n",
    "y_pred_rf_pca = rf_pca.predict(X_test_pca)\n",
    "\n",
    "evaluate_model(\"Random Forest (PCA)\", y_test_pca, y_pred_rf_pca)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4b8d0cda-2166-4255-9f80-824093c13361",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- Logistic Regression ---\n",
      "Accuracy : 0.7597402597402597\n",
      "Precision: 0.6730769230769231\n",
      "Recall   : 0.6363636363636364\n",
      "F1 Score : 0.6542056074766355\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "log_model = LogisticRegression(max_iter=1000)\n",
    "log_model.fit(X_train, y_train)\n",
    "\n",
    "y_pred_log = log_model.predict(X_test)\n",
    "\n",
    "evaluate_model(\"Logistic Regression\", y_test, y_pred_log)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8aca5e66-0f20-4c31-a145-a6bcd2947064",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- Naive Bayes ---\n",
      "Accuracy : 0.7532467532467533\n",
      "Precision: 0.6491228070175439\n",
      "Recall   : 0.6727272727272727\n",
      "F1 Score : 0.6607142857142858\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.naive_bayes import GaussianNB\n",
    "\n",
    "nb_model = GaussianNB()\n",
    "nb_model.fit(X_train, y_train)\n",
    "\n",
    "y_pred_nb = nb_model.predict(X_test)\n",
    "\n",
    "evaluate_model(\"Naive Bayes\", y_test, y_pred_nb)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3666ece5-b8a1-4917-95f5-4f439cc4df18",
   "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>Model</th>\n",
       "      <th>Accuracy</th>\n",
       "      <th>Precision</th>\n",
       "      <th>Recall</th>\n",
       "      <th>F1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>RF (All)</td>\n",
       "      <td>0.746753</td>\n",
       "      <td>0.648148</td>\n",
       "      <td>0.636364</td>\n",
       "      <td>0.642202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>RF (Top Features)</td>\n",
       "      <td>0.746753</td>\n",
       "      <td>0.637931</td>\n",
       "      <td>0.672727</td>\n",
       "      <td>0.654867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>RF (PCA)</td>\n",
       "      <td>0.720779</td>\n",
       "      <td>0.607143</td>\n",
       "      <td>0.618182</td>\n",
       "      <td>0.612613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Logistic Regression</td>\n",
       "      <td>0.759740</td>\n",
       "      <td>0.673077</td>\n",
       "      <td>0.636364</td>\n",
       "      <td>0.654206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Naive Bayes</td>\n",
       "      <td>0.753247</td>\n",
       "      <td>0.649123</td>\n",
       "      <td>0.672727</td>\n",
       "      <td>0.660714</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 Model  Accuracy  Precision    Recall        F1\n",
       "0             RF (All)  0.746753   0.648148  0.636364  0.642202\n",
       "1    RF (Top Features)  0.746753   0.637931  0.672727  0.654867\n",
       "2             RF (PCA)  0.720779   0.607143  0.618182  0.612613\n",
       "3  Logistic Regression  0.759740   0.673077  0.636364  0.654206\n",
       "4          Naive Bayes  0.753247   0.649123  0.672727  0.660714"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = pd.DataFrame(columns=[\"Model\", \"Accuracy\", \"Precision\", \"Recall\", \"F1\"])\n",
    "\n",
    "def add_result(name, y_true, y_pred):\n",
    "    results.loc[len(results)] = [\n",
    "        name,\n",
    "        accuracy_score(y_true, y_pred),\n",
    "        precision_score(y_true, y_pred),\n",
    "        recall_score(y_true, y_pred),\n",
    "        f1_score(y_true, y_pred)\n",
    "    ]\n",
    "\n",
    "add_result(\"RF (All)\", y_test, y_pred_rf)\n",
    "add_result(\"RF (Top Features)\", y_test, y_pred_rf_fs)\n",
    "add_result(\"RF (PCA)\", y_test_pca, y_pred_rf_pca)\n",
    "add_result(\"Logistic Regression\", y_test, y_pred_log)\n",
    "add_result(\"Naive Bayes\", y_test, y_pred_nb)\n",
    "\n",
    "results\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "759eb28b-4819-4f43-9b8f-4a6b6e92260f",
   "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
}
