{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"mount_file_id":"1j1yObtD_a49L7dT7grndQLARBD2vzee_","authorship_tag":"ABX9TyMX98kjm6Oo4c++pRYU4gfp"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":null,"metadata":{"id":"kOq8P8tFoAy7"},"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 accuracy_score, classification_report, confusion_matrix\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"]},{"cell_type":"code","source":["df = pd.read_csv('/content/drive/MyDrive/Application of Machine Learning/diabetes.csv')\n","df.head()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"id":"5kx2-A4foaUS","executionInfo":{"status":"ok","timestamp":1770141307041,"user_tz":0,"elapsed":43,"user":{"displayName":"Vishvaraj Rana","userId":"10986470577049836064"}},"outputId":"86b5beda-da0f-4d92-895f-67cc6c1ec47a"},"execution_count":4,"outputs":[{"output_type":"execute_result","data":{"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  "],"text/html":["\n","  <div id=\"df-c5f73bb7-ace8-41a0-8211-c5bc483a52c5\" class=\"colab-df-container\">\n","    <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>\n","    <div class=\"colab-df-buttons\">\n","\n","  <div class=\"colab-df-container\">\n","    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-c5f73bb7-ace8-41a0-8211-c5bc483a52c5')\"\n","            title=\"Convert this dataframe to an interactive table.\"\n","            style=\"display:none;\">\n","\n","  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n","    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n","  </svg>\n","    </button>\n","\n","  <style>\n","    .colab-df-container {\n","      display:flex;\n","      gap: 12px;\n","    }\n","\n","    .colab-df-convert {\n","      background-color: #E8F0FE;\n","      border: none;\n","      border-radius: 50%;\n","      cursor: pointer;\n","      display: none;\n","      fill: #1967D2;\n","      height: 32px;\n","      padding: 0 0 0 0;\n","      width: 32px;\n","    }\n","\n","    .colab-df-convert:hover {\n","      background-color: #E2EBFA;\n","      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n","      fill: #174EA6;\n","    }\n","\n","    .colab-df-buttons div {\n","      margin-bottom: 4px;\n","    }\n","\n","    [theme=dark] .colab-df-convert {\n","      background-color: #3B4455;\n","      fill: #D2E3FC;\n","    }\n","\n","    [theme=dark] .colab-df-convert:hover {\n","      background-color: #434B5C;\n","      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n","      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n","      fill: #FFFFFF;\n","    }\n","  </style>\n","\n","    <script>\n","      const buttonEl =\n","        document.querySelector('#df-c5f73bb7-ace8-41a0-8211-c5bc483a52c5 button.colab-df-convert');\n","      buttonEl.style.display =\n","        google.colab.kernel.accessAllowed ? 'block' : 'none';\n","\n","      async function convertToInteractive(key) {\n","        const element = document.querySelector('#df-c5f73bb7-ace8-41a0-8211-c5bc483a52c5');\n","        const dataTable =\n","          await google.colab.kernel.invokeFunction('convertToInteractive',\n","                                                    [key], {});\n","        if (!dataTable) return;\n","\n","        const docLinkHtml = 'Like what you see? Visit the ' +\n","          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n","          + ' to learn more about interactive tables.';\n","        element.innerHTML = '';\n","        dataTable['output_type'] = 'display_data';\n","        await google.colab.output.renderOutput(dataTable, element);\n","        const docLink = document.createElement('div');\n","        docLink.innerHTML = docLinkHtml;\n","        element.appendChild(docLink);\n","      }\n","    </script>\n","  </div>\n","\n","\n","    </div>\n","  </div>\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"df","summary":"{\n  \"name\": \"df\",\n  \"rows\": 768,\n  \"fields\": [\n    {\n      \"column\": \"PatientId\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 221,\n        \"min\": 1,\n        \"max\": 768,\n        \"num_unique_values\": 768,\n        \"samples\": [\n          669,\n          325,\n          625\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Pregnancies\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 3,\n        \"min\": 0,\n        \"max\": 17,\n        \"num_unique_values\": 17,\n        \"samples\": [\n          6,\n          1,\n          3\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Glucose\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 31,\n        \"min\": 0,\n        \"max\": 199,\n        \"num_unique_values\": 136,\n        \"samples\": [\n          151,\n          101,\n          112\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"BloodPressure\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 19,\n        \"min\": 0,\n        \"max\": 122,\n        \"num_unique_values\": 47,\n        \"samples\": [\n          86,\n          46,\n          85\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"SkinThickness\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 15,\n        \"min\": 0,\n        \"max\": 99,\n        \"num_unique_values\": 51,\n        \"samples\": [\n          7,\n          12,\n          48\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Insulin\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 115,\n        \"min\": 0,\n        \"max\": 846,\n        \"num_unique_values\": 186,\n        \"samples\": [\n          52,\n          41,\n          183\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"BMI\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 7.8841603203754405,\n        \"min\": 0.0,\n        \"max\": 67.1,\n        \"num_unique_values\": 248,\n        \"samples\": [\n          19.9,\n          31.0,\n          38.1\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"DiabetesPedigreeFunction\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.33132859501277484,\n        \"min\": 0.078,\n        \"max\": 2.42,\n        \"num_unique_values\": 517,\n        \"samples\": [\n          1.731,\n          0.426,\n          0.138\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Age\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 11,\n        \"min\": 21,\n        \"max\": 81,\n        \"num_unique_values\": 52,\n        \"samples\": [\n          60,\n          47,\n          72\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Outcome\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0,\n        \"min\": 0,\n        \"max\": 1,\n        \"num_unique_values\": 2,\n        \"samples\": [\n          0,\n          1\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}"}},"metadata":{},"execution_count":4}]},{"cell_type":"code","source":["# Features and target\n","X = df.drop('Outcome', axis=1)\n","y = df['Outcome']\n","\n","# 80/20 split\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","# Features and target\n","X = df.drop('Outcome', axis=1)\n","y = df['Outcome']\n","\n","# 80/20 Train-Test Split\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","# Function to Train & Evaluate\n","# ===============================\n","def evaluate_model(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","    print(\"=\"*50)\n","    print(f\"{model_name}\")\n","    print(\"=\"*50)\n","\n","    print(\"Training Accuracy:\", accuracy_score(y_train, y_train_pred))\n","    print(\"Test Accuracy:\", accuracy_score(y_test, y_test_pred))\n","\n","    print(\"\\nClassification Report:\")\n","    print(classification_report(y_test, y_test_pred))\n","\n","    print(\"Confusion Matrix:\")\n","    print(confusion_matrix(y_test, y_test_pred))\n","    print(\"\\n\")\n","\n","    return accuracy_score(y_train, y_train_pred), accuracy_score(y_test, y_test_pred)\n","\n","# ===============================\n","# Step 3: Train Models\n","# ===============================\n","\n","results = []\n","\n","# 1. Decision Tree\n","dt = DecisionTreeClassifier()\n","dt_train_acc, dt_test_acc = evaluate_model(dt, \"Decision Tree\")\n","results.append([\"Decision Tree\", dt_train_acc, dt_test_acc])\n","\n","# 2. Random Forest\n","rf = RandomForestClassifier(n_estimators=100, random_state=42)\n","rf_train_acc, rf_test_acc = evaluate_model(rf, \"Random Forest\")\n","results.append([\"Random Forest\", rf_train_acc, rf_test_acc])\n","\n","# 3. Naïve Bayes\n","nb = GaussianNB()\n","nb_train_acc, nb_test_acc = evaluate_model(nb, \"Naïve Bayes\")\n","results.append([\"Naïve Bayes\", nb_train_acc, nb_test_acc])\n","\n","# 4. SVM - Linear Kernel\n","svm_linear = SVC(kernel='linear')\n","svm_l_train_acc, svm_l_test_acc = evaluate_model(svm_linear, \"SVM (Linear)\")\n","results.append([\"SVM (Linear)\", svm_l_train_acc, svm_l_test_acc])\n","\n","# 5. SVM - RBF Kernel\n","svm_rbf = SVC(kernel='rbf')\n","svm_r_train_acc, svm_r_test_acc = evaluate_model(svm_rbf, \"SVM (RBF)\")\n","results.append([\"SVM (RBF)\", svm_r_train_acc, svm_r_test_acc])\n","\n","# ===============================\n","# Step 4: Summary Table\n","# ===============================\n","summary = pd.DataFrame(\n","    results,\n","    columns=[\"Model\", \"Training Accuracy\", \"Test Accuracy\"]\n",")\n","\n","print(\"FINAL MODEL COMPARISON\")\n","print(summary)\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"0q9rGtQ1pIWW","executionInfo":{"status":"ok","timestamp":1770141974541,"user_tz":0,"elapsed":22533,"user":{"displayName":"Vishvaraj Rana","userId":"10986470577049836064"}},"outputId":"be03c73d-8639-426f-b2f7-7166893f449a"},"execution_count":10,"outputs":[{"output_type":"stream","name":"stdout","text":["==================================================\n","Decision Tree\n","==================================================\n","Training Accuracy: 1.0\n","Test Accuracy: 0.7727272727272727\n","\n","Classification Report:\n","              precision    recall  f1-score   support\n","\n","           0       0.82      0.83      0.82        99\n","           1       0.69      0.67      0.68        55\n","\n","    accuracy                           0.77       154\n","   macro avg       0.75      0.75      0.75       154\n","weighted avg       0.77      0.77      0.77       154\n","\n","Confusion Matrix:\n","[[82 17]\n"," [18 37]]\n","\n","\n","==================================================\n","Random Forest\n","==================================================\n","Training Accuracy: 1.0\n","Test Accuracy: 0.7532467532467533\n","\n","Classification Report:\n","              precision    recall  f1-score   support\n","\n","           0       0.82      0.79      0.80        99\n","           1       0.64      0.69      0.67        55\n","\n","    accuracy                           0.75       154\n","   macro avg       0.73      0.74      0.74       154\n","weighted avg       0.76      0.75      0.76       154\n","\n","Confusion Matrix:\n","[[78 21]\n"," [17 38]]\n","\n","\n","==================================================\n","Naïve Bayes\n","==================================================\n","Training Accuracy: 0.752442996742671\n","Test Accuracy: 0.7597402597402597\n","\n","Classification Report:\n","              precision    recall  f1-score   support\n","\n","           0       0.83      0.79      0.81        99\n","           1       0.65      0.71      0.68        55\n","\n","    accuracy                           0.76       154\n","   macro avg       0.74      0.75      0.74       154\n","weighted avg       0.77      0.76      0.76       154\n","\n","Confusion Matrix:\n","[[78 21]\n"," [16 39]]\n","\n","\n","==================================================\n","SVM (Linear)\n","==================================================\n","Training Accuracy: 0.7736156351791531\n","Test Accuracy: 0.7662337662337663\n","\n","Classification Report:\n","              precision    recall  f1-score   support\n","\n","           0       0.82      0.82      0.82        99\n","           1       0.67      0.67      0.67        55\n","\n","    accuracy                           0.77       154\n","   macro avg       0.75      0.75      0.75       154\n","weighted avg       0.77      0.77      0.77       154\n","\n","Confusion Matrix:\n","[[81 18]\n"," [18 37]]\n","\n","\n","==================================================\n","SVM (RBF)\n","==================================================\n","Training Accuracy: 0.744299674267101\n","Test Accuracy: 0.7467532467532467\n","\n","Classification Report:\n","              precision    recall  f1-score   support\n","\n","           0       0.75      0.92      0.82        99\n","           1       0.75      0.44      0.55        55\n","\n","    accuracy                           0.75       154\n","   macro avg       0.75      0.68      0.69       154\n","weighted avg       0.75      0.75      0.73       154\n","\n","Confusion Matrix:\n","[[91  8]\n"," [31 24]]\n","\n","\n","FINAL MODEL COMPARISON\n","           Model  Training Accuracy  Test Accuracy\n","0  Decision Tree           1.000000       0.772727\n","1  Random Forest           1.000000       0.753247\n","2    Naïve Bayes           0.752443       0.759740\n","3   SVM (Linear)           0.773616       0.766234\n","4      SVM (RBF)           0.744300       0.746753\n"]}]}]}