|
a |
|
b/03-Experiments/05-LightBGM_With_FE.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "markdown", |
|
|
5 |
"metadata": {}, |
|
|
6 |
"source": [ |
|
|
7 |
"Gloabl Experiment Setup" |
|
|
8 |
] |
|
|
9 |
}, |
|
|
10 |
{ |
|
|
11 |
"cell_type": "code", |
|
|
12 |
"execution_count": 1, |
|
|
13 |
"metadata": {}, |
|
|
14 |
"outputs": [ |
|
|
15 |
{ |
|
|
16 |
"name": "stderr", |
|
|
17 |
"output_type": "stream", |
|
|
18 |
"text": [ |
|
|
19 |
"2024/04/25 15:58:03 INFO mlflow.tracking.fluent: Experiment with name 'LightGBM' does not exist. Creating a new experiment.\n" |
|
|
20 |
] |
|
|
21 |
}, |
|
|
22 |
{ |
|
|
23 |
"data": { |
|
|
24 |
"text/plain": [ |
|
|
25 |
"<Experiment: artifact_location='/Users/arham/Downloads/Projects/03-Experiments/mlruns/4', creation_time=1714075083201, experiment_id='4', last_update_time=1714075083201, lifecycle_stage='active', name='LightGBM', tags={}>" |
|
|
26 |
] |
|
|
27 |
}, |
|
|
28 |
"execution_count": 1, |
|
|
29 |
"metadata": {}, |
|
|
30 |
"output_type": "execute_result" |
|
|
31 |
} |
|
|
32 |
], |
|
|
33 |
"source": [ |
|
|
34 |
"import mlflow\n", |
|
|
35 |
"# Set the MLflow tracking URI to a new SQLite URI\n", |
|
|
36 |
"mlflow.set_tracking_uri(\"sqlite:///new_mlflow.db\")\n", |
|
|
37 |
"mlflow.set_experiment(\"LightGBM\")" |
|
|
38 |
] |
|
|
39 |
}, |
|
|
40 |
{ |
|
|
41 |
"cell_type": "code", |
|
|
42 |
"execution_count": 2, |
|
|
43 |
"metadata": {}, |
|
|
44 |
"outputs": [], |
|
|
45 |
"source": [ |
|
|
46 |
"import pandas as pd\n", |
|
|
47 |
"from sklearn.model_selection import train_test_split\n", |
|
|
48 |
"import matplotlib.pyplot as plt\n", |
|
|
49 |
"import seaborn as sns\n", |
|
|
50 |
"import numpy as np\n", |
|
|
51 |
"from sklearn.preprocessing import MinMaxScaler\n", |
|
|
52 |
"from sklearn.preprocessing import PolynomialFeatures\n", |
|
|
53 |
"import lightgbm as lgb\n", |
|
|
54 |
"from sklearn.metrics import accuracy_score\n", |
|
|
55 |
"\n", |
|
|
56 |
"def load_data(path):\n", |
|
|
57 |
" df = pd.read_csv(path)\n", |
|
|
58 |
" train_df, test_df = train_test_split(df, test_size=0.35, random_state=42)\n", |
|
|
59 |
" train_df, val_df, = train_test_split(train_df, test_size=0.20, random_state=42)\n", |
|
|
60 |
" train_df = train_df.drop(['id'], axis=1).drop_duplicates().reset_index(drop=True)\n", |
|
|
61 |
" test_df = test_df.drop(['id'], axis=1).drop_duplicates().reset_index(drop=True)\n", |
|
|
62 |
" val_df = val_df.drop(['id'], axis=1).drop_duplicates().reset_index(drop=True)\n", |
|
|
63 |
" return train_df, val_df, test_df\n", |
|
|
64 |
"\n", |
|
|
65 |
"def encode_target(train):\n", |
|
|
66 |
" target_key = {'Insufficient_Weight': 0, 'Normal_Weight': 1, 'Overweight_Level_I': 2, 'Overweight_Level_II': 3, 'Obesity_Type_I': 4,'Obesity_Type_II' : 5, 'Obesity_Type_III': 6}\n", |
|
|
67 |
" train['NObeyesdad'] = train['NObeyesdad'].map(target_key)\n", |
|
|
68 |
" return train\n", |
|
|
69 |
"\n", |
|
|
70 |
"def make_gender_binary(train):\n", |
|
|
71 |
" train['Gender'] = train['Gender'].map({'Male':0, 'Female':1})\n", |
|
|
72 |
"\n", |
|
|
73 |
"def datatypes(train):\n", |
|
|
74 |
" train['Weight'] = train['Weight'].astype(float)\n", |
|
|
75 |
" train['Age'] = train['Age'].astype(float)\n", |
|
|
76 |
" train['Height'] = train['Height'].astype(float)\n", |
|
|
77 |
" return train\n", |
|
|
78 |
"\n", |
|
|
79 |
"# def age_binning(train_df):\n", |
|
|
80 |
"# train_df['Age_Group'] = pd.cut(train_df['Age'], bins=[0, 20, 30, 40, 50, train_df['Age'].max()], labels=['0-20', '21-30', '31-40', '41-50', '50+'])\n", |
|
|
81 |
"# return train_df\n", |
|
|
82 |
"\n", |
|
|
83 |
"def age_binning(df):\n", |
|
|
84 |
" age_groups = []\n", |
|
|
85 |
" for age in df['Age']:\n", |
|
|
86 |
" if age <= 20:\n", |
|
|
87 |
" age_group = 1\n", |
|
|
88 |
" elif age <= 30:\n", |
|
|
89 |
" age_group = 2\n", |
|
|
90 |
" elif age <= 40:\n", |
|
|
91 |
" age_group = 3\n", |
|
|
92 |
" elif age <= 50:\n", |
|
|
93 |
" age_group = 4\n", |
|
|
94 |
" else:\n", |
|
|
95 |
" age_group = 5\n", |
|
|
96 |
" age_groups.append(age_group)\n", |
|
|
97 |
" df['Age_Group'] = age_groups\n", |
|
|
98 |
" return df\n", |
|
|
99 |
"\n", |
|
|
100 |
"def age_scaling_log(train_df):\n", |
|
|
101 |
" train_df['Age'] = train_df['Age'].astype(float)\n", |
|
|
102 |
" train_df['Log_Age'] = np.log1p(train_df['Age'])\n", |
|
|
103 |
" return train_df\n", |
|
|
104 |
"\n", |
|
|
105 |
"def age_scaling_minmax(train_df):\n", |
|
|
106 |
" train_df['Age'] = train_df['Age'].astype(float)\n", |
|
|
107 |
" scaler_age = MinMaxScaler()\n", |
|
|
108 |
" train_df['Scaled_Age'] = scaler_age.fit_transform(train_df['Age'].values.reshape(-1, 1))\n", |
|
|
109 |
" return train_df, scaler_age\n", |
|
|
110 |
"\n", |
|
|
111 |
"def weight_scaling_log(train_df):\n", |
|
|
112 |
" train_df['Weight'] = train_df['Weight'].astype(float)\n", |
|
|
113 |
" train_df['Log_Weight'] = np.log1p(train_df['Weight'])\n", |
|
|
114 |
" return train_df\n", |
|
|
115 |
"\n", |
|
|
116 |
"def weight_scaling_minmax(train_df):\n", |
|
|
117 |
" train_df['Weight'] = train_df['Weight'].astype(float)\n", |
|
|
118 |
" scaler_weight = MinMaxScaler()\n", |
|
|
119 |
" train_df['Scaled_Weight'] = scaler_weight.fit_transform(train_df['Weight'].values.reshape(-1, 1))\n", |
|
|
120 |
" return train_df, scaler_weight\n", |
|
|
121 |
"\n", |
|
|
122 |
"def height_scaling_log(train_df):\n", |
|
|
123 |
" train_df['Log_Height'] = np.log1p(train_df['Height'])\n", |
|
|
124 |
" return train_df\n", |
|
|
125 |
"\n", |
|
|
126 |
"def height_scaling_minmax(train_df):\n", |
|
|
127 |
" scaler_height = MinMaxScaler()\n", |
|
|
128 |
" train_df['Scaled_Height'] = scaler_height.fit_transform(train_df['Height'].values.reshape(-1, 1))\n", |
|
|
129 |
" return train_df, scaler_height\n", |
|
|
130 |
"\n", |
|
|
131 |
"def make_gender_binary(train):\n", |
|
|
132 |
" train['Gender'] = train['Gender'].map({'Female':1, 'Male':0})\n", |
|
|
133 |
" return train\n", |
|
|
134 |
"\n", |
|
|
135 |
"def fix_binary_columns(train):\n", |
|
|
136 |
" Binary_Cols = ['family_history_with_overweight','FAVC', 'SCC','SMOKE']\n", |
|
|
137 |
" # if yes then 1 else 0\n", |
|
|
138 |
" for col in Binary_Cols:\n", |
|
|
139 |
" train[col] = train[col].map({'yes': 1, 'no': 0})\n", |
|
|
140 |
" return train\n", |
|
|
141 |
"\n", |
|
|
142 |
"def freq_cat_cols(train):\n", |
|
|
143 |
" # One hot encoding\n", |
|
|
144 |
" cat_cols = ['CAEC', 'CALC']\n", |
|
|
145 |
" for col in cat_cols:\n", |
|
|
146 |
" train[col] = train[col].map({'no': 0, 'Sometimes': 1, 'Frequently': 2, 'Always': 3})\n", |
|
|
147 |
" return train\n", |
|
|
148 |
"\n", |
|
|
149 |
"def Mtrans(train):\n", |
|
|
150 |
" \"\"\"\n", |
|
|
151 |
" Public_Transportation 8692\n", |
|
|
152 |
" Automobile 1835\n", |
|
|
153 |
" Walking 231\n", |
|
|
154 |
" Motorbike 19\n", |
|
|
155 |
" Bike 16\n", |
|
|
156 |
" \"\"\"\n", |
|
|
157 |
" # train['MTRANS'] = train['MTRANS'].map({'Public_Transportation': 3, 'Automobile': 5, 'Walking': 1, 'Motorbike': 4, 'Bike': 2})\n", |
|
|
158 |
" # dummify column\n", |
|
|
159 |
" train = pd.get_dummies(train, columns=['MTRANS'])\n", |
|
|
160 |
" return train\n", |
|
|
161 |
"\n", |
|
|
162 |
"\n", |
|
|
163 |
"def other_features(train):\n", |
|
|
164 |
" train['BMI'] = train['Weight'] / (train['Height'] ** 2)\n", |
|
|
165 |
" # train['Age'*'Gender'] = train['Age'] * train['Gender']\n", |
|
|
166 |
" polynomial_features = PolynomialFeatures(degree=2)\n", |
|
|
167 |
" X_poly = polynomial_features.fit_transform(train[['Age', 'BMI']])\n", |
|
|
168 |
" poly_features_df = pd.DataFrame(X_poly, columns=['Age^2', 'Age^3', 'BMI^2', 'Age * BMI', 'Age * BMI^2', 'Age^2 * BMI^2'])\n", |
|
|
169 |
" train = pd.concat([train, poly_features_df], axis=1)\n", |
|
|
170 |
" return train\n", |
|
|
171 |
"\n", |
|
|
172 |
"\n", |
|
|
173 |
"def test_pipeline(test, scaler_age, scaler_weight, scaler_height):\n", |
|
|
174 |
" test = datatypes(test)\n", |
|
|
175 |
" test = encode_target(test)\n", |
|
|
176 |
" test = age_binning(test)\n", |
|
|
177 |
" test = age_scaling_log(test)\n", |
|
|
178 |
" test['Scaled_Age'] = scaler_age.transform(test['Age'].values.reshape(-1, 1))\n", |
|
|
179 |
" test = weight_scaling_log(test)\n", |
|
|
180 |
" test['Scaled_Weight'] = scaler_weight.transform(test['Weight'].values.reshape(-1, 1))\n", |
|
|
181 |
" test = height_scaling_log(test)\n", |
|
|
182 |
" test['Scaled_Height'] = scaler_height.transform(test['Height'].values.reshape(-1, 1))\n", |
|
|
183 |
" test = make_gender_binary(test)\n", |
|
|
184 |
" test = fix_binary_columns(test)\n", |
|
|
185 |
" test = freq_cat_cols(test)\n", |
|
|
186 |
" test = Mtrans(test)\n", |
|
|
187 |
" test = other_features(test)\n", |
|
|
188 |
"\n", |
|
|
189 |
" return test\n", |
|
|
190 |
"\n", |
|
|
191 |
"def train_model(params, X_train, y_train):\n", |
|
|
192 |
" lgb_train = lgb.Dataset(X_train, y_train)\n", |
|
|
193 |
" model = lgb.train(params, lgb_train, num_boost_round=1000)\n", |
|
|
194 |
" return model\n", |
|
|
195 |
"\n", |
|
|
196 |
"def evaluate_model(model, X_val, y_val):\n", |
|
|
197 |
" y_pred = model.predict(X_val)\n", |
|
|
198 |
" y_pred = [np.argmax(y) for y in y_pred]\n", |
|
|
199 |
" accuracy = accuracy_score(y_val, y_pred)\n", |
|
|
200 |
" return accuracy\n", |
|
|
201 |
"\n", |
|
|
202 |
"def objective(trial, X_train, y_train):\n", |
|
|
203 |
" params = {\n", |
|
|
204 |
" 'objective': 'multiclass',\n", |
|
|
205 |
" 'num_class': 7,\n", |
|
|
206 |
" 'metric': 'multi_logloss',\n", |
|
|
207 |
" 'boosting_type': 'gbdt',\n", |
|
|
208 |
" 'learning_rate': trial.suggest_loguniform('learning_rate', 0.005, 0.5),\n", |
|
|
209 |
" 'num_leaves': trial.suggest_int('num_leaves', 10, 1000),\n", |
|
|
210 |
" 'max_depth': trial.suggest_int('max_depth', -1, 20),\n", |
|
|
211 |
" 'bagging_fraction': trial.suggest_uniform('bagging_fraction', 0.6, 0.95),\n", |
|
|
212 |
" 'feature_fraction': trial.suggest_uniform('feature_fraction', 0.6, 0.95),\n", |
|
|
213 |
" 'verbosity': -1\n", |
|
|
214 |
" }\n", |
|
|
215 |
"\n", |
|
|
216 |
" n_splits = 5\n", |
|
|
217 |
" kf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=42)\n", |
|
|
218 |
" scores = []\n", |
|
|
219 |
"\n", |
|
|
220 |
" for train_index, val_index in kf.split(X_train, y_train):\n", |
|
|
221 |
" X_tr, X_val = X_train.iloc[train_index], X_train.iloc[val_index]\n", |
|
|
222 |
" y_tr, y_val = y_train.iloc[train_index], y_train.iloc[val_index]\n", |
|
|
223 |
"\n", |
|
|
224 |
" model = train_model(params, X_tr, y_tr)\n", |
|
|
225 |
" accuracy = evaluate_model(model, X_val, y_val)\n", |
|
|
226 |
" scores.append(accuracy)\n", |
|
|
227 |
"\n", |
|
|
228 |
" return np.mean(scores)\n", |
|
|
229 |
"\n", |
|
|
230 |
"def optimize_hyperparameters(X_train, y_train, n_trials=2):\n", |
|
|
231 |
" study = optuna.create_study(direction='maximize')\n", |
|
|
232 |
" study.optimize(lambda trial: objective(trial, X_train, y_train), n_trials=n_trials)\n", |
|
|
233 |
" return study.best_params\n", |
|
|
234 |
"\n", |
|
|
235 |
"def New_Test_Instances_Pipeline(test, scaler_age, scaler_weight, scaler_height):\n", |
|
|
236 |
" test = datatypes(test)\n", |
|
|
237 |
" test = age_binning(test)\n", |
|
|
238 |
" test = age_scaling_log(test)\n", |
|
|
239 |
" test['Scaled_Age'] = scaler_age.transform(test['Age'].values.reshape(-1, 1))\n", |
|
|
240 |
" test = weight_scaling_log(test)\n", |
|
|
241 |
" test['Scaled_Weight'] = scaler_weight.transform(test['Weight'].values.reshape(-1, 1))\n", |
|
|
242 |
" test = height_scaling_log(test)\n", |
|
|
243 |
" test['Scaled_Height'] = scaler_height.transform(test['Height'].values.reshape(-1, 1))\n", |
|
|
244 |
" test = make_gender_binary(test)\n", |
|
|
245 |
" test = fix_binary_columns(test)\n", |
|
|
246 |
" test = freq_cat_cols(test)\n", |
|
|
247 |
" test = Mtrans(test)\n", |
|
|
248 |
" test = other_features(test)\n", |
|
|
249 |
"\n", |
|
|
250 |
" return test" |
|
|
251 |
] |
|
|
252 |
}, |
|
|
253 |
{ |
|
|
254 |
"cell_type": "markdown", |
|
|
255 |
"metadata": {}, |
|
|
256 |
"source": [ |
|
|
257 |
"Experiment" |
|
|
258 |
] |
|
|
259 |
}, |
|
|
260 |
{ |
|
|
261 |
"cell_type": "code", |
|
|
262 |
"execution_count": 7, |
|
|
263 |
"metadata": {}, |
|
|
264 |
"outputs": [ |
|
|
265 |
{ |
|
|
266 |
"name": "stdout", |
|
|
267 |
"output_type": "stream", |
|
|
268 |
"text": [ |
|
|
269 |
"Target Drift For Each Class [0.004943133623686147, 0.011990707821925795, -0.0087675011457998, -0.001077949504617301, -0.017190035106736085, -0.00032756263090533144, 0.01042920694244659]\n", |
|
|
270 |
"[LightGBM] [Warning] Found whitespace in feature_names, replace with underlines\n", |
|
|
271 |
"[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.001163 seconds.\n", |
|
|
272 |
"You can set `force_row_wise=true` to remove the overhead.\n", |
|
|
273 |
"And if memory is not enough, you can set `force_col_wise=true`.\n", |
|
|
274 |
"[LightGBM] [Info] Total Bins 3576\n", |
|
|
275 |
"[LightGBM] [Info] Number of data points in the train set: 10793, number of used features: 25\n", |
|
|
276 |
"[LightGBM] [Info] Start training from score -2.103541\n", |
|
|
277 |
"[LightGBM] [Info] Start training from score -1.893390\n", |
|
|
278 |
"[LightGBM] [Info] Start training from score -2.159762\n", |
|
|
279 |
"[LightGBM] [Info] Start training from score -2.113461\n", |
|
|
280 |
"[LightGBM] [Info] Start training from score -1.974767\n", |
|
|
281 |
"[LightGBM] [Info] Start training from score -1.867272\n", |
|
|
282 |
"[LightGBM] [Info] Start training from score -1.619963\n", |
|
|
283 |
"[LightGBM] [Warning] Found whitespace in feature_names, replace with underlines\n", |
|
|
284 |
"[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.000883 seconds.\n", |
|
|
285 |
"You can set `force_row_wise=true` to remove the overhead.\n", |
|
|
286 |
"And if memory is not enough, you can set `force_col_wise=true`.\n", |
|
|
287 |
"[LightGBM] [Info] Total Bins 3576\n", |
|
|
288 |
"[LightGBM] [Info] Number of data points in the train set: 8634, number of used features: 25\n", |
|
|
289 |
"[LightGBM] [Info] Start training from score -2.104065\n", |
|
|
290 |
"[LightGBM] [Info] Start training from score -1.893344\n", |
|
|
291 |
"[LightGBM] [Info] Start training from score -2.159716\n", |
|
|
292 |
"[LightGBM] [Info] Start training from score -2.113607\n", |
|
|
293 |
"[LightGBM] [Info] Start training from score -1.974220\n", |
|
|
294 |
"[LightGBM] [Info] Start training from score -1.867526\n", |
|
|
295 |
"[LightGBM] [Info] Start training from score -1.619799\n", |
|
|
296 |
"[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n", |
|
|
297 |
"[LightGBM] [Warning] Found whitespace in feature_names, replace with underlines\n", |
|
|
298 |
"[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.001080 seconds.\n", |
|
|
299 |
"You can set `force_row_wise=true` to remove the overhead.\n", |
|
|
300 |
"And if memory is not enough, you can set `force_col_wise=true`.\n", |
|
|
301 |
"[LightGBM] [Info] Total Bins 3573\n", |
|
|
302 |
"[LightGBM] [Info] Number of data points in the train set: 8634, number of used features: 25\n", |
|
|
303 |
"[LightGBM] [Info] Start training from score -2.104065\n", |
|
|
304 |
"[LightGBM] [Info] Start training from score -1.893344\n", |
|
|
305 |
"[LightGBM] [Info] Start training from score -2.159716\n", |
|
|
306 |
"[LightGBM] [Info] Start training from score -2.112648\n", |
|
|
307 |
"[LightGBM] [Info] Start training from score -1.974220\n", |
|
|
308 |
"[LightGBM] [Info] Start training from score -1.867526\n", |
|
|
309 |
"[LightGBM] [Info] Start training from score -1.620385\n", |
|
|
310 |
"[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n", |
|
|
311 |
"[LightGBM] [Warning] Found whitespace in feature_names, replace with underlines\n", |
|
|
312 |
"[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.000459 seconds.\n", |
|
|
313 |
"You can set `force_row_wise=true` to remove the overhead.\n", |
|
|
314 |
"And if memory is not enough, you can set `force_col_wise=true`.\n", |
|
|
315 |
"[LightGBM] [Info] Total Bins 3572\n", |
|
|
316 |
"[LightGBM] [Info] Number of data points in the train set: 8634, number of used features: 25\n", |
|
|
317 |
"[LightGBM] [Info] Start training from score -2.103115\n", |
|
|
318 |
"[LightGBM] [Info] Start training from score -1.893344\n", |
|
|
319 |
"[LightGBM] [Info] Start training from score -2.159716\n", |
|
|
320 |
"[LightGBM] [Info] Start training from score -2.113607\n", |
|
|
321 |
"[LightGBM] [Info] Start training from score -1.975054\n", |
|
|
322 |
"[LightGBM] [Info] Start training from score -1.867526\n", |
|
|
323 |
"[LightGBM] [Info] Start training from score -1.619799\n", |
|
|
324 |
"[LightGBM] [Warning] Found whitespace in feature_names, replace with underlines\n", |
|
|
325 |
"[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.001021 seconds.\n", |
|
|
326 |
"You can set `force_row_wise=true` to remove the overhead.\n", |
|
|
327 |
"And if memory is not enough, you can set `force_col_wise=true`.\n", |
|
|
328 |
"[LightGBM] [Info] Total Bins 3571\n", |
|
|
329 |
"[LightGBM] [Info] Number of data points in the train set: 8635, number of used features: 25\n", |
|
|
330 |
"[LightGBM] [Info] Start training from score -2.103231\n", |
|
|
331 |
"[LightGBM] [Info] Start training from score -1.893459\n", |
|
|
332 |
"[LightGBM] [Info] Start training from score -2.159832\n", |
|
|
333 |
"[LightGBM] [Info] Start training from score -2.113723\n", |
|
|
334 |
"[LightGBM] [Info] Start training from score -1.975170\n", |
|
|
335 |
"[LightGBM] [Info] Start training from score -1.866892\n", |
|
|
336 |
"[LightGBM] [Info] Start training from score -1.619915\n", |
|
|
337 |
"[LightGBM] [Warning] Found whitespace in feature_names, replace with underlines\n", |
|
|
338 |
"[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.000919 seconds.\n", |
|
|
339 |
"You can set `force_row_wise=true` to remove the overhead.\n", |
|
|
340 |
"And if memory is not enough, you can set `force_col_wise=true`.\n", |
|
|
341 |
"[LightGBM] [Info] Total Bins 3575\n", |
|
|
342 |
"[LightGBM] [Info] Number of data points in the train set: 8635, number of used features: 25\n", |
|
|
343 |
"[LightGBM] [Info] Start training from score -2.103231\n", |
|
|
344 |
"[LightGBM] [Info] Start training from score -1.893459\n", |
|
|
345 |
"[LightGBM] [Info] Start training from score -2.159832\n", |
|
|
346 |
"[LightGBM] [Info] Start training from score -2.113723\n", |
|
|
347 |
"[LightGBM] [Info] Start training from score -1.975170\n", |
|
|
348 |
"[LightGBM] [Info] Start training from score -1.866892\n", |
|
|
349 |
"[LightGBM] [Info] Start training from score -1.619915\n", |
|
|
350 |
"[LightGBM] [Warning] No further splits with positive gain, best gain: -inf\n", |
|
|
351 |
"\n", |
|
|
352 |
"Accuracy: 0.904845733345687\n", |
|
|
353 |
"Precision: 0.9046557231546489\n", |
|
|
354 |
"Recall: 0.904845733345687\n", |
|
|
355 |
"F1: 0.9046297258523301\n", |
|
|
356 |
"[LightGBM] [Warning] Found whitespace in feature_names, replace with underlines\n", |
|
|
357 |
"[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.001173 seconds.\n", |
|
|
358 |
"You can set `force_row_wise=true` to remove the overhead.\n", |
|
|
359 |
"And if memory is not enough, you can set `force_col_wise=true`.\n", |
|
|
360 |
"[LightGBM] [Info] Total Bins 3576\n", |
|
|
361 |
"[LightGBM] [Info] Number of data points in the train set: 10793, number of used features: 25\n", |
|
|
362 |
"[LightGBM] [Info] Start training from score -2.103541\n", |
|
|
363 |
"[LightGBM] [Info] Start training from score -1.893390\n", |
|
|
364 |
"[LightGBM] [Info] Start training from score -2.159762\n", |
|
|
365 |
"[LightGBM] [Info] Start training from score -2.113461\n", |
|
|
366 |
"[LightGBM] [Info] Start training from score -1.974767\n", |
|
|
367 |
"[LightGBM] [Info] Start training from score -1.867272\n", |
|
|
368 |
"[LightGBM] [Info] Start training from score -1.619963\n", |
|
|
369 |
"Recall for class 0: 0.9367088607594937\n", |
|
|
370 |
"Recall for class 1: 0.9117647058823529\n", |
|
|
371 |
"Recall for class 2: 0.755223880597015\n", |
|
|
372 |
"Recall for class 3: 0.8267477203647416\n", |
|
|
373 |
"Recall for class 4: 0.8669833729216152\n", |
|
|
374 |
"Recall for class 5: 0.9617224880382775\n", |
|
|
375 |
"Recall for class 6: 0.9960474308300395\n" |
|
|
376 |
] |
|
|
377 |
} |
|
|
378 |
], |
|
|
379 |
"source": [ |
|
|
380 |
"path = '/Users/arham/Downloads/Projects/01-Dataset/01-Data-for-model-building/train.csv'\n", |
|
|
381 |
"train_df, val_df, test_df = load_data(path)\n", |
|
|
382 |
"\n", |
|
|
383 |
"# train test val pipeline\n", |
|
|
384 |
"train_df = datatypes(train_df)\n", |
|
|
385 |
"train_df = encode_target(train_df)\n", |
|
|
386 |
"train_df = age_binning(train_df)\n", |
|
|
387 |
"train_df, scaler_age = age_scaling_minmax(train_df)\n", |
|
|
388 |
"train_df = age_scaling_log(train_df)\n", |
|
|
389 |
"train_df, scaler_weight = weight_scaling_minmax(train_df)\n", |
|
|
390 |
"train_df = weight_scaling_log(train_df)\n", |
|
|
391 |
"train_df, scaler_height = height_scaling_minmax(train_df)\n", |
|
|
392 |
"train_df = height_scaling_log(train_df)\n", |
|
|
393 |
"train_df = make_gender_binary(train_df)\n", |
|
|
394 |
"train_df = fix_binary_columns(train_df)\n", |
|
|
395 |
"train_df = freq_cat_cols(train_df)\n", |
|
|
396 |
"train_df = Mtrans(train_df)\n", |
|
|
397 |
"train_df = other_features(train_df)\n", |
|
|
398 |
"val_df = test_pipeline(val_df, scaler_age, scaler_weight, scaler_height)\n", |
|
|
399 |
"test_df = test_pipeline(test_df, scaler_age, scaler_weight, scaler_height)\n", |
|
|
400 |
"\n", |
|
|
401 |
"\n", |
|
|
402 |
"# target & predictors\n", |
|
|
403 |
"Target = 'NObeyesdad'\n", |
|
|
404 |
"features = ['Gender', 'Age', 'Height', 'Weight', 'family_history_with_overweight',\n", |
|
|
405 |
" 'FAVC', 'FCVC', 'NCP', 'CAEC', 'SMOKE', 'CH2O', 'SCC', 'FAF', 'TUE',\n", |
|
|
406 |
" 'CALC', 'Age_Group', \n", |
|
|
407 |
" 'MTRANS_Automobile', 'MTRANS_Bike', 'MTRANS_Motorbike',\n", |
|
|
408 |
" 'MTRANS_Public_Transportation', 'MTRANS_Walking', 'BMI', 'Age^2',\n", |
|
|
409 |
" 'Age^3', 'BMI^2', 'Age * BMI', 'Age * BMI^2', 'Age^2 * BMI^2'] \n", |
|
|
410 |
" #'Scaled_Age', 'Log_Age', 'Scaled_Weight', 'Log_Weight', 'Scaled_Height', 'Log_Height',\n", |
|
|
411 |
"\n", |
|
|
412 |
"X_train = train_df[features]\n", |
|
|
413 |
"y_train = train_df[Target]\n", |
|
|
414 |
"X_val = val_df[features]\n", |
|
|
415 |
"y_val = val_df[Target]\n", |
|
|
416 |
"X_test = test_df[features]\n", |
|
|
417 |
"y_test = test_df[Target]\n", |
|
|
418 |
"\n", |
|
|
419 |
"\n", |
|
|
420 |
"lgb_train = lgb.Dataset(X_train, y_train)\n", |
|
|
421 |
"params = {\n", |
|
|
422 |
" 'objective': 'multiclass',\n", |
|
|
423 |
" 'num_class': 7,\n", |
|
|
424 |
" 'metric': 'multi_logloss',\n", |
|
|
425 |
"}\n", |
|
|
426 |
"\n", |
|
|
427 |
"from sklearn.metrics import precision_recall_fscore_support, accuracy_score\n", |
|
|
428 |
"import mlflow\n", |
|
|
429 |
"import lightgbm as lgb\n", |
|
|
430 |
"from lightgbm import LGBMClassifier\n", |
|
|
431 |
"from sklearn.model_selection import cross_val_predict\n", |
|
|
432 |
"\n", |
|
|
433 |
"mlflow.sklearn.autolog(disable=True)\n", |
|
|
434 |
"\n", |
|
|
435 |
"with mlflow.start_run(run_name=\"LGBM_without_FE_v2\"):\n", |
|
|
436 |
" class_counts_train = [y_train[y_train == i].count() / y_train.count() for i in range(7)]\n", |
|
|
437 |
" class_counts_val = [y_val[y_val == i].count() / y_val.count() for i in range(7)]\n", |
|
|
438 |
" target_drift = [(train_count - val_count) for train_count, val_count in zip(class_counts_train, class_counts_val)]\n", |
|
|
439 |
" print(f\"Target Drift For Each Class {target_drift}\")\n", |
|
|
440 |
" mlflow.log_params({'Target_Drift_' + str(i): freq for i, freq in enumerate(target_drift)})\n", |
|
|
441 |
"\n", |
|
|
442 |
" model = LGBMClassifier(**params) # Assuming you have your parameters defined somewhere\n", |
|
|
443 |
" model.fit(X_train, y_train) # Fit the model on training data\n", |
|
|
444 |
"\n", |
|
|
445 |
" # CV predictions of LightGBM\n", |
|
|
446 |
" cv_predictions = cross_val_predict(model, X_train, y_train, cv=5)\n", |
|
|
447 |
" accuracy_lgbm = accuracy_score(y_train, cv_predictions)\n", |
|
|
448 |
" \n", |
|
|
449 |
" # Compute precision, recall, and F1-score\n", |
|
|
450 |
" precision_lgbm, recall_lgbm, f1_lgbm, _ = precision_recall_fscore_support(y_train, cv_predictions, average='weighted')\n", |
|
|
451 |
" \n", |
|
|
452 |
" print(\"\\nAccuracy:\", accuracy_lgbm)\n", |
|
|
453 |
" print(\"Precision:\", precision_lgbm)\n", |
|
|
454 |
" print(\"Recall:\", recall_lgbm)\n", |
|
|
455 |
" print(\"F1:\", f1_lgbm)\n", |
|
|
456 |
" \n", |
|
|
457 |
" mlflow.log_metric('accuracy', accuracy_lgbm)\n", |
|
|
458 |
" mlflow.log_metric('precision', precision_lgbm)\n", |
|
|
459 |
" mlflow.log_metric('recall', recall_lgbm)\n", |
|
|
460 |
" mlflow.log_metric('f1', f1_lgbm)\n", |
|
|
461 |
"\n", |
|
|
462 |
" model.fit(X_train, y_train)\n", |
|
|
463 |
" y_val_pred_lgbm = model.predict(X_val)\n", |
|
|
464 |
" \n", |
|
|
465 |
" # Compute precision, recall, and F1-score per class\n", |
|
|
466 |
" precision_per_class, recall_per_class, f1_per_class, support_per_class = precision_recall_fscore_support(y_val, y_val_pred_lgbm, average=None)\n", |
|
|
467 |
" for i in range(len(recall_per_class)):\n", |
|
|
468 |
" print(f\"Recall for class {i}: {recall_per_class[i]}\")\n", |
|
|
469 |
" mlflow.log_metric(f'recall_class_{i}', recall_per_class[i])\n", |
|
|
470 |
"\n", |
|
|
471 |
" mlflow.lightgbm.log_model(model, 'model')\n", |
|
|
472 |
" mlflow.set_tag('experiments', 'Arham A.')\n", |
|
|
473 |
" mlflow.set_tag('model_name', 'LGBM')\n", |
|
|
474 |
" mlflow.set_tag('preprocessing', 'Yes')\n" |
|
|
475 |
] |
|
|
476 |
}, |
|
|
477 |
{ |
|
|
478 |
"cell_type": "code", |
|
|
479 |
"execution_count": 39, |
|
|
480 |
"metadata": {}, |
|
|
481 |
"outputs": [], |
|
|
482 |
"source": [ |
|
|
483 |
"# train_df, val_df, test_df = load_data(path)\n", |
|
|
484 |
"\n", |
|
|
485 |
"\n", |
|
|
486 |
"# X_val = val_df[features]\n", |
|
|
487 |
"# y_val = val_df[Target]\n", |
|
|
488 |
"# y_pred = model.predict(X_val, num_iteration=model.best_iteration)\n", |
|
|
489 |
"# # y_pred to a dataframe\n", |
|
|
490 |
"# y_pred = pd.DataFrame(y_pred, columns=['Insufficient_Weight', 'Normal_Weight', 'Overweight_Level_I', 'Overweight_Level_II', 'Obesity_Type_I', 'Obesity_Type_II', 'Obesity_Type_III'])\n", |
|
|
491 |
"# # add prefix to columns \"prob_lgbm_\"\n", |
|
|
492 |
"# y_pred = y_pred.add_prefix('prob_lgbm_')\n", |
|
|
493 |
"# # add to X_val\n", |
|
|
494 |
"# X_val = pd.concat([X_val, y_pred], axis=1)\n", |
|
|
495 |
"# # export as stack_aid_lgbm.csv\n", |
|
|
496 |
"# X_val.to_csv('stack_aid_lgbm.csv', index=False)" |
|
|
497 |
] |
|
|
498 |
}, |
|
|
499 |
{ |
|
|
500 |
"cell_type": "code", |
|
|
501 |
"execution_count": 25, |
|
|
502 |
"metadata": {}, |
|
|
503 |
"outputs": [ |
|
|
504 |
{ |
|
|
505 |
"data": { |
|
|
506 |
"text/html": [ |
|
|
507 |
"<div>\n", |
|
|
508 |
"<style scoped>\n", |
|
|
509 |
" .dataframe tbody tr th:only-of-type {\n", |
|
|
510 |
" vertical-align: middle;\n", |
|
|
511 |
" }\n", |
|
|
512 |
"\n", |
|
|
513 |
" .dataframe tbody tr th {\n", |
|
|
514 |
" vertical-align: top;\n", |
|
|
515 |
" }\n", |
|
|
516 |
"\n", |
|
|
517 |
" .dataframe thead th {\n", |
|
|
518 |
" text-align: right;\n", |
|
|
519 |
" }\n", |
|
|
520 |
"</style>\n", |
|
|
521 |
"<table border=\"1\" class=\"dataframe\">\n", |
|
|
522 |
" <thead>\n", |
|
|
523 |
" <tr style=\"text-align: right;\">\n", |
|
|
524 |
" <th></th>\n", |
|
|
525 |
" <th>Gender</th>\n", |
|
|
526 |
" <th>Age</th>\n", |
|
|
527 |
" <th>Height</th>\n", |
|
|
528 |
" <th>Weight</th>\n", |
|
|
529 |
" <th>family_history_with_overweight</th>\n", |
|
|
530 |
" <th>FAVC</th>\n", |
|
|
531 |
" <th>FCVC</th>\n", |
|
|
532 |
" <th>NCP</th>\n", |
|
|
533 |
" <th>CAEC</th>\n", |
|
|
534 |
" <th>SMOKE</th>\n", |
|
|
535 |
" <th>CH2O</th>\n", |
|
|
536 |
" <th>SCC</th>\n", |
|
|
537 |
" <th>FAF</th>\n", |
|
|
538 |
" <th>TUE</th>\n", |
|
|
539 |
" <th>CALC</th>\n", |
|
|
540 |
" <th>Age_Group</th>\n", |
|
|
541 |
" <th>MTRANS_Automobile</th>\n", |
|
|
542 |
" <th>MTRANS_Bike</th>\n", |
|
|
543 |
" <th>MTRANS_Motorbike</th>\n", |
|
|
544 |
" <th>MTRANS_Public_Transportation</th>\n", |
|
|
545 |
" <th>MTRANS_Walking</th>\n", |
|
|
546 |
" <th>BMI</th>\n", |
|
|
547 |
" <th>Age^2</th>\n", |
|
|
548 |
" <th>Age^3</th>\n", |
|
|
549 |
" <th>BMI^2</th>\n", |
|
|
550 |
" <th>Age * BMI</th>\n", |
|
|
551 |
" <th>Age * BMI^2</th>\n", |
|
|
552 |
" <th>Age^2 * BMI^2</th>\n", |
|
|
553 |
" </tr>\n", |
|
|
554 |
" </thead>\n", |
|
|
555 |
" <tbody>\n", |
|
|
556 |
" <tr>\n", |
|
|
557 |
" <th>0</th>\n", |
|
|
558 |
" <td>1</td>\n", |
|
|
559 |
" <td>21.000000</td>\n", |
|
|
560 |
" <td>1.550000</td>\n", |
|
|
561 |
" <td>51.000000</td>\n", |
|
|
562 |
" <td>0</td>\n", |
|
|
563 |
" <td>1</td>\n", |
|
|
564 |
" <td>3.0</td>\n", |
|
|
565 |
" <td>1.0</td>\n", |
|
|
566 |
" <td>2</td>\n", |
|
|
567 |
" <td>0</td>\n", |
|
|
568 |
" <td>2.000000</td>\n", |
|
|
569 |
" <td>0</td>\n", |
|
|
570 |
" <td>0.000000</td>\n", |
|
|
571 |
" <td>0.000000</td>\n", |
|
|
572 |
" <td>0</td>\n", |
|
|
573 |
" <td>21-30</td>\n", |
|
|
574 |
" <td>False</td>\n", |
|
|
575 |
" <td>False</td>\n", |
|
|
576 |
" <td>False</td>\n", |
|
|
577 |
" <td>True</td>\n", |
|
|
578 |
" <td>False</td>\n", |
|
|
579 |
" <td>21.227888</td>\n", |
|
|
580 |
" <td>1.0</td>\n", |
|
|
581 |
" <td>21.000000</td>\n", |
|
|
582 |
" <td>21.227888</td>\n", |
|
|
583 |
" <td>441.000000</td>\n", |
|
|
584 |
" <td>445.785640</td>\n", |
|
|
585 |
" <td>450.623213</td>\n", |
|
|
586 |
" </tr>\n", |
|
|
587 |
" <tr>\n", |
|
|
588 |
" <th>1</th>\n", |
|
|
589 |
" <td>0</td>\n", |
|
|
590 |
" <td>20.000000</td>\n", |
|
|
591 |
" <td>1.700000</td>\n", |
|
|
592 |
" <td>80.000000</td>\n", |
|
|
593 |
" <td>1</td>\n", |
|
|
594 |
" <td>1</td>\n", |
|
|
595 |
" <td>2.0</td>\n", |
|
|
596 |
" <td>3.0</td>\n", |
|
|
597 |
" <td>1</td>\n", |
|
|
598 |
" <td>0</td>\n", |
|
|
599 |
" <td>2.000000</td>\n", |
|
|
600 |
" <td>0</td>\n", |
|
|
601 |
" <td>2.000000</td>\n", |
|
|
602 |
" <td>1.000000</td>\n", |
|
|
603 |
" <td>1</td>\n", |
|
|
604 |
" <td>0-20</td>\n", |
|
|
605 |
" <td>False</td>\n", |
|
|
606 |
" <td>False</td>\n", |
|
|
607 |
" <td>False</td>\n", |
|
|
608 |
" <td>True</td>\n", |
|
|
609 |
" <td>False</td>\n", |
|
|
610 |
" <td>27.681661</td>\n", |
|
|
611 |
" <td>1.0</td>\n", |
|
|
612 |
" <td>20.000000</td>\n", |
|
|
613 |
" <td>27.681661</td>\n", |
|
|
614 |
" <td>400.000000</td>\n", |
|
|
615 |
" <td>553.633218</td>\n", |
|
|
616 |
" <td>766.274350</td>\n", |
|
|
617 |
" </tr>\n", |
|
|
618 |
" <tr>\n", |
|
|
619 |
" <th>2</th>\n", |
|
|
620 |
" <td>1</td>\n", |
|
|
621 |
" <td>18.000000</td>\n", |
|
|
622 |
" <td>1.600000</td>\n", |
|
|
623 |
" <td>60.000000</td>\n", |
|
|
624 |
" <td>1</td>\n", |
|
|
625 |
" <td>1</td>\n", |
|
|
626 |
" <td>2.0</td>\n", |
|
|
627 |
" <td>3.0</td>\n", |
|
|
628 |
" <td>1</td>\n", |
|
|
629 |
" <td>0</td>\n", |
|
|
630 |
" <td>2.000000</td>\n", |
|
|
631 |
" <td>0</td>\n", |
|
|
632 |
" <td>1.000000</td>\n", |
|
|
633 |
" <td>1.000000</td>\n", |
|
|
634 |
" <td>0</td>\n", |
|
|
635 |
" <td>0-20</td>\n", |
|
|
636 |
" <td>False</td>\n", |
|
|
637 |
" <td>False</td>\n", |
|
|
638 |
" <td>False</td>\n", |
|
|
639 |
" <td>False</td>\n", |
|
|
640 |
" <td>True</td>\n", |
|
|
641 |
" <td>23.437500</td>\n", |
|
|
642 |
" <td>1.0</td>\n", |
|
|
643 |
" <td>18.000000</td>\n", |
|
|
644 |
" <td>23.437500</td>\n", |
|
|
645 |
" <td>324.000000</td>\n", |
|
|
646 |
" <td>421.875000</td>\n", |
|
|
647 |
" <td>549.316406</td>\n", |
|
|
648 |
" </tr>\n", |
|
|
649 |
" <tr>\n", |
|
|
650 |
" <th>3</th>\n", |
|
|
651 |
" <td>1</td>\n", |
|
|
652 |
" <td>26.000000</td>\n", |
|
|
653 |
" <td>1.632983</td>\n", |
|
|
654 |
" <td>111.720238</td>\n", |
|
|
655 |
" <td>1</td>\n", |
|
|
656 |
" <td>1</td>\n", |
|
|
657 |
" <td>3.0</td>\n", |
|
|
658 |
" <td>3.0</td>\n", |
|
|
659 |
" <td>1</td>\n", |
|
|
660 |
" <td>0</td>\n", |
|
|
661 |
" <td>2.559750</td>\n", |
|
|
662 |
" <td>0</td>\n", |
|
|
663 |
" <td>0.000000</td>\n", |
|
|
664 |
" <td>0.396972</td>\n", |
|
|
665 |
" <td>1</td>\n", |
|
|
666 |
" <td>21-30</td>\n", |
|
|
667 |
" <td>False</td>\n", |
|
|
668 |
" <td>False</td>\n", |
|
|
669 |
" <td>False</td>\n", |
|
|
670 |
" <td>True</td>\n", |
|
|
671 |
" <td>False</td>\n", |
|
|
672 |
" <td>41.895611</td>\n", |
|
|
673 |
" <td>1.0</td>\n", |
|
|
674 |
" <td>26.000000</td>\n", |
|
|
675 |
" <td>41.895611</td>\n", |
|
|
676 |
" <td>676.000000</td>\n", |
|
|
677 |
" <td>1089.285877</td>\n", |
|
|
678 |
" <td>1755.242193</td>\n", |
|
|
679 |
" </tr>\n", |
|
|
680 |
" <tr>\n", |
|
|
681 |
" <th>4</th>\n", |
|
|
682 |
" <td>1</td>\n", |
|
|
683 |
" <td>21.682636</td>\n", |
|
|
684 |
" <td>1.748524</td>\n", |
|
|
685 |
" <td>133.845064</td>\n", |
|
|
686 |
" <td>1</td>\n", |
|
|
687 |
" <td>1</td>\n", |
|
|
688 |
" <td>3.0</td>\n", |
|
|
689 |
" <td>3.0</td>\n", |
|
|
690 |
" <td>1</td>\n", |
|
|
691 |
" <td>0</td>\n", |
|
|
692 |
" <td>2.843777</td>\n", |
|
|
693 |
" <td>0</td>\n", |
|
|
694 |
" <td>1.427037</td>\n", |
|
|
695 |
" <td>0.849236</td>\n", |
|
|
696 |
" <td>1</td>\n", |
|
|
697 |
" <td>21-30</td>\n", |
|
|
698 |
" <td>False</td>\n", |
|
|
699 |
" <td>False</td>\n", |
|
|
700 |
" <td>False</td>\n", |
|
|
701 |
" <td>True</td>\n", |
|
|
702 |
" <td>False</td>\n", |
|
|
703 |
" <td>43.778327</td>\n", |
|
|
704 |
" <td>1.0</td>\n", |
|
|
705 |
" <td>21.682636</td>\n", |
|
|
706 |
" <td>43.778327</td>\n", |
|
|
707 |
" <td>470.136704</td>\n", |
|
|
708 |
" <td>949.229536</td>\n", |
|
|
709 |
" <td>1916.541944</td>\n", |
|
|
710 |
" </tr>\n", |
|
|
711 |
" </tbody>\n", |
|
|
712 |
"</table>\n", |
|
|
713 |
"</div>" |
|
|
714 |
], |
|
|
715 |
"text/plain": [ |
|
|
716 |
" Gender Age Height Weight family_history_with_overweight \\\n", |
|
|
717 |
"0 1 21.000000 1.550000 51.000000 0 \n", |
|
|
718 |
"1 0 20.000000 1.700000 80.000000 1 \n", |
|
|
719 |
"2 1 18.000000 1.600000 60.000000 1 \n", |
|
|
720 |
"3 1 26.000000 1.632983 111.720238 1 \n", |
|
|
721 |
"4 1 21.682636 1.748524 133.845064 1 \n", |
|
|
722 |
"\n", |
|
|
723 |
" FAVC FCVC NCP CAEC SMOKE CH2O SCC FAF TUE CALC \\\n", |
|
|
724 |
"0 1 3.0 1.0 2 0 2.000000 0 0.000000 0.000000 0 \n", |
|
|
725 |
"1 1 2.0 3.0 1 0 2.000000 0 2.000000 1.000000 1 \n", |
|
|
726 |
"2 1 2.0 3.0 1 0 2.000000 0 1.000000 1.000000 0 \n", |
|
|
727 |
"3 1 3.0 3.0 1 0 2.559750 0 0.000000 0.396972 1 \n", |
|
|
728 |
"4 1 3.0 3.0 1 0 2.843777 0 1.427037 0.849236 1 \n", |
|
|
729 |
"\n", |
|
|
730 |
" Age_Group MTRANS_Automobile MTRANS_Bike MTRANS_Motorbike \\\n", |
|
|
731 |
"0 21-30 False False False \n", |
|
|
732 |
"1 0-20 False False False \n", |
|
|
733 |
"2 0-20 False False False \n", |
|
|
734 |
"3 21-30 False False False \n", |
|
|
735 |
"4 21-30 False False False \n", |
|
|
736 |
"\n", |
|
|
737 |
" MTRANS_Public_Transportation MTRANS_Walking BMI Age^2 Age^3 \\\n", |
|
|
738 |
"0 True False 21.227888 1.0 21.000000 \n", |
|
|
739 |
"1 True False 27.681661 1.0 20.000000 \n", |
|
|
740 |
"2 False True 23.437500 1.0 18.000000 \n", |
|
|
741 |
"3 True False 41.895611 1.0 26.000000 \n", |
|
|
742 |
"4 True False 43.778327 1.0 21.682636 \n", |
|
|
743 |
"\n", |
|
|
744 |
" BMI^2 Age * BMI Age * BMI^2 Age^2 * BMI^2 \n", |
|
|
745 |
"0 21.227888 441.000000 445.785640 450.623213 \n", |
|
|
746 |
"1 27.681661 400.000000 553.633218 766.274350 \n", |
|
|
747 |
"2 23.437500 324.000000 421.875000 549.316406 \n", |
|
|
748 |
"3 41.895611 676.000000 1089.285877 1755.242193 \n", |
|
|
749 |
"4 43.778327 470.136704 949.229536 1916.541944 " |
|
|
750 |
] |
|
|
751 |
}, |
|
|
752 |
"execution_count": 25, |
|
|
753 |
"metadata": {}, |
|
|
754 |
"output_type": "execute_result" |
|
|
755 |
} |
|
|
756 |
], |
|
|
757 |
"source": [ |
|
|
758 |
"# show all columns\n", |
|
|
759 |
"pd.set_option('display.max_columns', None)\n", |
|
|
760 |
"X_train.head()" |
|
|
761 |
] |
|
|
762 |
}, |
|
|
763 |
{ |
|
|
764 |
"cell_type": "markdown", |
|
|
765 |
"metadata": {}, |
|
|
766 |
"source": [ |
|
|
767 |
"Testing Single Instance For Architecture Development" |
|
|
768 |
] |
|
|
769 |
}, |
|
|
770 |
{ |
|
|
771 |
"cell_type": "code", |
|
|
772 |
"execution_count": 37, |
|
|
773 |
"metadata": {}, |
|
|
774 |
"outputs": [], |
|
|
775 |
"source": [ |
|
|
776 |
"input_data = {\n", |
|
|
777 |
" \"id\": 6204,\n", |
|
|
778 |
" \"Gender\": \"Female\",\n", |
|
|
779 |
" \"Age\": 23.0,\n", |
|
|
780 |
" \"Height\": 1.581527,\n", |
|
|
781 |
" \"Weight\": 78.089575,\n", |
|
|
782 |
" \"family_history_with_overweight\": \"yes\",\n", |
|
|
783 |
" \"FAVC\": \"yes\",\n", |
|
|
784 |
" \"FCVC\": 2.0,\n", |
|
|
785 |
" \"NCP\": 2.070033,\n", |
|
|
786 |
" \"CAEC\": \"Sometimes\",\n", |
|
|
787 |
" \"SMOKE\": \"no\", \n", |
|
|
788 |
" \"CH2O\": 2.953192,\n", |
|
|
789 |
" \"SCC\": \"no\",\n", |
|
|
790 |
" \"FAF\": 0.118271,\n", |
|
|
791 |
" \"TUE\": 0.0,\n", |
|
|
792 |
" \"CALC\": \"no\",\n", |
|
|
793 |
" \"MTRANS\": \"Public_Transportation\"\n", |
|
|
794 |
" \n", |
|
|
795 |
"}\n", |
|
|
796 |
"\n", |
|
|
797 |
"input_df = pd.DataFrame([input_data])\n", |
|
|
798 |
"input_df = New_Test_Instances_Pipeline(input_df, scaler_age, scaler_weight, scaler_height)\n", |
|
|
799 |
"\n", |
|
|
800 |
"# X input to have same columns as features\n", |
|
|
801 |
"X_input = pd.DataFrame(columns=features)\n", |
|
|
802 |
"# if input df does not have a column that is in features, add it with 0s at the same position\n", |
|
|
803 |
"for col in features:\n", |
|
|
804 |
" if col not in input_df.columns:\n", |
|
|
805 |
" if col in ['MTRANS_Automobile', 'MTRANS_Bike', 'MTRANS_Motorbike', 'MTRANS_Public_Transportation', 'MTRANS_Walking']:\n", |
|
|
806 |
" X_input[col] = False\n", |
|
|
807 |
" else:\n", |
|
|
808 |
" X_input[col] = 0\n", |
|
|
809 |
" else:\n", |
|
|
810 |
" X_input[col] = input_df[col]\n", |
|
|
811 |
" # if MTRANS_Automobile, MTRANS_Bike, MTRANS_Motorbike, MTRANS_Public_Transportation, MTRANS_Walking are zero, make them False\n", |
|
|
812 |
" \n", |
|
|
813 |
"y_pred_proba = model.predict(X_input)\n", |
|
|
814 |
"y_pred = np.argmax(y_pred_proba)\n", |
|
|
815 |
"\n", |
|
|
816 |
"y_pred" |
|
|
817 |
] |
|
|
818 |
}, |
|
|
819 |
{ |
|
|
820 |
"cell_type": "code", |
|
|
821 |
"execution_count": 38, |
|
|
822 |
"metadata": {}, |
|
|
823 |
"outputs": [ |
|
|
824 |
{ |
|
|
825 |
"data": { |
|
|
826 |
"text/plain": [ |
|
|
827 |
"4" |
|
|
828 |
] |
|
|
829 |
}, |
|
|
830 |
"execution_count": 38, |
|
|
831 |
"metadata": {}, |
|
|
832 |
"output_type": "execute_result" |
|
|
833 |
} |
|
|
834 |
], |
|
|
835 |
"source": [] |
|
|
836 |
}, |
|
|
837 |
{ |
|
|
838 |
"cell_type": "code", |
|
|
839 |
"execution_count": null, |
|
|
840 |
"metadata": {}, |
|
|
841 |
"outputs": [], |
|
|
842 |
"source": [] |
|
|
843 |
} |
|
|
844 |
], |
|
|
845 |
"metadata": { |
|
|
846 |
"kernelspec": { |
|
|
847 |
"display_name": "DataScience", |
|
|
848 |
"language": "python", |
|
|
849 |
"name": "python3" |
|
|
850 |
}, |
|
|
851 |
"language_info": { |
|
|
852 |
"codemirror_mode": { |
|
|
853 |
"name": "ipython", |
|
|
854 |
"version": 3 |
|
|
855 |
}, |
|
|
856 |
"file_extension": ".py", |
|
|
857 |
"mimetype": "text/x-python", |
|
|
858 |
"name": "python", |
|
|
859 |
"nbconvert_exporter": "python", |
|
|
860 |
"pygments_lexer": "ipython3", |
|
|
861 |
"version": "3.10.13" |
|
|
862 |
} |
|
|
863 |
}, |
|
|
864 |
"nbformat": 4, |
|
|
865 |
"nbformat_minor": 2 |
|
|
866 |
} |