Diff of /diabetes_model.ipynb [000000] .. [b018ba]

Switch to unified view

a b/diabetes_model.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "markdown",
5
   "id": "9af65cb9-8a84-47e4-8bea-1547afe46a15",
6
   "metadata": {},
7
   "source": [
8
    "DIABETIES"
9
   ]
10
  },
11
  {
12
   "cell_type": "code",
13
   "execution_count": 2,
14
   "id": "3fba3c9b-5e48-4771-a28a-4ec54fc4bc1b",
15
   "metadata": {},
16
   "outputs": [
17
    {
18
     "data": {
19
      "text/plain": [
20
       "Pregnancies                 0\n",
21
       "Glucose                     0\n",
22
       "BloodPressure               0\n",
23
       "SkinThickness               0\n",
24
       "Insulin                     0\n",
25
       "BMI                         0\n",
26
       "DiabetesPedigreeFunction    0\n",
27
       "Age                         0\n",
28
       "Outcome                     0\n",
29
       "dtype: int64"
30
      ]
31
     },
32
     "execution_count": 2,
33
     "metadata": {},
34
     "output_type": "execute_result"
35
    }
36
   ],
37
   "source": [
38
    "import numpy as np\n",
39
    "import pandas as pd\n",
40
    "from sklearn.model_selection import train_test_split\n",
41
    "from sklearn import svm\n",
42
    "from sklearn.metrics import accuracy_score\n",
43
    "dataset= pd.read_csv(r'C:\\Users\\Pranshu Saini\\Desktop\\disease-prediction-main\\docpat\\datasets\\diabetes.csv')\n",
44
    "dataset.head(5)\n",
45
    "dataset.isna().sum()"
46
   ]
47
  },
48
  {
49
   "cell_type": "code",
50
   "execution_count": 4,
51
   "id": "e99dd297-c606-4501-80c8-fa87d89fc237",
52
   "metadata": {},
53
   "outputs": [
54
    {
55
     "name": "stdout",
56
     "output_type": "stream",
57
     "text": [
58
      "The reduced dataframe has 9 columns.\n"
59
     ]
60
    }
61
   ],
62
   "source": [
63
    "# removing highly correlated features\n",
64
    "\n",
65
    "corr_matrix = dataset.corr().abs() \n",
66
    "\n",
67
    "mask = np.triu(np.ones_like(corr_matrix, dtype = bool))\n",
68
    "tri_df = corr_matrix.mask(mask)\n",
69
    "\n",
70
    "to_drop = [x for x in tri_df.columns if any(tri_df[x] > 0.92)]\n",
71
    "\n",
72
    "df = dataset.drop(to_drop, axis = 1)\n",
73
    "\n",
74
    "print(f\"The reduced dataframe has {df.shape[1]} columns.\")"
75
   ]
76
  },
77
  {
78
   "cell_type": "code",
79
   "execution_count": 5,
80
   "id": "64693a8e",
81
   "metadata": {},
82
   "outputs": [
83
    {
84
     "data": {
85
      "text/html": [
86
       "<div>\n",
87
       "<style scoped>\n",
88
       "    .dataframe tbody tr th:only-of-type {\n",
89
       "        vertical-align: middle;\n",
90
       "    }\n",
91
       "\n",
92
       "    .dataframe tbody tr th {\n",
93
       "        vertical-align: top;\n",
94
       "    }\n",
95
       "\n",
96
       "    .dataframe thead th {\n",
97
       "        text-align: right;\n",
98
       "    }\n",
99
       "</style>\n",
100
       "<table border=\"1\" class=\"dataframe\">\n",
101
       "  <thead>\n",
102
       "    <tr style=\"text-align: right;\">\n",
103
       "      <th></th>\n",
104
       "      <th>Pregnancies</th>\n",
105
       "      <th>Glucose</th>\n",
106
       "      <th>BloodPressure</th>\n",
107
       "      <th>SkinThickness</th>\n",
108
       "      <th>Insulin</th>\n",
109
       "      <th>BMI</th>\n",
110
       "      <th>DiabetesPedigreeFunction</th>\n",
111
       "      <th>Age</th>\n",
112
       "      <th>Outcome</th>\n",
113
       "    </tr>\n",
114
       "  </thead>\n",
115
       "  <tbody>\n",
116
       "    <tr>\n",
117
       "      <th>0</th>\n",
118
       "      <td>6</td>\n",
119
       "      <td>148</td>\n",
120
       "      <td>72</td>\n",
121
       "      <td>35</td>\n",
122
       "      <td>0</td>\n",
123
       "      <td>33.6</td>\n",
124
       "      <td>0.627</td>\n",
125
       "      <td>50</td>\n",
126
       "      <td>1</td>\n",
127
       "    </tr>\n",
128
       "    <tr>\n",
129
       "      <th>1</th>\n",
130
       "      <td>1</td>\n",
131
       "      <td>85</td>\n",
132
       "      <td>66</td>\n",
133
       "      <td>29</td>\n",
134
       "      <td>0</td>\n",
135
       "      <td>26.6</td>\n",
136
       "      <td>0.351</td>\n",
137
       "      <td>31</td>\n",
138
       "      <td>0</td>\n",
139
       "    </tr>\n",
140
       "    <tr>\n",
141
       "      <th>2</th>\n",
142
       "      <td>8</td>\n",
143
       "      <td>183</td>\n",
144
       "      <td>64</td>\n",
145
       "      <td>0</td>\n",
146
       "      <td>0</td>\n",
147
       "      <td>23.3</td>\n",
148
       "      <td>0.672</td>\n",
149
       "      <td>32</td>\n",
150
       "      <td>1</td>\n",
151
       "    </tr>\n",
152
       "    <tr>\n",
153
       "      <th>3</th>\n",
154
       "      <td>1</td>\n",
155
       "      <td>89</td>\n",
156
       "      <td>66</td>\n",
157
       "      <td>23</td>\n",
158
       "      <td>94</td>\n",
159
       "      <td>28.1</td>\n",
160
       "      <td>0.167</td>\n",
161
       "      <td>21</td>\n",
162
       "      <td>0</td>\n",
163
       "    </tr>\n",
164
       "    <tr>\n",
165
       "      <th>4</th>\n",
166
       "      <td>0</td>\n",
167
       "      <td>137</td>\n",
168
       "      <td>40</td>\n",
169
       "      <td>35</td>\n",
170
       "      <td>168</td>\n",
171
       "      <td>43.1</td>\n",
172
       "      <td>2.288</td>\n",
173
       "      <td>33</td>\n",
174
       "      <td>1</td>\n",
175
       "    </tr>\n",
176
       "    <tr>\n",
177
       "      <th>...</th>\n",
178
       "      <td>...</td>\n",
179
       "      <td>...</td>\n",
180
       "      <td>...</td>\n",
181
       "      <td>...</td>\n",
182
       "      <td>...</td>\n",
183
       "      <td>...</td>\n",
184
       "      <td>...</td>\n",
185
       "      <td>...</td>\n",
186
       "      <td>...</td>\n",
187
       "    </tr>\n",
188
       "    <tr>\n",
189
       "      <th>763</th>\n",
190
       "      <td>10</td>\n",
191
       "      <td>101</td>\n",
192
       "      <td>76</td>\n",
193
       "      <td>48</td>\n",
194
       "      <td>180</td>\n",
195
       "      <td>32.9</td>\n",
196
       "      <td>0.171</td>\n",
197
       "      <td>63</td>\n",
198
       "      <td>0</td>\n",
199
       "    </tr>\n",
200
       "    <tr>\n",
201
       "      <th>764</th>\n",
202
       "      <td>2</td>\n",
203
       "      <td>122</td>\n",
204
       "      <td>70</td>\n",
205
       "      <td>27</td>\n",
206
       "      <td>0</td>\n",
207
       "      <td>36.8</td>\n",
208
       "      <td>0.340</td>\n",
209
       "      <td>27</td>\n",
210
       "      <td>0</td>\n",
211
       "    </tr>\n",
212
       "    <tr>\n",
213
       "      <th>765</th>\n",
214
       "      <td>5</td>\n",
215
       "      <td>121</td>\n",
216
       "      <td>72</td>\n",
217
       "      <td>23</td>\n",
218
       "      <td>112</td>\n",
219
       "      <td>26.2</td>\n",
220
       "      <td>0.245</td>\n",
221
       "      <td>30</td>\n",
222
       "      <td>0</td>\n",
223
       "    </tr>\n",
224
       "    <tr>\n",
225
       "      <th>766</th>\n",
226
       "      <td>1</td>\n",
227
       "      <td>126</td>\n",
228
       "      <td>60</td>\n",
229
       "      <td>0</td>\n",
230
       "      <td>0</td>\n",
231
       "      <td>30.1</td>\n",
232
       "      <td>0.349</td>\n",
233
       "      <td>47</td>\n",
234
       "      <td>1</td>\n",
235
       "    </tr>\n",
236
       "    <tr>\n",
237
       "      <th>767</th>\n",
238
       "      <td>1</td>\n",
239
       "      <td>93</td>\n",
240
       "      <td>70</td>\n",
241
       "      <td>31</td>\n",
242
       "      <td>0</td>\n",
243
       "      <td>30.4</td>\n",
244
       "      <td>0.315</td>\n",
245
       "      <td>23</td>\n",
246
       "      <td>0</td>\n",
247
       "    </tr>\n",
248
       "  </tbody>\n",
249
       "</table>\n",
250
       "<p>768 rows × 9 columns</p>\n",
251
       "</div>"
252
      ],
253
      "text/plain": [
254
       "     Pregnancies  Glucose  BloodPressure  SkinThickness  Insulin   BMI  \\\n",
255
       "0              6      148             72             35        0  33.6   \n",
256
       "1              1       85             66             29        0  26.6   \n",
257
       "2              8      183             64              0        0  23.3   \n",
258
       "3              1       89             66             23       94  28.1   \n",
259
       "4              0      137             40             35      168  43.1   \n",
260
       "..           ...      ...            ...            ...      ...   ...   \n",
261
       "763           10      101             76             48      180  32.9   \n",
262
       "764            2      122             70             27        0  36.8   \n",
263
       "765            5      121             72             23      112  26.2   \n",
264
       "766            1      126             60              0        0  30.1   \n",
265
       "767            1       93             70             31        0  30.4   \n",
266
       "\n",
267
       "     DiabetesPedigreeFunction  Age  Outcome  \n",
268
       "0                       0.627   50        1  \n",
269
       "1                       0.351   31        0  \n",
270
       "2                       0.672   32        1  \n",
271
       "3                       0.167   21        0  \n",
272
       "4                       2.288   33        1  \n",
273
       "..                        ...  ...      ...  \n",
274
       "763                     0.171   63        0  \n",
275
       "764                     0.340   27        0  \n",
276
       "765                     0.245   30        0  \n",
277
       "766                     0.349   47        1  \n",
278
       "767                     0.315   23        0  \n",
279
       "\n",
280
       "[768 rows x 9 columns]"
281
      ]
282
     },
283
     "execution_count": 5,
284
     "metadata": {},
285
     "output_type": "execute_result"
286
    }
287
   ],
288
   "source": [
289
    "df"
290
   ]
291
  },
292
  {
293
   "cell_type": "code",
294
   "execution_count": null,
295
   "id": "ecc668ff-a516-4ee6-8b2f-76b07d9df34f",
296
   "metadata": {},
297
   "outputs": [
298
    {
299
     "name": "stdout",
300
     "output_type": "stream",
301
     "text": [
302
      "(768, 8) (614, 8) (154, 8)\n"
303
     ]
304
    }
305
   ],
306
   "source": [
307
    "A= dataset.drop(columns = 'Outcome', axis=1)\n",
308
    "B= dataset['Outcome']\n",
309
    "A_training, A_testing, B_training, B_testing = train_test_split(A,B, test_size = 0.2, stratify=B, random_state=5)\n",
310
    "print(A.shape, A_training.shape, A_testing.shape)\n"
311
   ]
312
  },
313
  {
314
   "cell_type": "markdown",
315
   "id": "62af783b-d901-479d-bf3f-512a897fceaa",
316
   "metadata": {},
317
   "source": [
318
    "LogisticRegression"
319
   ]
320
  },
321
  {
322
   "cell_type": "code",
323
   "execution_count": null,
324
   "id": "69152bd3-c30b-46b4-bf8e-32dbcd33e163",
325
   "metadata": {},
326
   "outputs": [
327
    {
328
     "name": "stdout",
329
     "output_type": "stream",
330
     "text": [
331
      "0.7817589576547231\n",
332
      "0.7532467532467533\n"
333
     ]
334
    },
335
    {
336
     "name": "stderr",
337
     "output_type": "stream",
338
     "text": [
339
      "c:\\Users\\Dell\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\sklearn\\linear_model\\_logistic.py:460: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
340
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
341
      "\n",
342
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
343
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
344
      "Please also refer to the documentation for alternative solver options:\n",
345
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
346
      "  n_iter_i = _check_optimize_result(\n"
347
     ]
348
    }
349
   ],
350
   "source": [
351
    "# fitting data to model\n",
352
    "\n",
353
    "from sklearn.linear_model import LogisticRegression\n",
354
    "\n",
355
    "log_reg = LogisticRegression()\n",
356
    "log_reg.fit(A_training, B_training)\n",
357
    "B_pred = log_reg.predict(A_testing)\n",
358
    "# accuracy score\n",
359
    "\n",
360
    "from sklearn.metrics import accuracy_score, confusion_matrix, classification_report\n",
361
    "\n",
362
    "print(accuracy_score(B_training, log_reg.predict(A_training)))\n",
363
    "\n",
364
    "log_reg_acc = accuracy_score(B_testing, log_reg.predict(A_testing))\n",
365
    "print(log_reg_acc)"
366
   ]
367
  },
368
  {
369
   "cell_type": "markdown",
370
   "id": "90967102-86d1-4939-9113-4d06ce5bb054",
371
   "metadata": {},
372
   "source": [
373
    "K Neighbors Classifier (KNN)\n"
374
   ]
375
  },
376
  {
377
   "cell_type": "code",
378
   "execution_count": null,
379
   "id": "2092a5bc-4602-4aa6-adb3-34ee677f8134",
380
   "metadata": {},
381
   "outputs": [
382
    {
383
     "name": "stdout",
384
     "output_type": "stream",
385
     "text": [
386
      "0.7980456026058632\n",
387
      "0.7142857142857143\n"
388
     ]
389
    }
390
   ],
391
   "source": [
392
    "from sklearn.neighbors import KNeighborsClassifier\n",
393
    "\n",
394
    "knn = KNeighborsClassifier()\n",
395
    "knn.fit(A_training, B_training)\n",
396
    "# model predictions \n",
397
    "\n",
398
    "B_pred = knn.predict(A_testing)\n",
399
    "# accuracy score\n",
400
    "\n",
401
    "print(accuracy_score(B_training, knn.predict(A_training)))\n",
402
    "\n",
403
    "knn_acc = accuracy_score(B_testing, knn.predict(A_testing))\n",
404
    "print(knn_acc)"
405
   ]
406
  },
407
  {
408
   "cell_type": "markdown",
409
   "id": "da68f26d-4291-4076-bf2c-f20dd524c6e7",
410
   "metadata": {},
411
   "source": [
412
    "Support Vector Machine (SVM)"
413
   ]
414
  },
415
  {
416
   "cell_type": "code",
417
   "execution_count": null,
418
   "id": "a27c4a3c-15e7-492d-88a6-526ddfc968cd",
419
   "metadata": {},
420
   "outputs": [
421
    {
422
     "data": {
423
      "text/plain": [
424
       "{'C': 1, 'gamma': 0.0001}"
425
      ]
426
     },
427
     "execution_count": 8,
428
     "metadata": {},
429
     "output_type": "execute_result"
430
    }
431
   ],
432
   "source": [
433
    "from sklearn.svm import SVC\n",
434
    "from sklearn.model_selection import GridSearchCV\n",
435
    "\n",
436
    "svc = SVC(probability=True)\n",
437
    "parameters = {\n",
438
    "    'gamma' : [0.0001, 0.001, 0.01, 0.1],\n",
439
    "    'C' : [0.01, 0.05, 0.5, 0.1, 1, 10, 15, 20]\n",
440
    "}\n",
441
    "\n",
442
    "grid_search = GridSearchCV(svc, parameters)\n",
443
    "grid_search.fit(A_training, B_training)\n",
444
    "# best parameters\n",
445
    "\n",
446
    "grid_search.best_params_\n",
447
    "\n"
448
   ]
449
  },
450
  {
451
   "cell_type": "code",
452
   "execution_count": null,
453
   "id": "d8da6a6f-eba3-4a66-b61f-04a58313de41",
454
   "metadata": {},
455
   "outputs": [
456
    {
457
     "data": {
458
      "text/plain": [
459
       "0.7557643609222977"
460
      ]
461
     },
462
     "execution_count": 9,
463
     "metadata": {},
464
     "output_type": "execute_result"
465
    }
466
   ],
467
   "source": [
468
    "# best score \n",
469
    "\n",
470
    "grid_search.best_score_"
471
   ]
472
  },
473
  {
474
   "cell_type": "code",
475
   "execution_count": null,
476
   "id": "0627d35c-f004-499b-be77-0fe0380aa002",
477
   "metadata": {},
478
   "outputs": [
479
    {
480
     "name": "stdout",
481
     "output_type": "stream",
482
     "text": [
483
      "1.0\n",
484
      "0.6428571428571429\n",
485
      "              precision    recall  f1-score   support\n",
486
      "\n",
487
      "           0       0.66      0.93      0.77       100\n",
488
      "           1       0.46      0.11      0.18        54\n",
489
      "\n",
490
      "    accuracy                           0.64       154\n",
491
      "   macro avg       0.56      0.52      0.48       154\n",
492
      "weighted avg       0.59      0.64      0.56       154\n",
493
      "\n"
494
     ]
495
    }
496
   ],
497
   "source": [
498
    "svc = SVC(C = 10, gamma = 0.01, probability=True)\n",
499
    "svc.fit(A_training, B_training)\n",
500
    "# model predictions \n",
501
    "\n",
502
    "B_pred = svc.predict(A_testing)\n",
503
    "# accuracy score\n",
504
    "\n",
505
    "print(accuracy_score(B_training, svc.predict(A_training)))\n",
506
    "\n",
507
    "svc_acc = accuracy_score(B_testing, svc.predict(A_testing))\n",
508
    "print(svc_acc)\n",
509
    "# classification report\n",
510
    "\n",
511
    "print(classification_report(B_testing, B_pred))"
512
   ]
513
  },
514
  {
515
   "cell_type": "markdown",
516
   "id": "e4b77944-3ceb-4b80-a738-e8e5a99e009d",
517
   "metadata": {},
518
   "source": [
519
    "DECISION TREE"
520
   ]
521
  },
522
  {
523
   "cell_type": "code",
524
   "execution_count": null,
525
   "id": "fe80f32d-ba91-4297-a9d7-6232a48b434c",
526
   "metadata": {},
527
   "outputs": [
528
    {
529
     "name": "stdout",
530
     "output_type": "stream",
531
     "text": [
532
      "Fitting 5 folds for each of 8640 candidates, totalling 43200 fits\n"
533
     ]
534
    },
535
    {
536
     "data": {
537
      "text/html": [
538
       "<style>#sk-container-id-1 {color: black;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>GridSearchCV(cv=5, estimator=DecisionTreeClassifier(), n_jobs=-1,\n",
539
       "             param_grid={&#x27;criterion&#x27;: [&#x27;gini&#x27;, &#x27;entropy&#x27;],\n",
540
       "                         &#x27;max_depth&#x27;: range(2, 32),\n",
541
       "                         &#x27;min_samples_leaf&#x27;: range(1, 10),\n",
542
       "                         &#x27;min_samples_split&#x27;: range(2, 10),\n",
543
       "                         &#x27;splitter&#x27;: [&#x27;best&#x27;, &#x27;random&#x27;]},\n",
544
       "             verbose=1)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item sk-dashed-wrapped\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" ><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">GridSearchCV</label><div class=\"sk-toggleable__content\"><pre>GridSearchCV(cv=5, estimator=DecisionTreeClassifier(), n_jobs=-1,\n",
545
       "             param_grid={&#x27;criterion&#x27;: [&#x27;gini&#x27;, &#x27;entropy&#x27;],\n",
546
       "                         &#x27;max_depth&#x27;: range(2, 32),\n",
547
       "                         &#x27;min_samples_leaf&#x27;: range(1, 10),\n",
548
       "                         &#x27;min_samples_split&#x27;: range(2, 10),\n",
549
       "                         &#x27;splitter&#x27;: [&#x27;best&#x27;, &#x27;random&#x27;]},\n",
550
       "             verbose=1)</pre></div></div></div><div class=\"sk-parallel\"><div class=\"sk-parallel-item\"><div class=\"sk-item\"><div class=\"sk-label-container\"><div class=\"sk-label sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" ><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">estimator: DecisionTreeClassifier</label><div class=\"sk-toggleable__content\"><pre>DecisionTreeClassifier()</pre></div></div></div><div class=\"sk-serial\"><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" ><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">DecisionTreeClassifier</label><div class=\"sk-toggleable__content\"><pre>DecisionTreeClassifier()</pre></div></div></div></div></div></div></div></div></div></div>"
551
      ],
552
      "text/plain": [
553
       "GridSearchCV(cv=5, estimator=DecisionTreeClassifier(), n_jobs=-1,\n",
554
       "             param_grid={'criterion': ['gini', 'entropy'],\n",
555
       "                         'max_depth': range(2, 32),\n",
556
       "                         'min_samples_leaf': range(1, 10),\n",
557
       "                         'min_samples_split': range(2, 10),\n",
558
       "                         'splitter': ['best', 'random']},\n",
559
       "             verbose=1)"
560
      ]
561
     },
562
     "execution_count": 11,
563
     "metadata": {},
564
     "output_type": "execute_result"
565
    }
566
   ],
567
   "source": [
568
    "from sklearn.tree import DecisionTreeClassifier\n",
569
    "\n",
570
    "dtc = DecisionTreeClassifier()\n",
571
    "\n",
572
    "parameters = {\n",
573
    "    'criterion' : ['gini', 'entropy'],\n",
574
    "    'max_depth' : range(2, 32, 1),\n",
575
    "    'min_samples_leaf' : range(1, 10, 1),\n",
576
    "    'min_samples_split' : range(2, 10, 1),\n",
577
    "    'splitter' : ['best', 'random']\n",
578
    "}\n",
579
    "\n",
580
    "grid_search_dt = GridSearchCV(dtc, parameters, cv = 5, n_jobs = -1, verbose = 1)\n",
581
    "grid_search_dt.fit(A_training, B_training)"
582
   ]
583
  },
584
  {
585
   "cell_type": "code",
586
   "execution_count": null,
587
   "id": "3500946f-0e9a-4d31-b076-35c851e1ca69",
588
   "metadata": {},
589
   "outputs": [
590
    {
591
     "data": {
592
      "text/html": [
593
       "<style>#sk-container-id-2 {color: black;}#sk-container-id-2 pre{padding: 0;}#sk-container-id-2 div.sk-toggleable {background-color: white;}#sk-container-id-2 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-2 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-2 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-2 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-2 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-2 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-2 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-2 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-2 div.sk-item {position: relative;z-index: 1;}#sk-container-id-2 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-2 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-2 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-2 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-2 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-2 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-2 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-2 div.sk-label-container {text-align: center;}#sk-container-id-2 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-2 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>DecisionTreeClassifier(criterion=&#x27;entropy&#x27;, max_depth=19, min_samples_leaf=4,\n",
594
       "                       min_samples_split=6, splitter=&#x27;random&#x27;)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-4\" type=\"checkbox\" checked><label for=\"sk-estimator-id-4\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">DecisionTreeClassifier</label><div class=\"sk-toggleable__content\"><pre>DecisionTreeClassifier(criterion=&#x27;entropy&#x27;, max_depth=19, min_samples_leaf=4,\n",
595
       "                       min_samples_split=6, splitter=&#x27;random&#x27;)</pre></div></div></div></div></div>"
596
      ],
597
      "text/plain": [
598
       "DecisionTreeClassifier(criterion='entropy', max_depth=19, min_samples_leaf=4,\n",
599
       "                       min_samples_split=6, splitter='random')"
600
      ]
601
     },
602
     "execution_count": 12,
603
     "metadata": {},
604
     "output_type": "execute_result"
605
    }
606
   ],
607
   "source": [
608
    "# best score\n",
609
    "\n",
610
    "grid_search_dt.best_score_\n",
611
    "dtc = DecisionTreeClassifier(criterion= 'entropy', max_depth= 19, min_samples_leaf= 4, min_samples_split= 6, splitter= 'random')\n",
612
    "dtc.fit(A_training, B_training)"
613
   ]
614
  },
615
  {
616
   "cell_type": "code",
617
   "execution_count": null,
618
   "id": "73fa053a-be12-46d6-9315-fb8d5d557b7c",
619
   "metadata": {},
620
   "outputs": [
621
    {
622
     "name": "stdout",
623
     "output_type": "stream",
624
     "text": [
625
      "0.8224755700325733\n",
626
      "0.6883116883116883\n"
627
     ]
628
    }
629
   ],
630
   "source": [
631
    "B_pred = dtc.predict(A_testing)\n",
632
    "# accuracy score\n",
633
    "\n",
634
    "print(accuracy_score(B_training, dtc.predict(A_training)))\n",
635
    "\n",
636
    "dtc_acc = accuracy_score(B_testing, dtc.predict(A_testing))\n",
637
    "print(dtc_acc)"
638
   ]
639
  },
640
  {
641
   "cell_type": "code",
642
   "execution_count": null,
643
   "id": "f7f0b47c-f612-4fb1-b06f-9c074da06bb7",
644
   "metadata": {},
645
   "outputs": [
646
    {
647
     "name": "stdout",
648
     "output_type": "stream",
649
     "text": [
650
      "              precision    recall  f1-score   support\n",
651
      "\n",
652
      "           0       0.74      0.80      0.77       100\n",
653
      "           1       0.57      0.48      0.52        54\n",
654
      "\n",
655
      "    accuracy                           0.69       154\n",
656
      "   macro avg       0.65      0.64      0.64       154\n",
657
      "weighted avg       0.68      0.69      0.68       154\n",
658
      "\n"
659
     ]
660
    }
661
   ],
662
   "source": [
663
    "# classification report\n",
664
    "\n",
665
    "print(classification_report(B_testing, B_pred))"
666
   ]
667
  },
668
  {
669
   "cell_type": "code",
670
   "execution_count": null,
671
   "id": "55fde2ec-b707-47d5-8556-6b461a71f5dd",
672
   "metadata": {},
673
   "outputs": [
674
    {
675
     "data": {
676
      "text/html": [
677
       "<div>\n",
678
       "<style scoped>\n",
679
       "    .dataframe tbody tr th:only-of-type {\n",
680
       "        vertical-align: middle;\n",
681
       "    }\n",
682
       "\n",
683
       "    .dataframe tbody tr th {\n",
684
       "        vertical-align: top;\n",
685
       "    }\n",
686
       "\n",
687
       "    .dataframe thead th {\n",
688
       "        text-align: right;\n",
689
       "    }\n",
690
       "</style>\n",
691
       "<table border=\"1\" class=\"dataframe\">\n",
692
       "  <thead>\n",
693
       "    <tr style=\"text-align: right;\">\n",
694
       "      <th></th>\n",
695
       "      <th>Model</th>\n",
696
       "      <th>Score</th>\n",
697
       "    </tr>\n",
698
       "  </thead>\n",
699
       "  <tbody>\n",
700
       "    <tr>\n",
701
       "      <th>0</th>\n",
702
       "      <td>Logistic Regression</td>\n",
703
       "      <td>75.32</td>\n",
704
       "    </tr>\n",
705
       "    <tr>\n",
706
       "      <th>1</th>\n",
707
       "      <td>KNN</td>\n",
708
       "      <td>71.43</td>\n",
709
       "    </tr>\n",
710
       "    <tr>\n",
711
       "      <th>3</th>\n",
712
       "      <td>Decision Tree Classifier</td>\n",
713
       "      <td>68.83</td>\n",
714
       "    </tr>\n",
715
       "    <tr>\n",
716
       "      <th>2</th>\n",
717
       "      <td>SVM</td>\n",
718
       "      <td>64.29</td>\n",
719
       "    </tr>\n",
720
       "  </tbody>\n",
721
       "</table>\n",
722
       "</div>"
723
      ],
724
      "text/plain": [
725
       "                      Model  Score\n",
726
       "0       Logistic Regression  75.32\n",
727
       "1                       KNN  71.43\n",
728
       "3  Decision Tree Classifier  68.83\n",
729
       "2                       SVM  64.29"
730
      ]
731
     },
732
     "execution_count": 15,
733
     "metadata": {},
734
     "output_type": "execute_result"
735
    }
736
   ],
737
   "source": [
738
    "models = pd.DataFrame({\n",
739
    "    'Model': ['Logistic Regression', 'KNN', 'SVM', 'Decision Tree Classifier'],\n",
740
    "    'Score': [100*round(log_reg_acc,4), 100*round(knn_acc,4), 100*round(svc_acc,4), 100*round(dtc_acc,4)]\n",
741
    "})\n",
742
    "models.sort_values(by = 'Score', ascending = False)"
743
   ]
744
  },
745
  {
746
   "cell_type": "code",
747
   "execution_count": null,
748
   "id": "24684911-cbad-474c-8743-fcf517c7e01c",
749
   "metadata": {},
750
   "outputs": [],
751
   "source": []
752
  },
753
  {
754
   "cell_type": "code",
755
   "execution_count": null,
756
   "id": "41401323-1a4c-4091-8bc2-7797137c0f65",
757
   "metadata": {},
758
   "outputs": [],
759
   "source": [
760
    "import pickle\n",
761
    "filename = 'C:/Users/Dell/OneDrive/Desktop/DM PROJECT/diabetes_model.pkl'\n",
762
    "pickle.dump(log_reg, open(filename, 'wb'))"
763
   ]
764
  },
765
  {
766
   "cell_type": "code",
767
   "execution_count": null,
768
   "id": "b1b7f82b-c26e-437a-8a01-c2bfd34e7268",
769
   "metadata": {},
770
   "outputs": [],
771
   "source": [
772
    "'''import pickle\n",
773
    "def load_model(path):\n",
774
    "    with open(path, 'rb') as file:\n",
775
    "        model = pickle.load(file)\n",
776
    "diabetes_model = load_model(r'C:\\Users\\DELL\\Desktop\\app\\diabetes_model.pkl')\n",
777
    "def predict(inputs):\n",
778
    "    return diabetes_model.predict(inputs)'''"
779
   ]
780
  },
781
  {
782
   "cell_type": "code",
783
   "execution_count": null,
784
   "id": "2ef4b8e2-d3d0-4a14-b512-c618d848c8d8",
785
   "metadata": {},
786
   "outputs": [],
787
   "source": []
788
  }
789
 ],
790
 "metadata": {
791
  "kernelspec": {
792
   "display_name": "Python 3",
793
   "language": "python",
794
   "name": "python3"
795
  },
796
  "language_info": {
797
   "codemirror_mode": {
798
    "name": "ipython",
799
    "version": 3
800
   },
801
   "file_extension": ".py",
802
   "mimetype": "text/x-python",
803
   "name": "python",
804
   "nbconvert_exporter": "python",
805
   "pygments_lexer": "ipython3",
806
   "version": "3.12.3"
807
  }
808
 },
809
 "nbformat": 4,
810
 "nbformat_minor": 5
811
}