{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "collapsed": true,
        "id": "273zC8GGEb_J",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "da2519d9-8b98-42a5-c324-1b420af8117e"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "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",
            "    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",
            "Feature Importance:\n",
            "                    Feature  Importance\n",
            "2                   Glucose    0.267089\n",
            "6                       BMI    0.153334\n",
            "8                       Age    0.129185\n",
            "7  DiabetesPedigreeFunction    0.102934\n",
            "0                 PatientId    0.090048\n",
            "3             BloodPressure    0.075748\n",
            "1               Pregnancies    0.065340\n",
            "5                   Insulin    0.060622\n",
            "4             SkinThickness    0.055700\n",
            "\n",
            "Model Comparison Results:\n",
            "                          Model  Accuracy  Precision    Recall  F1 Score\n",
            "0      Random Forest (Original)  0.753247   0.644068  0.690909  0.666667\n",
            "1  Random Forest (Top Features)  0.772727   0.678571  0.690909  0.684685\n",
            "2           Random Forest (PCA)  0.701299   0.591837  0.527273  0.557692\n",
            "3           Logistic Regression  0.733766   0.645833  0.563636  0.601942\n",
            "4                   Naive Bayes  0.772727   0.717391  0.600000  0.653465\n"
          ]
        }
      ],
      "source": [
        "# ===============================\n",
        "# Feature Selection & Model Comparison\n",
        "# ===============================\n",
        "\n",
        "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",
        "\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",
        "\n",
        "# ===============================\n",
        "# Load Dataset\n",
        "# ===============================\n",
        "df = pd.read_csv('/content/diabetes (1).csv')\n",
        "print(df.head())\n",
        "\n",
        "# ===============================\n",
        "# Handle Missing Values\n",
        "# ===============================\n",
        "df.fillna(df.mean(), inplace=True)\n",
        "\n",
        "# ===============================\n",
        "# Split Features and Target\n",
        "# ===============================\n",
        "X = df.drop('Outcome', axis=1)\n",
        "y = df['Outcome']\n",
        "\n",
        "# ===============================\n",
        "# Train-Test Split (Original Data)\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",
        "\n",
        "# ===============================\n",
        "# Random Forest - Original Features\n",
        "# ===============================\n",
        "rf = RandomForestClassifier(random_state=42)\n",
        "rf.fit(X_train, y_train)\n",
        "y_pred_rf = rf.predict(X_test)\n",
        "\n",
        "rf_acc = accuracy_score(y_test, y_pred_rf)\n",
        "rf_prec = precision_score(y_test, y_pred_rf)\n",
        "rf_rec = recall_score(y_test, y_pred_rf)\n",
        "rf_f1 = f1_score(y_test, y_pred_rf)\n",
        "\n",
        "# ===============================\n",
        "# Feature Importance & Selection\n",
        "# ===============================\n",
        "feature_importance = pd.DataFrame({\n",
        "    'Feature': X.columns,\n",
        "    'Importance': rf.feature_importances_\n",
        "}).sort_values(by='Importance', ascending=False)\n",
        "\n",
        "top_features = feature_importance['Feature'][:5]\n",
        "X_top = X[top_features]\n",
        "\n",
        "X_train, X_test, y_train, y_test = train_test_split(\n",
        "    X_top, y, test_size=0.2, random_state=42\n",
        ")\n",
        "\n",
        "rf_top = RandomForestClassifier(random_state=42)\n",
        "rf_top.fit(X_train, y_train)\n",
        "y_pred_top = rf_top.predict(X_test)\n",
        "\n",
        "rf_top_acc = accuracy_score(y_test, y_pred_top)\n",
        "rf_top_prec = precision_score(y_test, y_pred_top)\n",
        "rf_top_rec = recall_score(y_test, y_pred_top)\n",
        "rf_top_f1 = f1_score(y_test, y_pred_top)\n",
        "\n",
        "# ===============================\n",
        "# PCA Feature Reduction\n",
        "# ===============================\n",
        "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, X_test, y_train, y_test = 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, y_train)\n",
        "y_pred_pca = rf_pca.predict(X_test)\n",
        "\n",
        "rf_pca_acc = accuracy_score(y_test, y_pred_pca)\n",
        "rf_pca_prec = precision_score(y_test, y_pred_pca)\n",
        "rf_pca_rec = recall_score(y_test, y_pred_pca)\n",
        "rf_pca_f1 = f1_score(y_test, y_pred_pca)\n",
        "\n",
        "# ===============================\n",
        "# Logistic Regression\n",
        "# ===============================\n",
        "lr = LogisticRegression(max_iter=1000)\n",
        "lr.fit(X_train, y_train)\n",
        "y_pred_lr = lr.predict(X_test)\n",
        "\n",
        "lr_acc = accuracy_score(y_test, y_pred_lr)\n",
        "lr_prec = precision_score(y_test, y_pred_lr)\n",
        "lr_rec = recall_score(y_test, y_pred_lr)\n",
        "lr_f1 = f1_score(y_test, y_pred_lr)\n",
        "\n",
        "# ===============================\n",
        "# Naive Bayes\n",
        "# ===============================\n",
        "nb = GaussianNB()\n",
        "nb.fit(X_train, y_train)\n",
        "y_pred_nb = nb.predict(X_test)\n",
        "\n",
        "nb_acc = accuracy_score(y_test, y_pred_nb)\n",
        "nb_prec = precision_score(y_test, y_pred_nb)\n",
        "nb_rec = recall_score(y_test, y_pred_nb)\n",
        "nb_f1 = f1_score(y_test, y_pred_nb)\n",
        "\n",
        "# ===============================\n",
        "# Results Comparison\n",
        "# ===============================\n",
        "results = pd.DataFrame({\n",
        "    'Model': [\n",
        "        'Random Forest (Original)',\n",
        "        'Random Forest (Top Features)',\n",
        "        'Random Forest (PCA)',\n",
        "        'Logistic Regression',\n",
        "        'Naive Bayes'\n",
        "    ],\n",
        "    'Accuracy': [\n",
        "        rf_acc,\n",
        "        rf_top_acc,\n",
        "        rf_pca_acc,\n",
        "        lr_acc,\n",
        "        nb_acc\n",
        "    ],\n",
        "    'Precision': [\n",
        "        rf_prec,\n",
        "        rf_top_prec,\n",
        "        rf_pca_prec,\n",
        "        lr_prec,\n",
        "        nb_prec\n",
        "    ],\n",
        "    'Recall': [\n",
        "        rf_rec,\n",
        "        rf_top_rec,\n",
        "        rf_pca_rec,\n",
        "        lr_rec,\n",
        "        nb_rec\n",
        "    ],\n",
        "    'F1 Score': [\n",
        "        rf_f1,\n",
        "        rf_top_f1,\n",
        "        rf_pca_f1,\n",
        "        lr_f1,\n",
        "        nb_f1\n",
        "    ]\n",
        "})\n",
        "\n",
        "print(\"\\nFeature Importance:\")\n",
        "print(feature_importance)\n",
        "\n",
        "print(\"\\nModel Comparison Results:\")\n",
        "print(results)"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "The analysis shows that glucose level is the most important factor in predicting diabetes, followed by BMI, age, and family history. This matches medical knowledge, as high blood sugar, obesity, and increasing age increase the risk of diabetes. Other features such as blood pressure, pregnancies, insulin, and skin thickness have less impact on the prediction.\n",
        "\n",
        "Among all models, the Random Forest model using selected top features performed the best, giving higher accuracy and balanced precision and recall. Models like Logistic Regression and Naive Bayes showed reasonable results but were less reliable for identifying all diabetic cases. The PCA-based model performed the worst, as it removed important information. Overall, Random Forest with important features is the most suitable model for this dataset."
      ],
      "metadata": {
        "id": "65XkIobEfVIx"
      }
    }
  ]
}