{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "46e4101d-448f-4a84-b8d6-be10585fc447",
   "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.ensemble import RandomForestClassifier\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n",
    "\n",
    "# Load data\n",
    "df = pd.read_csv(\"diabetes.csv\")\n",
    "\n",
    "cols = [\"Glucose\", \"BloodPressure\", \"SkinThickness\", \"Insulin\", \"BMI\"]\n",
    "df[cols] = df[cols].replace(0, np.nan)\n",
    "df[cols] = df[cols].fillna(df[cols].median())\n",
    "\n",
    "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": 19,
   "id": "9b37a212-48e8-407e-b27a-adbed42cbab2",
   "metadata": {},
   "outputs": [],
   "source": [
    "rf = RandomForestClassifier(random_state=42)\n",
    "rf.fit(X_train, y_train)\n",
    "rf_pred = rf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8ae89fa5-fdea-4376-9035-ebd7fda1d1eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "importances = rf.feature_importances_\n",
    "features = X.columns\n",
    "\n",
    "important_features = features[importances.argsort()[-4:]]\n",
    "X_imp = X[important_features]\n",
    "\n",
    "X_train_i, X_test_i, y_train_i, y_test_i = train_test_split(\n",
    "    X_imp, y, test_size=0.2, random_state=42\n",
    ")\n",
    "\n",
    "rf_imp = RandomForestClassifier(random_state=42)\n",
    "rf_imp.fit(X_train_i, y_train_i)\n",
    "rf_imp_pred = rf_imp.predict(X_test_i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "63c542e5-1ee6-4f49-ae9e-f6bdb0e987ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "pca = PCA(n_components=4)\n",
    "X_pca = pca.fit_transform(X)\n",
    "\n",
    "X_train_p, X_test_p, y_train_p, y_test_p = train_test_split(\n",
    "    X_pca, y, test_size=0.2, random_state=42\n",
    ")\n",
    "\n",
    "rf_pca = RandomForestClassifier(random_state=42)\n",
    "rf_pca.fit(X_train_p, y_train_p)\n",
    "rf_pca_pred = rf_pca.predict(X_test_p)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "799041ed-87b0-4fc9-b63b-5e8450f66b76",
   "metadata": {},
   "outputs": [],
   "source": [
    "lr = LogisticRegression(max_iter=1000)\n",
    "lr.fit(X_train, y_train)\n",
    "lr_pred = lr.predict(X_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "51de2017-1245-461c-87f8-20f27ef008e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "nb = GaussianNB()\n",
    "nb.fit(X_train, y_train)\n",
    "nb_pred = nb.predict(X_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "79fc68d6-1a45-44c9-951f-0ebd62e03cba",
   "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>Random Forest</th>\n",
       "      <th>Random ForestF (Features Selected)</th>\n",
       "      <th>PCA-Reduced Random Forest</th>\n",
       "      <th>Logistic Regression</th>\n",
       "      <th>Naive Bayes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Accuracy</th>\n",
       "      <td>0.746753</td>\n",
       "      <td>0.753247</td>\n",
       "      <td>0.701299</td>\n",
       "      <td>0.759740</td>\n",
       "      <td>0.753247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Precision</th>\n",
       "      <td>0.648148</td>\n",
       "      <td>0.644068</td>\n",
       "      <td>0.578947</td>\n",
       "      <td>0.673077</td>\n",
       "      <td>0.649123</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Recall</th>\n",
       "      <td>0.636364</td>\n",
       "      <td>0.690909</td>\n",
       "      <td>0.600000</td>\n",
       "      <td>0.636364</td>\n",
       "      <td>0.672727</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F1</th>\n",
       "      <td>0.642202</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.589286</td>\n",
       "      <td>0.654206</td>\n",
       "      <td>0.660714</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Random Forest  Random ForestF (Features Selected)  \\\n",
       "Accuracy        0.746753                            0.753247   \n",
       "Precision       0.648148                            0.644068   \n",
       "Recall          0.636364                            0.690909   \n",
       "F1              0.642202                            0.666667   \n",
       "\n",
       "           PCA-Reduced Random Forest  Logistic Regression  Naive Bayes  \n",
       "Accuracy                    0.701299             0.759740     0.753247  \n",
       "Precision                   0.578947             0.673077     0.649123  \n",
       "Recall                      0.600000             0.636364     0.672727  \n",
       "F1                          0.589286             0.654206     0.660714  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def evaluate(y_true, y_pred):\n",
    "    return {\n",
    "        \"Accuracy\": accuracy_score(y_true, y_pred),\n",
    "        \"Precision\": precision_score(y_true, y_pred),\n",
    "        \"Recall\": recall_score(y_true, y_pred),\n",
    "        \"F1\": f1_score(y_true, y_pred)\n",
    "    }\n",
    "\n",
    "results = {\n",
    "    \"Random Forest\": evaluate(y_test, rf_pred),\n",
    "    \"Random ForestF (Features Selected)\": evaluate(y_test_i, rf_imp_pred),\n",
    "    \"PCA-Reduced Random Forest\": evaluate(y_test_p, rf_pca_pred),\n",
    "    \"Logistic Regression\": evaluate(y_test, lr_pred),\n",
    "    \"Naive Bayes\": evaluate(y_test, nb_pred)\n",
    "}\n",
    "\n",
    "pd.DataFrame(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d95376ff-8c04-4994-8294-10da7fa397e0",
   "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
}
