|
a |
|
b/Model Buliding/final_notebook.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "code", |
|
|
5 |
"execution_count": 138, |
|
|
6 |
"metadata": {}, |
|
|
7 |
"outputs": [], |
|
|
8 |
"source": [ |
|
|
9 |
"import pandas as pd\n", |
|
|
10 |
"import numpy as np\n", |
|
|
11 |
"import matplotlib.pyplot as plt\n", |
|
|
12 |
"from pandas.plotting import scatter_matrix\n", |
|
|
13 |
"import seaborn as sns\n", |
|
|
14 |
"import pickle\n", |
|
|
15 |
"from sklearn.model_selection import train_test_split\n", |
|
|
16 |
"from sklearn.preprocessing import StandardScaler\n", |
|
|
17 |
"from sklearn.preprocessing import LabelEncoder\n", |
|
|
18 |
"from sklearn.metrics import accuracy_score, classification_report\n", |
|
|
19 |
"from sklearn.linear_model import LogisticRegression\n", |
|
|
20 |
"# from sklearn.tree import DecisionTreeClassifier\n", |
|
|
21 |
"# from sklearn.svm import SVC\n", |
|
|
22 |
"# from sklearn.neighbors import KNeighborsClassifier\n", |
|
|
23 |
"# from sklearn.ensemble import RandomForestClassifier\n", |
|
|
24 |
"# from imblearn.over_sampling import SMOTE\n", |
|
|
25 |
"# from sklearn.model_selection import GridSearchCV\n", |
|
|
26 |
"import warnings\n", |
|
|
27 |
"\n", |
|
|
28 |
"# Ignore all warnings\n", |
|
|
29 |
"warnings.filterwarnings(\"ignore\")" |
|
|
30 |
] |
|
|
31 |
}, |
|
|
32 |
{ |
|
|
33 |
"cell_type": "code", |
|
|
34 |
"execution_count": 139, |
|
|
35 |
"metadata": {}, |
|
|
36 |
"outputs": [ |
|
|
37 |
{ |
|
|
38 |
"data": { |
|
|
39 |
"text/html": [ |
|
|
40 |
"<div>\n", |
|
|
41 |
"<style scoped>\n", |
|
|
42 |
" .dataframe tbody tr th:only-of-type {\n", |
|
|
43 |
" vertical-align: middle;\n", |
|
|
44 |
" }\n", |
|
|
45 |
"\n", |
|
|
46 |
" .dataframe tbody tr th {\n", |
|
|
47 |
" vertical-align: top;\n", |
|
|
48 |
" }\n", |
|
|
49 |
"\n", |
|
|
50 |
" .dataframe thead th {\n", |
|
|
51 |
" text-align: right;\n", |
|
|
52 |
" }\n", |
|
|
53 |
"</style>\n", |
|
|
54 |
"<table border=\"1\" class=\"dataframe\">\n", |
|
|
55 |
" <thead>\n", |
|
|
56 |
" <tr style=\"text-align: right;\">\n", |
|
|
57 |
" <th></th>\n", |
|
|
58 |
" <th>General_Health</th>\n", |
|
|
59 |
" <th>Checkup</th>\n", |
|
|
60 |
" <th>Exercise</th>\n", |
|
|
61 |
" <th>Heart_Disease</th>\n", |
|
|
62 |
" <th>Skin_Cancer</th>\n", |
|
|
63 |
" <th>Other_Cancer</th>\n", |
|
|
64 |
" <th>Depression</th>\n", |
|
|
65 |
" <th>Diabetes</th>\n", |
|
|
66 |
" <th>Arthritis</th>\n", |
|
|
67 |
" <th>Sex</th>\n", |
|
|
68 |
" <th>Age_Category</th>\n", |
|
|
69 |
" <th>Height_(cm)</th>\n", |
|
|
70 |
" <th>Weight_(kg)</th>\n", |
|
|
71 |
" <th>BMI</th>\n", |
|
|
72 |
" <th>Smoking_History</th>\n", |
|
|
73 |
" <th>Alcohol_Consumption</th>\n", |
|
|
74 |
" <th>Fruit_Consumption</th>\n", |
|
|
75 |
" <th>Green_Vegetables_Consumption</th>\n", |
|
|
76 |
" <th>FriedPotato_Consumption</th>\n", |
|
|
77 |
" </tr>\n", |
|
|
78 |
" </thead>\n", |
|
|
79 |
" <tbody>\n", |
|
|
80 |
" <tr>\n", |
|
|
81 |
" <th>0</th>\n", |
|
|
82 |
" <td>Poor</td>\n", |
|
|
83 |
" <td>Within the past 2 years</td>\n", |
|
|
84 |
" <td>No</td>\n", |
|
|
85 |
" <td>No</td>\n", |
|
|
86 |
" <td>No</td>\n", |
|
|
87 |
" <td>No</td>\n", |
|
|
88 |
" <td>No</td>\n", |
|
|
89 |
" <td>No</td>\n", |
|
|
90 |
" <td>Yes</td>\n", |
|
|
91 |
" <td>Female</td>\n", |
|
|
92 |
" <td>70-74</td>\n", |
|
|
93 |
" <td>150.0</td>\n", |
|
|
94 |
" <td>32.66</td>\n", |
|
|
95 |
" <td>14.54</td>\n", |
|
|
96 |
" <td>Yes</td>\n", |
|
|
97 |
" <td>0.0</td>\n", |
|
|
98 |
" <td>30.0</td>\n", |
|
|
99 |
" <td>16.0</td>\n", |
|
|
100 |
" <td>12.0</td>\n", |
|
|
101 |
" </tr>\n", |
|
|
102 |
" <tr>\n", |
|
|
103 |
" <th>1</th>\n", |
|
|
104 |
" <td>Very Good</td>\n", |
|
|
105 |
" <td>Within the past year</td>\n", |
|
|
106 |
" <td>No</td>\n", |
|
|
107 |
" <td>Yes</td>\n", |
|
|
108 |
" <td>No</td>\n", |
|
|
109 |
" <td>No</td>\n", |
|
|
110 |
" <td>No</td>\n", |
|
|
111 |
" <td>Yes</td>\n", |
|
|
112 |
" <td>No</td>\n", |
|
|
113 |
" <td>Female</td>\n", |
|
|
114 |
" <td>70-74</td>\n", |
|
|
115 |
" <td>165.0</td>\n", |
|
|
116 |
" <td>77.11</td>\n", |
|
|
117 |
" <td>28.29</td>\n", |
|
|
118 |
" <td>No</td>\n", |
|
|
119 |
" <td>0.0</td>\n", |
|
|
120 |
" <td>30.0</td>\n", |
|
|
121 |
" <td>0.0</td>\n", |
|
|
122 |
" <td>4.0</td>\n", |
|
|
123 |
" </tr>\n", |
|
|
124 |
" <tr>\n", |
|
|
125 |
" <th>2</th>\n", |
|
|
126 |
" <td>Very Good</td>\n", |
|
|
127 |
" <td>Within the past year</td>\n", |
|
|
128 |
" <td>Yes</td>\n", |
|
|
129 |
" <td>No</td>\n", |
|
|
130 |
" <td>No</td>\n", |
|
|
131 |
" <td>No</td>\n", |
|
|
132 |
" <td>No</td>\n", |
|
|
133 |
" <td>Yes</td>\n", |
|
|
134 |
" <td>No</td>\n", |
|
|
135 |
" <td>Female</td>\n", |
|
|
136 |
" <td>60-64</td>\n", |
|
|
137 |
" <td>163.0</td>\n", |
|
|
138 |
" <td>88.45</td>\n", |
|
|
139 |
" <td>33.47</td>\n", |
|
|
140 |
" <td>No</td>\n", |
|
|
141 |
" <td>4.0</td>\n", |
|
|
142 |
" <td>12.0</td>\n", |
|
|
143 |
" <td>3.0</td>\n", |
|
|
144 |
" <td>16.0</td>\n", |
|
|
145 |
" </tr>\n", |
|
|
146 |
" <tr>\n", |
|
|
147 |
" <th>3</th>\n", |
|
|
148 |
" <td>Poor</td>\n", |
|
|
149 |
" <td>Within the past year</td>\n", |
|
|
150 |
" <td>Yes</td>\n", |
|
|
151 |
" <td>Yes</td>\n", |
|
|
152 |
" <td>No</td>\n", |
|
|
153 |
" <td>No</td>\n", |
|
|
154 |
" <td>No</td>\n", |
|
|
155 |
" <td>Yes</td>\n", |
|
|
156 |
" <td>No</td>\n", |
|
|
157 |
" <td>Male</td>\n", |
|
|
158 |
" <td>75-79</td>\n", |
|
|
159 |
" <td>180.0</td>\n", |
|
|
160 |
" <td>93.44</td>\n", |
|
|
161 |
" <td>28.73</td>\n", |
|
|
162 |
" <td>No</td>\n", |
|
|
163 |
" <td>0.0</td>\n", |
|
|
164 |
" <td>30.0</td>\n", |
|
|
165 |
" <td>30.0</td>\n", |
|
|
166 |
" <td>8.0</td>\n", |
|
|
167 |
" </tr>\n", |
|
|
168 |
" <tr>\n", |
|
|
169 |
" <th>4</th>\n", |
|
|
170 |
" <td>Good</td>\n", |
|
|
171 |
" <td>Within the past year</td>\n", |
|
|
172 |
" <td>No</td>\n", |
|
|
173 |
" <td>No</td>\n", |
|
|
174 |
" <td>No</td>\n", |
|
|
175 |
" <td>No</td>\n", |
|
|
176 |
" <td>No</td>\n", |
|
|
177 |
" <td>No</td>\n", |
|
|
178 |
" <td>No</td>\n", |
|
|
179 |
" <td>Male</td>\n", |
|
|
180 |
" <td>80+</td>\n", |
|
|
181 |
" <td>191.0</td>\n", |
|
|
182 |
" <td>88.45</td>\n", |
|
|
183 |
" <td>24.37</td>\n", |
|
|
184 |
" <td>Yes</td>\n", |
|
|
185 |
" <td>0.0</td>\n", |
|
|
186 |
" <td>8.0</td>\n", |
|
|
187 |
" <td>4.0</td>\n", |
|
|
188 |
" <td>0.0</td>\n", |
|
|
189 |
" </tr>\n", |
|
|
190 |
" </tbody>\n", |
|
|
191 |
"</table>\n", |
|
|
192 |
"</div>" |
|
|
193 |
], |
|
|
194 |
"text/plain": [ |
|
|
195 |
" General_Health Checkup Exercise Heart_Disease Skin_Cancer \\\n", |
|
|
196 |
"0 Poor Within the past 2 years No No No \n", |
|
|
197 |
"1 Very Good Within the past year No Yes No \n", |
|
|
198 |
"2 Very Good Within the past year Yes No No \n", |
|
|
199 |
"3 Poor Within the past year Yes Yes No \n", |
|
|
200 |
"4 Good Within the past year No No No \n", |
|
|
201 |
"\n", |
|
|
202 |
" Other_Cancer Depression Diabetes Arthritis Sex Age_Category \\\n", |
|
|
203 |
"0 No No No Yes Female 70-74 \n", |
|
|
204 |
"1 No No Yes No Female 70-74 \n", |
|
|
205 |
"2 No No Yes No Female 60-64 \n", |
|
|
206 |
"3 No No Yes No Male 75-79 \n", |
|
|
207 |
"4 No No No No Male 80+ \n", |
|
|
208 |
"\n", |
|
|
209 |
" Height_(cm) Weight_(kg) BMI Smoking_History Alcohol_Consumption \\\n", |
|
|
210 |
"0 150.0 32.66 14.54 Yes 0.0 \n", |
|
|
211 |
"1 165.0 77.11 28.29 No 0.0 \n", |
|
|
212 |
"2 163.0 88.45 33.47 No 4.0 \n", |
|
|
213 |
"3 180.0 93.44 28.73 No 0.0 \n", |
|
|
214 |
"4 191.0 88.45 24.37 Yes 0.0 \n", |
|
|
215 |
"\n", |
|
|
216 |
" Fruit_Consumption Green_Vegetables_Consumption FriedPotato_Consumption \n", |
|
|
217 |
"0 30.0 16.0 12.0 \n", |
|
|
218 |
"1 30.0 0.0 4.0 \n", |
|
|
219 |
"2 12.0 3.0 16.0 \n", |
|
|
220 |
"3 30.0 30.0 8.0 \n", |
|
|
221 |
"4 8.0 4.0 0.0 " |
|
|
222 |
] |
|
|
223 |
}, |
|
|
224 |
"execution_count": 139, |
|
|
225 |
"metadata": {}, |
|
|
226 |
"output_type": "execute_result" |
|
|
227 |
} |
|
|
228 |
], |
|
|
229 |
"source": [ |
|
|
230 |
"df = pd.read_csv(\"CVD_cleaned.csv\")\n", |
|
|
231 |
"df.head()" |
|
|
232 |
] |
|
|
233 |
}, |
|
|
234 |
{ |
|
|
235 |
"cell_type": "code", |
|
|
236 |
"execution_count": 140, |
|
|
237 |
"metadata": {}, |
|
|
238 |
"outputs": [ |
|
|
239 |
{ |
|
|
240 |
"name": "stdout", |
|
|
241 |
"output_type": "stream", |
|
|
242 |
"text": [ |
|
|
243 |
"<class 'pandas.core.frame.DataFrame'>\n", |
|
|
244 |
"RangeIndex: 308854 entries, 0 to 308853\n", |
|
|
245 |
"Data columns (total 19 columns):\n", |
|
|
246 |
" # Column Non-Null Count Dtype \n", |
|
|
247 |
"--- ------ -------------- ----- \n", |
|
|
248 |
" 0 General_Health 308854 non-null object \n", |
|
|
249 |
" 1 Checkup 308854 non-null object \n", |
|
|
250 |
" 2 Exercise 308854 non-null object \n", |
|
|
251 |
" 3 Heart_Disease 308854 non-null object \n", |
|
|
252 |
" 4 Skin_Cancer 308854 non-null object \n", |
|
|
253 |
" 5 Other_Cancer 308854 non-null object \n", |
|
|
254 |
" 6 Depression 308854 non-null object \n", |
|
|
255 |
" 7 Diabetes 308854 non-null object \n", |
|
|
256 |
" 8 Arthritis 308854 non-null object \n", |
|
|
257 |
" 9 Sex 308854 non-null object \n", |
|
|
258 |
" 10 Age_Category 308854 non-null object \n", |
|
|
259 |
" 11 Height_(cm) 308854 non-null float64\n", |
|
|
260 |
" 12 Weight_(kg) 308854 non-null float64\n", |
|
|
261 |
" 13 BMI 308854 non-null float64\n", |
|
|
262 |
" 14 Smoking_History 308854 non-null object \n", |
|
|
263 |
" 15 Alcohol_Consumption 308854 non-null float64\n", |
|
|
264 |
" 16 Fruit_Consumption 308854 non-null float64\n", |
|
|
265 |
" 17 Green_Vegetables_Consumption 308854 non-null float64\n", |
|
|
266 |
" 18 FriedPotato_Consumption 308854 non-null float64\n", |
|
|
267 |
"dtypes: float64(7), object(12)\n", |
|
|
268 |
"memory usage: 44.8+ MB\n" |
|
|
269 |
] |
|
|
270 |
} |
|
|
271 |
], |
|
|
272 |
"source": [ |
|
|
273 |
"df.info()" |
|
|
274 |
] |
|
|
275 |
}, |
|
|
276 |
{ |
|
|
277 |
"cell_type": "code", |
|
|
278 |
"execution_count": 141, |
|
|
279 |
"metadata": {}, |
|
|
280 |
"outputs": [ |
|
|
281 |
{ |
|
|
282 |
"data": { |
|
|
283 |
"text/plain": [ |
|
|
284 |
"Index(['General_Health', 'Checkup', 'Exercise', 'Heart_Disease', 'Skin_Cancer',\n", |
|
|
285 |
" 'Other_Cancer', 'Depression', 'Diabetes', 'Arthritis', 'Sex',\n", |
|
|
286 |
" 'Age_Category', 'Height_(cm)', 'Weight_(kg)', 'BMI', 'Smoking_History',\n", |
|
|
287 |
" 'Alcohol_Consumption', 'Fruit_Consumption',\n", |
|
|
288 |
" 'Green_Vegetables_Consumption', 'FriedPotato_Consumption'],\n", |
|
|
289 |
" dtype='object')" |
|
|
290 |
] |
|
|
291 |
}, |
|
|
292 |
"execution_count": 141, |
|
|
293 |
"metadata": {}, |
|
|
294 |
"output_type": "execute_result" |
|
|
295 |
} |
|
|
296 |
], |
|
|
297 |
"source": [ |
|
|
298 |
"df.columns" |
|
|
299 |
] |
|
|
300 |
}, |
|
|
301 |
{ |
|
|
302 |
"cell_type": "code", |
|
|
303 |
"execution_count": 142, |
|
|
304 |
"metadata": {}, |
|
|
305 |
"outputs": [], |
|
|
306 |
"source": [ |
|
|
307 |
"df.drop(columns=['Skin_Cancer','Other_Cancer'],inplace = True)" |
|
|
308 |
] |
|
|
309 |
}, |
|
|
310 |
{ |
|
|
311 |
"cell_type": "code", |
|
|
312 |
"execution_count": 143, |
|
|
313 |
"metadata": {}, |
|
|
314 |
"outputs": [ |
|
|
315 |
{ |
|
|
316 |
"data": { |
|
|
317 |
"text/plain": [ |
|
|
318 |
"Diabetes\n", |
|
|
319 |
"No 266037\n", |
|
|
320 |
"Yes 42817\n", |
|
|
321 |
"Name: count, dtype: int64" |
|
|
322 |
] |
|
|
323 |
}, |
|
|
324 |
"execution_count": 143, |
|
|
325 |
"metadata": {}, |
|
|
326 |
"output_type": "execute_result" |
|
|
327 |
} |
|
|
328 |
], |
|
|
329 |
"source": [ |
|
|
330 |
"df['Diabetes'] = df['Diabetes'].replace({\n", |
|
|
331 |
" 'Yes': 'Yes',\n", |
|
|
332 |
" 'Yes, but female told only during pregnancy': 'Yes',\n", |
|
|
333 |
" 'No': 'No',\n", |
|
|
334 |
" 'No, pre-diabetes or borderline diabetes': 'No'\n", |
|
|
335 |
"})\n", |
|
|
336 |
"df['Diabetes'].value_counts()" |
|
|
337 |
] |
|
|
338 |
}, |
|
|
339 |
{ |
|
|
340 |
"cell_type": "code", |
|
|
341 |
"execution_count": 144, |
|
|
342 |
"metadata": {}, |
|
|
343 |
"outputs": [], |
|
|
344 |
"source": [ |
|
|
345 |
"categorical_columns = df.select_dtypes(include=['object', 'category']).columns\n", |
|
|
346 |
"numerical_columns = df.select_dtypes(include=['number'])" |
|
|
347 |
] |
|
|
348 |
}, |
|
|
349 |
{ |
|
|
350 |
"cell_type": "code", |
|
|
351 |
"execution_count": 145, |
|
|
352 |
"metadata": {}, |
|
|
353 |
"outputs": [ |
|
|
354 |
{ |
|
|
355 |
"data": { |
|
|
356 |
"text/plain": [ |
|
|
357 |
"obesity\n", |
|
|
358 |
"Overweight 109866\n", |
|
|
359 |
"Obesity 106738\n", |
|
|
360 |
"Normal weight 87706\n", |
|
|
361 |
"Underweight 4544\n", |
|
|
362 |
"Name: count, dtype: int64" |
|
|
363 |
] |
|
|
364 |
}, |
|
|
365 |
"execution_count": 145, |
|
|
366 |
"metadata": {}, |
|
|
367 |
"output_type": "execute_result" |
|
|
368 |
} |
|
|
369 |
], |
|
|
370 |
"source": [ |
|
|
371 |
"bins = [-float('inf'), 18.5, 24.9, 29.9, float('inf')]\n", |
|
|
372 |
"labels = ['Underweight', 'Normal weight', 'Overweight', 'Obesity']\n", |
|
|
373 |
"\n", |
|
|
374 |
"# Create a new column 'obesity' based on BMI classification\n", |
|
|
375 |
"df['obesity'] = pd.cut(df['BMI'], bins=bins, labels=labels)\n", |
|
|
376 |
"df['obesity'].value_counts()" |
|
|
377 |
] |
|
|
378 |
}, |
|
|
379 |
{ |
|
|
380 |
"cell_type": "code", |
|
|
381 |
"execution_count": 146, |
|
|
382 |
"metadata": {}, |
|
|
383 |
"outputs": [], |
|
|
384 |
"source": [ |
|
|
385 |
"label_encoders = {}\n", |
|
|
386 |
"\n", |
|
|
387 |
"# Apply Label Encoding to categorical columns\n", |
|
|
388 |
"for column in categorical_columns:\n", |
|
|
389 |
" df[column] = df[column].astype(str)\n", |
|
|
390 |
" label_encoder = LabelEncoder() \n", |
|
|
391 |
" df[column] = label_encoder.fit_transform(df[column])\n", |
|
|
392 |
" label_encoders[column] = label_encoder \n" |
|
|
393 |
] |
|
|
394 |
}, |
|
|
395 |
{ |
|
|
396 |
"cell_type": "code", |
|
|
397 |
"execution_count": 147, |
|
|
398 |
"metadata": {}, |
|
|
399 |
"outputs": [], |
|
|
400 |
"source": [ |
|
|
401 |
"with open('label_encoders.pkl', 'wb') as f:\n", |
|
|
402 |
" pickle.dump(label_encoders, f)" |
|
|
403 |
] |
|
|
404 |
}, |
|
|
405 |
{ |
|
|
406 |
"cell_type": "code", |
|
|
407 |
"execution_count": 148, |
|
|
408 |
"metadata": {}, |
|
|
409 |
"outputs": [ |
|
|
410 |
{ |
|
|
411 |
"name": "stdout", |
|
|
412 |
"output_type": "stream", |
|
|
413 |
"text": [ |
|
|
414 |
"Checkup Label Encoder Mappings:\n", |
|
|
415 |
"0: No\n", |
|
|
416 |
"1: Yes\n" |
|
|
417 |
] |
|
|
418 |
} |
|
|
419 |
], |
|
|
420 |
"source": [ |
|
|
421 |
"if 'Heart_Disease' in label_encoders:\n", |
|
|
422 |
" encoder = label_encoders['Heart_Disease']\n", |
|
|
423 |
" mappings = dict(zip(encoder.transform(encoder.classes_), encoder.classes_))\n", |
|
|
424 |
" print(\"Checkup Label Encoder Mappings:\")\n", |
|
|
425 |
" for key, value in mappings.items():\n", |
|
|
426 |
" print(f\"{key}: {value}\")\n", |
|
|
427 |
"else:\n", |
|
|
428 |
" print(\"Checkup column not found in label encoders.\")" |
|
|
429 |
] |
|
|
430 |
}, |
|
|
431 |
{ |
|
|
432 |
"cell_type": "code", |
|
|
433 |
"execution_count": 149, |
|
|
434 |
"metadata": {}, |
|
|
435 |
"outputs": [ |
|
|
436 |
{ |
|
|
437 |
"data": { |
|
|
438 |
"text/plain": [ |
|
|
439 |
"18" |
|
|
440 |
] |
|
|
441 |
}, |
|
|
442 |
"execution_count": 149, |
|
|
443 |
"metadata": {}, |
|
|
444 |
"output_type": "execute_result" |
|
|
445 |
} |
|
|
446 |
], |
|
|
447 |
"source": [ |
|
|
448 |
"len(df.columns.tolist())" |
|
|
449 |
] |
|
|
450 |
}, |
|
|
451 |
{ |
|
|
452 |
"cell_type": "code", |
|
|
453 |
"execution_count": 150, |
|
|
454 |
"metadata": {}, |
|
|
455 |
"outputs": [ |
|
|
456 |
{ |
|
|
457 |
"data": { |
|
|
458 |
"text/plain": [ |
|
|
459 |
"Diabetes\n", |
|
|
460 |
"0 266037\n", |
|
|
461 |
"1 42817\n", |
|
|
462 |
"Name: count, dtype: int64" |
|
|
463 |
] |
|
|
464 |
}, |
|
|
465 |
"execution_count": 150, |
|
|
466 |
"metadata": {}, |
|
|
467 |
"output_type": "execute_result" |
|
|
468 |
} |
|
|
469 |
], |
|
|
470 |
"source": [ |
|
|
471 |
"df['Diabetes'].value_counts()" |
|
|
472 |
] |
|
|
473 |
}, |
|
|
474 |
{ |
|
|
475 |
"cell_type": "code", |
|
|
476 |
"execution_count": 151, |
|
|
477 |
"metadata": {}, |
|
|
478 |
"outputs": [], |
|
|
479 |
"source": [ |
|
|
480 |
"y_heart_disease = df['Heart_Disease']\n", |
|
|
481 |
"y_diabetes = df['Diabetes']\n", |
|
|
482 |
"y_obesity = df['obesity']\n", |
|
|
483 |
"X_train = df.drop(columns=['Heart_Disease','Diabetes','obesity'])\n" |
|
|
484 |
] |
|
|
485 |
}, |
|
|
486 |
{ |
|
|
487 |
"cell_type": "code", |
|
|
488 |
"execution_count": 152, |
|
|
489 |
"metadata": {}, |
|
|
490 |
"outputs": [], |
|
|
491 |
"source": [ |
|
|
492 |
"# Feature scaling (Standardizing the data)\n", |
|
|
493 |
"# scaler = StandardScaler()\n", |
|
|
494 |
"# X_scaled = scaler.fit_transform(X)\n", |
|
|
495 |
"\n", |
|
|
496 |
"# X_resampled, y_resampled = smote.fit_resample(X_scaled, y)\n", |
|
|
497 |
"X_train_ht, X_test_ht, y_train_ht, y_test_ht = train_test_split(X_train,y_heart_disease,test_size= 0.2)\n", |
|
|
498 |
"X_train_dt, X_test_dt, y_train_dt, y_test_dt = train_test_split(X_train,y_diabetes,test_size= 0.2)\n", |
|
|
499 |
"X_train_ob, X_test_ob, y_train_ob, y_test_ob = train_test_split(X_train,y_obesity,test_size= 0.2)" |
|
|
500 |
] |
|
|
501 |
}, |
|
|
502 |
{ |
|
|
503 |
"cell_type": "code", |
|
|
504 |
"execution_count": 154, |
|
|
505 |
"metadata": {}, |
|
|
506 |
"outputs": [ |
|
|
507 |
{ |
|
|
508 |
"data": { |
|
|
509 |
"text/plain": [ |
|
|
510 |
"Index(['General_Health', 'Checkup', 'Exercise', 'Depression', 'Arthritis',\n", |
|
|
511 |
" 'Sex', 'Age_Category', 'Height_(cm)', 'Weight_(kg)', 'BMI',\n", |
|
|
512 |
" 'Smoking_History', 'Alcohol_Consumption', 'Fruit_Consumption',\n", |
|
|
513 |
" 'Green_Vegetables_Consumption', 'FriedPotato_Consumption'],\n", |
|
|
514 |
" dtype='object')" |
|
|
515 |
] |
|
|
516 |
}, |
|
|
517 |
"execution_count": 154, |
|
|
518 |
"metadata": {}, |
|
|
519 |
"output_type": "execute_result" |
|
|
520 |
} |
|
|
521 |
], |
|
|
522 |
"source": [ |
|
|
523 |
"X_train_dt.columns" |
|
|
524 |
] |
|
|
525 |
}, |
|
|
526 |
{ |
|
|
527 |
"cell_type": "code", |
|
|
528 |
"execution_count": 155, |
|
|
529 |
"metadata": {}, |
|
|
530 |
"outputs": [ |
|
|
531 |
{ |
|
|
532 |
"name": "stdout", |
|
|
533 |
"output_type": "stream", |
|
|
534 |
"text": [ |
|
|
535 |
"Accuracy: 0.9201\n", |
|
|
536 |
" precision recall f1-score support\n", |
|
|
537 |
"\n", |
|
|
538 |
" 0 0.92 1.00 0.96 56850\n", |
|
|
539 |
" 1 0.33 0.00 0.01 4921\n", |
|
|
540 |
"\n", |
|
|
541 |
" accuracy 0.92 61771\n", |
|
|
542 |
" macro avg 0.63 0.50 0.48 61771\n", |
|
|
543 |
"weighted avg 0.87 0.92 0.88 61771\n", |
|
|
544 |
"\n" |
|
|
545 |
] |
|
|
546 |
} |
|
|
547 |
], |
|
|
548 |
"source": [ |
|
|
549 |
"### Logistic Regression\n", |
|
|
550 |
"lr_ht = LogisticRegression()\n", |
|
|
551 |
"lr_ht.fit(X_train_ht,y_train_ht)\n", |
|
|
552 |
"y_pred_ht = lr_ht.predict(X_test_ht)\n", |
|
|
553 |
"print(f\"Accuracy: {accuracy_score(y_test_ht, y_pred_ht):.4f}\")\n", |
|
|
554 |
"print(classification_report(y_test_ht, y_pred_ht))" |
|
|
555 |
] |
|
|
556 |
}, |
|
|
557 |
{ |
|
|
558 |
"cell_type": "code", |
|
|
559 |
"execution_count": 156, |
|
|
560 |
"metadata": {}, |
|
|
561 |
"outputs": [ |
|
|
562 |
{ |
|
|
563 |
"name": "stdout", |
|
|
564 |
"output_type": "stream", |
|
|
565 |
"text": [ |
|
|
566 |
"Accuracy: 0.8609\n", |
|
|
567 |
" precision recall f1-score support\n", |
|
|
568 |
"\n", |
|
|
569 |
" 0 0.86 0.99 0.92 53194\n", |
|
|
570 |
" 1 0.49 0.04 0.07 8577\n", |
|
|
571 |
"\n", |
|
|
572 |
" accuracy 0.86 61771\n", |
|
|
573 |
" macro avg 0.68 0.52 0.50 61771\n", |
|
|
574 |
"weighted avg 0.81 0.86 0.81 61771\n", |
|
|
575 |
"\n" |
|
|
576 |
] |
|
|
577 |
} |
|
|
578 |
], |
|
|
579 |
"source": [ |
|
|
580 |
"### Logistic Regression\n", |
|
|
581 |
"lr_dt = LogisticRegression()\n", |
|
|
582 |
"lr_dt.fit(X_train_dt,y_train_dt)\n", |
|
|
583 |
"y_pred_dt = lr_dt.predict(X_test_dt)\n", |
|
|
584 |
"print(f\"Accuracy: {accuracy_score(y_test_dt, y_pred_dt):.4f}\")\n", |
|
|
585 |
"print(classification_report(y_test_dt, y_pred_dt))" |
|
|
586 |
] |
|
|
587 |
}, |
|
|
588 |
{ |
|
|
589 |
"cell_type": "code", |
|
|
590 |
"execution_count": 157, |
|
|
591 |
"metadata": {}, |
|
|
592 |
"outputs": [ |
|
|
593 |
{ |
|
|
594 |
"name": "stdout", |
|
|
595 |
"output_type": "stream", |
|
|
596 |
"text": [ |
|
|
597 |
"Accuracy: 0.9732\n", |
|
|
598 |
" precision recall f1-score support\n", |
|
|
599 |
"\n", |
|
|
600 |
"Normal weight 0.97 0.97 0.97 17380\n", |
|
|
601 |
" Obesity 0.99 0.99 0.99 21321\n", |
|
|
602 |
" Overweight 0.98 0.98 0.98 22138\n", |
|
|
603 |
" Underweight 0.63 0.65 0.64 932\n", |
|
|
604 |
"\n", |
|
|
605 |
" accuracy 0.97 61771\n", |
|
|
606 |
" macro avg 0.89 0.89 0.89 61771\n", |
|
|
607 |
" weighted avg 0.97 0.97 0.97 61771\n", |
|
|
608 |
"\n" |
|
|
609 |
] |
|
|
610 |
} |
|
|
611 |
], |
|
|
612 |
"source": [ |
|
|
613 |
"### Logistic Regression\n", |
|
|
614 |
"lr_ob = LogisticRegression()\n", |
|
|
615 |
"lr_ob.fit(X_train_ob,y_train_ob)\n", |
|
|
616 |
"y_pred_ob = lr_ob.predict(X_test_ob)\n", |
|
|
617 |
"print(f\"Accuracy: {accuracy_score(y_test_ob, y_pred_ob):.4f}\")\n", |
|
|
618 |
"print(classification_report(y_test_ob, y_pred_ob))" |
|
|
619 |
] |
|
|
620 |
}, |
|
|
621 |
{ |
|
|
622 |
"cell_type": "code", |
|
|
623 |
"execution_count": 158, |
|
|
624 |
"metadata": {}, |
|
|
625 |
"outputs": [ |
|
|
626 |
{ |
|
|
627 |
"name": "stdout", |
|
|
628 |
"output_type": "stream", |
|
|
629 |
"text": [ |
|
|
630 |
"Accuracy: 0.9732\n", |
|
|
631 |
" precision recall f1-score support\n", |
|
|
632 |
"\n", |
|
|
633 |
"Normal weight 0.97 0.97 0.97 17380\n", |
|
|
634 |
" Obesity 0.99 0.99 0.99 21321\n", |
|
|
635 |
" Overweight 0.98 0.98 0.98 22138\n", |
|
|
636 |
" Underweight 0.63 0.65 0.64 932\n", |
|
|
637 |
"\n", |
|
|
638 |
" accuracy 0.97 61771\n", |
|
|
639 |
" macro avg 0.89 0.89 0.89 61771\n", |
|
|
640 |
" weighted avg 0.97 0.97 0.97 61771\n", |
|
|
641 |
"\n" |
|
|
642 |
] |
|
|
643 |
} |
|
|
644 |
], |
|
|
645 |
"source": [ |
|
|
646 |
"### Logistic Regression\n", |
|
|
647 |
"lr_ob = LogisticRegression()\n", |
|
|
648 |
"lr_ob.fit(X_train_ob,y_train_ob)\n", |
|
|
649 |
"y_pred_ob = lr_ob.predict(X_test_ob)\n", |
|
|
650 |
"print(f\"Accuracy: {accuracy_score(y_test_ob, y_pred_ob):.4f}\")\n", |
|
|
651 |
"print(classification_report(y_test_ob, y_pred_ob))" |
|
|
652 |
] |
|
|
653 |
}, |
|
|
654 |
{ |
|
|
655 |
"cell_type": "code", |
|
|
656 |
"execution_count": null, |
|
|
657 |
"metadata": {}, |
|
|
658 |
"outputs": [], |
|
|
659 |
"source": [] |
|
|
660 |
}, |
|
|
661 |
{ |
|
|
662 |
"cell_type": "code", |
|
|
663 |
"execution_count": 159, |
|
|
664 |
"metadata": {}, |
|
|
665 |
"outputs": [ |
|
|
666 |
{ |
|
|
667 |
"name": "stdout", |
|
|
668 |
"output_type": "stream", |
|
|
669 |
"text": [ |
|
|
670 |
"Model saved to lr_ht.pkl\n" |
|
|
671 |
] |
|
|
672 |
} |
|
|
673 |
], |
|
|
674 |
"source": [ |
|
|
675 |
"model_filename = 'lr_ht.pkl'\n", |
|
|
676 |
"with open(model_filename, 'wb') as file:\n", |
|
|
677 |
" pickle.dump(lr_ht, file)\n", |
|
|
678 |
"\n", |
|
|
679 |
"print(f\"Model saved to {model_filename}\")" |
|
|
680 |
] |
|
|
681 |
}, |
|
|
682 |
{ |
|
|
683 |
"cell_type": "code", |
|
|
684 |
"execution_count": 160, |
|
|
685 |
"metadata": {}, |
|
|
686 |
"outputs": [ |
|
|
687 |
{ |
|
|
688 |
"name": "stdout", |
|
|
689 |
"output_type": "stream", |
|
|
690 |
"text": [ |
|
|
691 |
"Model saved to lr_dt.pkl\n" |
|
|
692 |
] |
|
|
693 |
} |
|
|
694 |
], |
|
|
695 |
"source": [ |
|
|
696 |
"model_filename = 'lr_dt.pkl'\n", |
|
|
697 |
"with open(model_filename, 'wb') as file:\n", |
|
|
698 |
" pickle.dump(lr_dt, file)\n", |
|
|
699 |
"\n", |
|
|
700 |
"print(f\"Model saved to {model_filename}\")" |
|
|
701 |
] |
|
|
702 |
}, |
|
|
703 |
{ |
|
|
704 |
"cell_type": "code", |
|
|
705 |
"execution_count": 161, |
|
|
706 |
"metadata": {}, |
|
|
707 |
"outputs": [ |
|
|
708 |
{ |
|
|
709 |
"name": "stdout", |
|
|
710 |
"output_type": "stream", |
|
|
711 |
"text": [ |
|
|
712 |
"Model saved to lr_ob.pkl\n" |
|
|
713 |
] |
|
|
714 |
} |
|
|
715 |
], |
|
|
716 |
"source": [ |
|
|
717 |
"model_filename = 'lr_ob.pkl'\n", |
|
|
718 |
"with open(model_filename, 'wb') as file:\n", |
|
|
719 |
" pickle.dump(lr_ob, file)\n", |
|
|
720 |
"\n", |
|
|
721 |
"print(f\"Model saved to {model_filename}\")" |
|
|
722 |
] |
|
|
723 |
}, |
|
|
724 |
{ |
|
|
725 |
"cell_type": "code", |
|
|
726 |
"execution_count": 112, |
|
|
727 |
"metadata": {}, |
|
|
728 |
"outputs": [ |
|
|
729 |
{ |
|
|
730 |
"name": "stdout", |
|
|
731 |
"output_type": "stream", |
|
|
732 |
"text": [ |
|
|
733 |
"Loaded Model Accuracy: 0.9727\n" |
|
|
734 |
] |
|
|
735 |
} |
|
|
736 |
], |
|
|
737 |
"source": [ |
|
|
738 |
"with open(model_filename, 'rb') as file:\n", |
|
|
739 |
" loaded_model = pickle.load(file)\n", |
|
|
740 |
"\n", |
|
|
741 |
"# Now you can use loaded_model to make predictions\n", |
|
|
742 |
"y_pred_loaded = loaded_model.predict(X_test_ob)\n", |
|
|
743 |
"print(f\"Loaded Model Accuracy: {accuracy_score(y_test_ob, y_pred_loaded):.4f}\")" |
|
|
744 |
] |
|
|
745 |
}, |
|
|
746 |
{ |
|
|
747 |
"cell_type": "code", |
|
|
748 |
"execution_count": 113, |
|
|
749 |
"metadata": {}, |
|
|
750 |
"outputs": [ |
|
|
751 |
{ |
|
|
752 |
"data": { |
|
|
753 |
"text/plain": [ |
|
|
754 |
"Index(['General_Health', 'Checkup', 'Exercise', 'Skin_Cancer', 'Other_Cancer',\n", |
|
|
755 |
" 'Depression', 'Arthritis', 'Sex', 'Age_Category', 'Height_(cm)',\n", |
|
|
756 |
" 'Weight_(kg)', 'BMI', 'Smoking_History', 'Alcohol_Consumption',\n", |
|
|
757 |
" 'Fruit_Consumption', 'Green_Vegetables_Consumption',\n", |
|
|
758 |
" 'FriedPotato_Consumption'],\n", |
|
|
759 |
" dtype='object')" |
|
|
760 |
] |
|
|
761 |
}, |
|
|
762 |
"execution_count": 113, |
|
|
763 |
"metadata": {}, |
|
|
764 |
"output_type": "execute_result" |
|
|
765 |
} |
|
|
766 |
], |
|
|
767 |
"source": [ |
|
|
768 |
"X_test_ob.columns" |
|
|
769 |
] |
|
|
770 |
}, |
|
|
771 |
{ |
|
|
772 |
"cell_type": "code", |
|
|
773 |
"execution_count": null, |
|
|
774 |
"metadata": {}, |
|
|
775 |
"outputs": [], |
|
|
776 |
"source": [] |
|
|
777 |
}, |
|
|
778 |
{ |
|
|
779 |
"cell_type": "code", |
|
|
780 |
"execution_count": 114, |
|
|
781 |
"metadata": {}, |
|
|
782 |
"outputs": [ |
|
|
783 |
{ |
|
|
784 |
"data": { |
|
|
785 |
"text/plain": [ |
|
|
786 |
"['General_Health',\n", |
|
|
787 |
" 'Checkup',\n", |
|
|
788 |
" 'Exercise',\n", |
|
|
789 |
" 'Heart_Disease',\n", |
|
|
790 |
" 'Skin_Cancer',\n", |
|
|
791 |
" 'Other_Cancer',\n", |
|
|
792 |
" 'Depression',\n", |
|
|
793 |
" 'Diabetes',\n", |
|
|
794 |
" 'Arthritis',\n", |
|
|
795 |
" 'Sex',\n", |
|
|
796 |
" 'Age_Category',\n", |
|
|
797 |
" 'Smoking_History']" |
|
|
798 |
] |
|
|
799 |
}, |
|
|
800 |
"execution_count": 114, |
|
|
801 |
"metadata": {}, |
|
|
802 |
"output_type": "execute_result" |
|
|
803 |
} |
|
|
804 |
], |
|
|
805 |
"source": [ |
|
|
806 |
"categorical_columns.tolist()" |
|
|
807 |
] |
|
|
808 |
}, |
|
|
809 |
{ |
|
|
810 |
"cell_type": "code", |
|
|
811 |
"execution_count": 115, |
|
|
812 |
"metadata": {}, |
|
|
813 |
"outputs": [ |
|
|
814 |
{ |
|
|
815 |
"name": "stdout", |
|
|
816 |
"output_type": "stream", |
|
|
817 |
"text": [ |
|
|
818 |
"Encoded Input DataFrame:\n" |
|
|
819 |
] |
|
|
820 |
}, |
|
|
821 |
{ |
|
|
822 |
"data": { |
|
|
823 |
"text/html": [ |
|
|
824 |
"<div>\n", |
|
|
825 |
"<style scoped>\n", |
|
|
826 |
" .dataframe tbody tr th:only-of-type {\n", |
|
|
827 |
" vertical-align: middle;\n", |
|
|
828 |
" }\n", |
|
|
829 |
"\n", |
|
|
830 |
" .dataframe tbody tr th {\n", |
|
|
831 |
" vertical-align: top;\n", |
|
|
832 |
" }\n", |
|
|
833 |
"\n", |
|
|
834 |
" .dataframe thead th {\n", |
|
|
835 |
" text-align: right;\n", |
|
|
836 |
" }\n", |
|
|
837 |
"</style>\n", |
|
|
838 |
"<table border=\"1\" class=\"dataframe\">\n", |
|
|
839 |
" <thead>\n", |
|
|
840 |
" <tr style=\"text-align: right;\">\n", |
|
|
841 |
" <th></th>\n", |
|
|
842 |
" <th>General_Health</th>\n", |
|
|
843 |
" <th>Checkup</th>\n", |
|
|
844 |
" <th>Exercise</th>\n", |
|
|
845 |
" <th>Depression</th>\n", |
|
|
846 |
" <th>Arthritis</th>\n", |
|
|
847 |
" <th>Sex</th>\n", |
|
|
848 |
" <th>Age_Category</th>\n", |
|
|
849 |
" <th>Height_(cm)</th>\n", |
|
|
850 |
" <th>Weight_(kg)</th>\n", |
|
|
851 |
" <th>BMI</th>\n", |
|
|
852 |
" <th>Smoking_History</th>\n", |
|
|
853 |
" <th>Alcohol_Consumption</th>\n", |
|
|
854 |
" <th>Fruit_Consumption</th>\n", |
|
|
855 |
" <th>Green_Vegetables_Consumption</th>\n", |
|
|
856 |
" <th>FriedPotato_Consumption</th>\n", |
|
|
857 |
" </tr>\n", |
|
|
858 |
" </thead>\n", |
|
|
859 |
" <tbody>\n", |
|
|
860 |
" <tr>\n", |
|
|
861 |
" <th>0</th>\n", |
|
|
862 |
" <td>3</td>\n", |
|
|
863 |
" <td>2</td>\n", |
|
|
864 |
" <td>0</td>\n", |
|
|
865 |
" <td>0</td>\n", |
|
|
866 |
" <td>1</td>\n", |
|
|
867 |
" <td>0</td>\n", |
|
|
868 |
" <td>10</td>\n", |
|
|
869 |
" <td>150</td>\n", |
|
|
870 |
" <td>32.66</td>\n", |
|
|
871 |
" <td>14.34</td>\n", |
|
|
872 |
" <td>1</td>\n", |
|
|
873 |
" <td>0.0</td>\n", |
|
|
874 |
" <td>30.0</td>\n", |
|
|
875 |
" <td>16</td>\n", |
|
|
876 |
" <td>12</td>\n", |
|
|
877 |
" </tr>\n", |
|
|
878 |
" </tbody>\n", |
|
|
879 |
"</table>\n", |
|
|
880 |
"</div>" |
|
|
881 |
], |
|
|
882 |
"text/plain": [ |
|
|
883 |
" General_Health Checkup Exercise Depression Arthritis Sex \\\n", |
|
|
884 |
"0 3 2 0 0 1 0 \n", |
|
|
885 |
"\n", |
|
|
886 |
" Age_Category Height_(cm) Weight_(kg) BMI Smoking_History \\\n", |
|
|
887 |
"0 10 150 32.66 14.34 1 \n", |
|
|
888 |
"\n", |
|
|
889 |
" Alcohol_Consumption Fruit_Consumption Green_Vegetables_Consumption \\\n", |
|
|
890 |
"0 0.0 30.0 16 \n", |
|
|
891 |
"\n", |
|
|
892 |
" FriedPotato_Consumption \n", |
|
|
893 |
"0 12 " |
|
|
894 |
] |
|
|
895 |
}, |
|
|
896 |
"execution_count": 115, |
|
|
897 |
"metadata": {}, |
|
|
898 |
"output_type": "execute_result" |
|
|
899 |
} |
|
|
900 |
], |
|
|
901 |
"source": [ |
|
|
902 |
"# Define the columns\n", |
|
|
903 |
"columns = ['General_Health', 'Checkup', 'Exercise', 'Depression', 'Arthritis',\n", |
|
|
904 |
" 'Sex', 'Age_Category', 'Height_(cm)', 'Weight_(kg)', 'BMI',\n", |
|
|
905 |
" 'Smoking_History', 'Alcohol_Consumption', 'Fruit_Consumption',\n", |
|
|
906 |
" 'Green_Vegetables_Consumption', 'FriedPotato_Consumption']\n", |
|
|
907 |
"\n", |
|
|
908 |
"# Define the input values for each column (replace these with actual values)\n", |
|
|
909 |
"input_values = {\n", |
|
|
910 |
" 'General_Health': 'Poor', # Example values\n", |
|
|
911 |
" 'Checkup': 'Within the past 2 years',\n", |
|
|
912 |
" 'Exercise': 'No',\n", |
|
|
913 |
" 'Depression': 'No',\n", |
|
|
914 |
" 'Arthritis': 'Yes',\n", |
|
|
915 |
" 'Sex': 'Female',\n", |
|
|
916 |
" 'Age_Category': '70-74',\n", |
|
|
917 |
" 'Height_(cm)': 150, # Example numerical values\n", |
|
|
918 |
" 'Weight_(kg)': 32.66,\n", |
|
|
919 |
" 'BMI': 14.34,\n", |
|
|
920 |
" 'Smoking_History': 'Yes',\n", |
|
|
921 |
" 'Alcohol_Consumption': 0.0,\n", |
|
|
922 |
" 'Fruit_Consumption': 30.0,\n", |
|
|
923 |
" 'Green_Vegetables_Consumption': 16,\n", |
|
|
924 |
" 'FriedPotato_Consumption': 12\n", |
|
|
925 |
"}\n", |
|
|
926 |
"\n", |
|
|
927 |
"# Create a DataFrame from input values\n", |
|
|
928 |
"input_df = pd.DataFrame([input_values])\n", |
|
|
929 |
"\n", |
|
|
930 |
"# Encode categorical columns using the same LabelEncoders you used during training\n", |
|
|
931 |
"for column in categorical_columns:\n", |
|
|
932 |
" if column in input_df.columns:\n", |
|
|
933 |
" # Transform the input values using the stored encoder\n", |
|
|
934 |
" input_df[column] = label_encoders[column].transform(input_df[column].astype(str))\n", |
|
|
935 |
"\n", |
|
|
936 |
"# Display the input DataFrame after encoding\n", |
|
|
937 |
"print(\"Encoded Input DataFrame:\")\n", |
|
|
938 |
"input_df\n" |
|
|
939 |
] |
|
|
940 |
}, |
|
|
941 |
{ |
|
|
942 |
"cell_type": "code", |
|
|
943 |
"execution_count": 116, |
|
|
944 |
"metadata": {}, |
|
|
945 |
"outputs": [ |
|
|
946 |
{ |
|
|
947 |
"ename": "ValueError", |
|
|
948 |
"evalue": "The feature names should match those that were passed during fit.\nFeature names seen at fit time, yet now missing:\n- Other_Cancer\n- Skin_Cancer\n", |
|
|
949 |
"output_type": "error", |
|
|
950 |
"traceback": [ |
|
|
951 |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
|
|
952 |
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", |
|
|
953 |
"Cell \u001b[1;32mIn[116], line 5\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Prepare input for prediction (make sure to drop any non-feature columns if necessary)\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# X_input = input_df[columns]\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# Make the prediction\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m prediction \u001b[38;5;241m=\u001b[39m loaded_model\u001b[38;5;241m.\u001b[39mpredict(input_df)\n\u001b[0;32m 7\u001b[0m \u001b[38;5;66;03m# Output the prediction\u001b[39;00m\n\u001b[0;32m 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPredicted Class: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mprediction[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", |
|
|
954 |
"File \u001b[1;32mc:\\ProgramData\\anaconda3\\Lib\\site-packages\\sklearn\\linear_model\\_base.py:351\u001b[0m, in \u001b[0;36mLinearClassifierMixin.predict\u001b[1;34m(self, X)\u001b[0m\n\u001b[0;32m 337\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m 338\u001b[0m \u001b[39mPredict class labels for samples in X.\u001b[39;00m\n\u001b[0;32m 339\u001b[0m \n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 348\u001b[0m \u001b[39m Vector containing the class labels for each sample.\u001b[39;00m\n\u001b[0;32m 349\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m 350\u001b[0m xp, _ \u001b[39m=\u001b[39m get_namespace(X)\n\u001b[1;32m--> 351\u001b[0m scores \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdecision_function(X)\n\u001b[0;32m 352\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mlen\u001b[39m(scores\u001b[39m.\u001b[39mshape) \u001b[39m==\u001b[39m \u001b[39m1\u001b[39m:\n\u001b[0;32m 353\u001b[0m indices \u001b[39m=\u001b[39m xp\u001b[39m.\u001b[39mastype(scores \u001b[39m>\u001b[39m \u001b[39m0\u001b[39m, \u001b[39mint\u001b[39m)\n", |
|
|
955 |
"File \u001b[1;32mc:\\ProgramData\\anaconda3\\Lib\\site-packages\\sklearn\\linear_model\\_base.py:332\u001b[0m, in \u001b[0;36mLinearClassifierMixin.decision_function\u001b[1;34m(self, X)\u001b[0m\n\u001b[0;32m 329\u001b[0m check_is_fitted(\u001b[39mself\u001b[39m)\n\u001b[0;32m 330\u001b[0m xp, _ \u001b[39m=\u001b[39m get_namespace(X)\n\u001b[1;32m--> 332\u001b[0m X \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_validate_data(X, accept_sparse\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mcsr\u001b[39m\u001b[39m\"\u001b[39m, reset\u001b[39m=\u001b[39m\u001b[39mFalse\u001b[39;00m)\n\u001b[0;32m 333\u001b[0m scores \u001b[39m=\u001b[39m safe_sparse_dot(X, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcoef_\u001b[39m.\u001b[39mT, dense_output\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m) \u001b[39m+\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mintercept_\n\u001b[0;32m 334\u001b[0m \u001b[39mreturn\u001b[39;00m xp\u001b[39m.\u001b[39mreshape(scores, (\u001b[39m-\u001b[39m\u001b[39m1\u001b[39m,)) \u001b[39mif\u001b[39;00m scores\u001b[39m.\u001b[39mshape[\u001b[39m1\u001b[39m] \u001b[39m==\u001b[39m \u001b[39m1\u001b[39m \u001b[39melse\u001b[39;00m scores\n", |
|
|
956 |
"File \u001b[1;32mc:\\ProgramData\\anaconda3\\Lib\\site-packages\\sklearn\\base.py:608\u001b[0m, in \u001b[0;36mBaseEstimator._validate_data\u001b[1;34m(self, X, y, reset, validate_separately, cast_to_ndarray, **check_params)\u001b[0m\n\u001b[0;32m 537\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_validate_data\u001b[39m(\n\u001b[0;32m 538\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[0;32m 539\u001b[0m X\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mno_validation\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 544\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mcheck_params,\n\u001b[0;32m 545\u001b[0m ):\n\u001b[0;32m 546\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"Validate input data and set or check the `n_features_in_` attribute.\u001b[39;00m\n\u001b[0;32m 547\u001b[0m \n\u001b[0;32m 548\u001b[0m \u001b[39m Parameters\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 606\u001b[0m \u001b[39m validated.\u001b[39;00m\n\u001b[0;32m 607\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[1;32m--> 608\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_check_feature_names(X, reset\u001b[39m=\u001b[39mreset)\n\u001b[0;32m 610\u001b[0m \u001b[39mif\u001b[39;00m y \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_get_tags()[\u001b[39m\"\u001b[39m\u001b[39mrequires_y\u001b[39m\u001b[39m\"\u001b[39m]:\n\u001b[0;32m 611\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[0;32m 612\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mThis \u001b[39m\u001b[39m{\u001b[39;00m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__class__\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__name__\u001b[39m\u001b[39m}\u001b[39;00m\u001b[39m estimator \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 613\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mrequires y to be passed, but the target y is None.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 614\u001b[0m )\n", |
|
|
957 |
"File \u001b[1;32mc:\\ProgramData\\anaconda3\\Lib\\site-packages\\sklearn\\base.py:535\u001b[0m, in \u001b[0;36mBaseEstimator._check_feature_names\u001b[1;34m(self, X, reset)\u001b[0m\n\u001b[0;32m 530\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m missing_names \u001b[39mand\u001b[39;00m \u001b[39mnot\u001b[39;00m unexpected_names:\n\u001b[0;32m 531\u001b[0m message \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m (\n\u001b[0;32m 532\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mFeature names must be in the same order as they were in fit.\u001b[39m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m\n\u001b[0;32m 533\u001b[0m )\n\u001b[1;32m--> 535\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(message)\n", |
|
|
958 |
"\u001b[1;31mValueError\u001b[0m: The feature names should match those that were passed during fit.\nFeature names seen at fit time, yet now missing:\n- Other_Cancer\n- Skin_Cancer\n" |
|
|
959 |
] |
|
|
960 |
} |
|
|
961 |
], |
|
|
962 |
"source": [ |
|
|
963 |
"# Prepare input for prediction (make sure to drop any non-feature columns if necessary)\n", |
|
|
964 |
"# X_input = input_df[columns]\n", |
|
|
965 |
"\n", |
|
|
966 |
"# Make the prediction\n", |
|
|
967 |
"prediction = loaded_model.predict(input_df)\n", |
|
|
968 |
"\n", |
|
|
969 |
"# Output the prediction\n", |
|
|
970 |
"print(f\"Predicted Class: {prediction[0]}\")" |
|
|
971 |
] |
|
|
972 |
}, |
|
|
973 |
{ |
|
|
974 |
"cell_type": "code", |
|
|
975 |
"execution_count": null, |
|
|
976 |
"metadata": {}, |
|
|
977 |
"outputs": [ |
|
|
978 |
{ |
|
|
979 |
"data": { |
|
|
980 |
"text/plain": [ |
|
|
981 |
"array(['Underweight'], dtype=object)" |
|
|
982 |
] |
|
|
983 |
}, |
|
|
984 |
"execution_count": 65, |
|
|
985 |
"metadata": {}, |
|
|
986 |
"output_type": "execute_result" |
|
|
987 |
} |
|
|
988 |
], |
|
|
989 |
"source": [ |
|
|
990 |
"prediction" |
|
|
991 |
] |
|
|
992 |
}, |
|
|
993 |
{ |
|
|
994 |
"cell_type": "code", |
|
|
995 |
"execution_count": null, |
|
|
996 |
"metadata": {}, |
|
|
997 |
"outputs": [ |
|
|
998 |
{ |
|
|
999 |
"data": { |
|
|
1000 |
"text/plain": [ |
|
|
1001 |
"array([[0.90208773, 0.01696106, 0.06855007, 0.01240114]])" |
|
|
1002 |
] |
|
|
1003 |
}, |
|
|
1004 |
"execution_count": 70, |
|
|
1005 |
"metadata": {}, |
|
|
1006 |
"output_type": "execute_result" |
|
|
1007 |
} |
|
|
1008 |
], |
|
|
1009 |
"source": [ |
|
|
1010 |
"model_filename = 'lr_dt.pkl'\n", |
|
|
1011 |
"\n", |
|
|
1012 |
"with open(model_filename, 'rb') as file:\n", |
|
|
1013 |
" loaded_model_lr_dt = pickle.load(file)\n", |
|
|
1014 |
"y_pred_loaded = loaded_model_lr_dt.predict_proba(input_df)\n", |
|
|
1015 |
"y_pred_loaded" |
|
|
1016 |
] |
|
|
1017 |
}, |
|
|
1018 |
{ |
|
|
1019 |
"cell_type": "code", |
|
|
1020 |
"execution_count": null, |
|
|
1021 |
"metadata": {}, |
|
|
1022 |
"outputs": [], |
|
|
1023 |
"source": [ |
|
|
1024 |
"df" |
|
|
1025 |
] |
|
|
1026 |
} |
|
|
1027 |
], |
|
|
1028 |
"metadata": { |
|
|
1029 |
"kernelspec": { |
|
|
1030 |
"display_name": "Python 3.12.4 ('base')", |
|
|
1031 |
"language": "python", |
|
|
1032 |
"name": "python3" |
|
|
1033 |
}, |
|
|
1034 |
"language_info": { |
|
|
1035 |
"codemirror_mode": { |
|
|
1036 |
"name": "ipython", |
|
|
1037 |
"version": 3 |
|
|
1038 |
}, |
|
|
1039 |
"file_extension": ".py", |
|
|
1040 |
"mimetype": "text/x-python", |
|
|
1041 |
"name": "python", |
|
|
1042 |
"nbconvert_exporter": "python", |
|
|
1043 |
"pygments_lexer": "ipython3", |
|
|
1044 |
"version": "3.12.4" |
|
|
1045 |
}, |
|
|
1046 |
"orig_nbformat": 4, |
|
|
1047 |
"vscode": { |
|
|
1048 |
"interpreter": { |
|
|
1049 |
"hash": "3c06e3e46abf38078fe4dac36a0085ec2b134ebbd73dd076183d243eeca6918f" |
|
|
1050 |
} |
|
|
1051 |
} |
|
|
1052 |
}, |
|
|
1053 |
"nbformat": 4, |
|
|
1054 |
"nbformat_minor": 2 |
|
|
1055 |
} |