{
  "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": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "oKnyyIujGia-",
        "outputId": "080d965b-ba5b-4ea3-ea68-6b67e7f15b0d"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Dataset shape: (768, 10)\n",
            "\n",
            "Columns: ['PatientId', 'Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome']\n",
            "\n",
            "Target distribution:\n",
            " Outcome\n",
            "0    0.651042\n",
            "1    0.348958\n",
            "Name: proportion, dtype: float64\n",
            "\n",
            "Decision Tree\n",
            "---------------------------------------------\n",
            "\n",
            "Classification Report (Test set):\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           0       0.75      0.77      0.76       100\n",
            "           1       0.56      0.54      0.55        54\n",
            "\n",
            "    accuracy                           0.69       154\n",
            "   macro avg       0.66      0.65      0.65       154\n",
            "weighted avg       0.69      0.69      0.69       154\n",
            "\n",
            "Confusion Matrix (Test set):\n",
            "[[77 23]\n",
            " [25 29]]\n",
            "Training Accuracy : 1.0000\n",
            "Test Accuracy     : 0.6883\n",
            "Precision (1)     : 0.5577\n",
            "Recall (1)        : 0.5370\n",
            "F1-score (1)      : 0.5472\n",
            "\n",
            "Random Forest\n",
            "---------------------------------------------\n",
            "\n",
            "Classification Report (Test set):\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           0       0.79      0.82      0.80       100\n",
            "           1       0.64      0.59      0.62        54\n",
            "\n",
            "    accuracy                           0.74       154\n",
            "   macro avg       0.71      0.71      0.71       154\n",
            "weighted avg       0.74      0.74      0.74       154\n",
            "\n",
            "Confusion Matrix (Test set):\n",
            "[[82 18]\n",
            " [22 32]]\n",
            "Training Accuracy : 1.0000\n",
            "Test Accuracy     : 0.7403\n",
            "Precision (1)     : 0.6400\n",
            "Recall (1)        : 0.5926\n",
            "F1-score (1)      : 0.6154\n",
            "\n",
            "Naive Bayes\n",
            "---------------------------------------------\n",
            "\n",
            "Classification Report (Test set):\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           0       0.79      0.76      0.78       100\n",
            "           1       0.59      0.63      0.61        54\n",
            "\n",
            "    accuracy                           0.71       154\n",
            "   macro avg       0.69      0.69      0.69       154\n",
            "weighted avg       0.72      0.71      0.72       154\n",
            "\n",
            "Confusion Matrix (Test set):\n",
            "[[76 24]\n",
            " [20 34]]\n",
            "Training Accuracy : 0.7752\n",
            "Test Accuracy     : 0.7143\n",
            "Precision (1)     : 0.5862\n",
            "Recall (1)        : 0.6296\n",
            "F1-score (1)      : 0.6071\n",
            "\n",
            "SVM (Linear)\n",
            "---------------------------------------------\n",
            "\n",
            "Classification Report (Test set):\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           0       0.75      0.83      0.79       100\n",
            "           1       0.61      0.50      0.55        54\n",
            "\n",
            "    accuracy                           0.71       154\n",
            "   macro avg       0.68      0.67      0.67       154\n",
            "weighted avg       0.71      0.71      0.71       154\n",
            "\n",
            "Confusion Matrix (Test set):\n",
            "[[83 17]\n",
            " [27 27]]\n",
            "Training Accuracy : 0.7948\n",
            "Test Accuracy     : 0.7143\n",
            "Precision (1)     : 0.6136\n",
            "Recall (1)        : 0.5000\n",
            "F1-score (1)      : 0.5510\n",
            "\n",
            "SVM (RBF)\n",
            "---------------------------------------------\n",
            "\n",
            "Classification Report (Test set):\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           0       0.70      0.87      0.77       100\n",
            "           1       0.55      0.30      0.39        54\n",
            "\n",
            "    accuracy                           0.67       154\n",
            "   macro avg       0.62      0.58      0.58       154\n",
            "weighted avg       0.65      0.67      0.64       154\n",
            "\n",
            "Confusion Matrix (Test set):\n",
            "[[87 13]\n",
            " [38 16]]\n",
            "Training Accuracy : 0.7443\n",
            "Test Accuracy     : 0.6688\n",
            "Precision (1)     : 0.5517\n",
            "Recall (1)        : 0.2963\n",
            "F1-score (1)      : 0.3855\n"
          ]
        }
      ],
      "source": [
        "# ==============================\n",
        "# Step 1: Import Libraries\n",
        "# ==============================\n",
        "import pandas as pd\n",
        "import numpy as np\n",
        "\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n",
        "\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",
        "\n",
        "\n",
        "# ==============================\n",
        "# Step 2: Load Dataset\n",
        "# ==============================\n",
        "df = pd.read_csv(\"/content/diabetes.csv\")\n",
        "\n",
        "print(\"Dataset shape:\", df.shape)\n",
        "print(\"\\nColumns:\", df.columns.tolist())\n",
        "print(\"\\nTarget distribution:\\n\", df['Outcome'].value_counts(normalize=True))\n",
        "\n",
        "\n",
        "# ==============================\n",
        "# Step 3: Prepare Data\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,\n",
        "    test_size=0.20,\n",
        "    random_state=42,\n",
        "    stratify=y\n",
        ")\n",
        "\n",
        "\n",
        "# ==============================\n",
        "# Step 4: Evaluation Function\n",
        "# (Diabetes class = 1)\n",
        "# ==============================\n",
        "def evaluate_model_diabetes(model, model_name):\n",
        "    model.fit(X_train, y_train)\n",
        "\n",
        "    y_train_pred = model.predict(X_train)\n",
        "    y_test_pred  = model.predict(X_test)\n",
        "\n",
        "    train_acc = accuracy_score(y_train, y_train_pred)\n",
        "    test_acc  = accuracy_score(y_test, y_test_pred)\n",
        "\n",
        "    print(f\"\\n{model_name}\")\n",
        "    print(\"-\" * 45)\n",
        "\n",
        "    print(\"\\nClassification Report (Test set):\")\n",
        "    print(classification_report(y_test, y_test_pred))\n",
        "\n",
        "    print(\"Confusion Matrix (Test set):\")\n",
        "    print(confusion_matrix(y_test, y_test_pred))\n",
        "\n",
        "    report = classification_report(y_test, y_test_pred, output_dict=True)\n",
        "\n",
        "    precision = report['1']['precision']\n",
        "    recall    = report['1']['recall']\n",
        "    f1        = report['1']['f1-score']\n",
        "\n",
        "    print(f\"Training Accuracy : {train_acc:.4f}\")\n",
        "    print(f\"Test Accuracy     : {test_acc:.4f}\")\n",
        "    print(f\"Precision (1)     : {precision:.4f}\")\n",
        "    print(f\"Recall (1)        : {recall:.4f}\")\n",
        "    print(f\"F1-score (1)      : {f1:.4f}\")\n",
        "\n",
        "\n",
        "# ==============================\n",
        "# Step 5: Train & Evaluate Models\n",
        "# ==============================\n",
        "\n",
        "# Decision Tree\n",
        "evaluate_model_diabetes(\n",
        "    DecisionTreeClassifier(random_state=42),\n",
        "    \"Decision Tree\"\n",
        ")\n",
        "\n",
        "# Random Forest\n",
        "evaluate_model_diabetes(\n",
        "    RandomForestClassifier(n_estimators=100, random_state=42),\n",
        "    \"Random Forest\"\n",
        ")\n",
        "\n",
        "# Naive Bayes\n",
        "evaluate_model_diabetes(\n",
        "    GaussianNB(),\n",
        "    \"Naive Bayes\"\n",
        ")\n",
        "\n",
        "# SVM (Linear)\n",
        "evaluate_model_diabetes(\n",
        "    SVC(kernel='linear'),\n",
        "    \"SVM (Linear)\"\n",
        ")\n",
        "\n",
        "# SVM (RBF)\n",
        "evaluate_model_diabetes(\n",
        "    SVC(kernel='rbf'),\n",
        "    \"SVM (RBF)\"\n",
        ")\n"
      ]
    }
  ]
}