mertkarabacak commited on
Commit
147cb13
1 Parent(s): 4d9129a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -53
app.py CHANGED
@@ -235,20 +235,24 @@ def y1_predict_rf(*args):
235
  def y2_predict_xgb(*args):
236
  df2 = pd.DataFrame([args], columns=x2.columns)
237
  df2 = df2.astype({col: "category" for col in categorical_columns2})
238
- pos_pred = y2_model_xgb.predict(xgb.DMatrix(df2, enable_categorical=True))
239
- return {"Facility Discharge": float(pos_pred[0]), "Home Discharge": 1 - float(pos_pred[0])}
 
 
240
 
241
  def y2_predict_lgb(*args):
242
- df2 = pd.DataFrame([args], columns=x2_lgb.columns)
243
  df2 = df2.astype({col: "category" for col in categorical_columns2})
244
- pos_pred = y2_model_lgb.predict(df2)
245
- return {"Facility Discharge": float(pos_pred[0]), "Home Discharge": 1 - float(pos_pred[0])}
 
 
246
 
247
  def y2_predict_cb(*args):
248
  df2 = pd.DataFrame([args], columns=x2.columns)
249
  df2 = df2.astype({col: "category" for col in categorical_columns2})
250
  pos_pred = y2_model_cb.predict(Pool(df2, cat_features = categorical_columns2), prediction_type='Probability')
251
- return {"Facility Discharge": float(pos_pred[0][1]), "Home Discharge": float(pos_pred[0][0])}
252
 
253
  def y2_predict_rf(*args):
254
  df2 = pd.DataFrame([args], columns=x2_rf.columns)
@@ -256,89 +260,100 @@ def y2_predict_rf(*args):
256
  d2 = dict.fromkeys(df2.select_dtypes(np.int64).columns, np.int32)
257
  df2 = df2.astype(d2)
258
  pos_pred = y2_model_rf.predict_proba(df2)
259
- return {"Facility Discharge": float(pos_pred[0][1]), "Home Discharge": float(pos_pred[0][0])}
260
 
261
  #Define predict for y3 (LOS).
262
  def y3_predict_xgb(*args):
263
  df3 = pd.DataFrame([args], columns=x3.columns)
264
  df3 = df3.astype({col: "category" for col in categorical_columns3})
265
- pos_pred = y3_model_xgb.predict(xgb.DMatrix(df3, enable_categorical=True))
266
- return {"Prolonged LOS": float(pos_pred[0]), "No Prolonged LOS": 1 - float(pos_pred[0])}
 
 
267
 
268
  def y3_predict_lgb(*args):
269
- df3 = pd.DataFrame([args], columns=x3_lgb.columns)
270
  df3 = df3.astype({col: "category" for col in categorical_columns3})
271
- pos_pred = y3_model_lgb.predict(df3)
272
- return {"Prolonged LOS": float(pos_pred[0]), "No Prolonged LOS": 1 - float(pos_pred[0])}
 
 
273
 
274
  def y3_predict_cb(*args):
275
  df3 = pd.DataFrame([args], columns=x3.columns)
276
  df3 = df3.astype({col: "category" for col in categorical_columns3})
277
  pos_pred = y3_model_cb.predict(Pool(df3, cat_features = categorical_columns3), prediction_type='Probability')
278
- return {"Prolonged LOS": float(pos_pred[0][1]), "No Prolonged LOS": float(pos_pred[0][0])}
279
 
280
  def y3_predict_rf(*args):
281
  df3 = pd.DataFrame([args], columns=x3_rf.columns)
282
  df3 = df3.astype({col: "category" for col in categorical_columns3})
283
- d3 = dict.fromkeys(df3.select_dtypes(np.int64).columns, np.int32)
284
- df3 = df.astype(d3)
285
  pos_pred = y3_model_rf.predict_proba(df3)
286
- return {"Prolonged LOS": float(pos_pred[0][1]), "No Prolonged LOS": float(pos_pred[0][0])}
287
 
288
  #Define predict for y4 (ICU LOS).
289
  def y4_predict_xgb(*args):
290
  df4 = pd.DataFrame([args], columns=x4.columns)
291
  df4 = df4.astype({col: "category" for col in categorical_columns4})
292
- pos_pred = y4_model_xgb.predict(xgb.DMatrix(df4, enable_categorical=True))
293
- return {"Prolonged ICU LOS": float(pos_pred[0]), "No Prolonged ICU LOS": 1 - float(pos_pred[0])}
 
 
294
 
295
  def y4_predict_lgb(*args):
296
- df4 = pd.DataFrame([args], columns=x4_lgb.columns)
297
  df4 = df4.astype({col: "category" for col in categorical_columns4})
298
- pos_pred = y4_model_lgb.predict(df4)
299
- return {"Prolonged ICU LOS": float(pos_pred[0]), "No Prolonged ICU LOS": 1 - float(pos_pred[0])}
 
 
300
 
301
  def y4_predict_cb(*args):
302
- df4 = df4.DataFrame([args], columns=x4.columns)
303
  df4 = df4.astype({col: "category" for col in categorical_columns4})
304
  pos_pred = y4_model_cb.predict(Pool(df4, cat_features = categorical_columns4), prediction_type='Probability')
305
- return {"Prolonged ICU LOS": float(pos_pred[0][1]), "No Prolonged ICU LOS": float(pos_pred[0][0])}
306
 
307
  def y4_predict_rf(*args):
308
  df4 = pd.DataFrame([args], columns=x4_rf.columns)
309
  df4 = df4.astype({col: "category" for col in categorical_columns4})
310
- d4 = dict.fromkeys(df4.select_dtypes(np.int64).columns, np.int32)
311
  df4 = df4.astype(d4)
312
  pos_pred = y4_model_rf.predict_proba(df4)
313
- return {"Prolonged ICU LOS": float(pos_pred[0][1]), "No Prolonged ICU LOS": float(pos_pred[0][0])}
314
 
315
  #Define predict for y5 (complications).
316
- def y5_predict_xgb(*args):
317
- df5 = pd.DataFrame([args], columns=x5.columns)
318
- df5 = df5.astype({col: "category" for col in categorical_columns5})
319
- pos_pred = y5_model_xgb.predict(xgb.DMatrix(df5, enable_categorical=True))
320
- return {"Major Complications": float(pos_pred[0]), "No Major Complications": 1 - float(pos_pred[0])}
321
-
322
- def y5_predict_lgb(*args):
323
- df5 = pd.DataFrame([args], columns=x5_lgb.columns)
324
- df5 = df5.astype({col: "category" for col in categorical_columns5})
325
- pos_pred = y5_model_lgb.predict(df5)
326
- return {"Major Complications": float(pos_pred[0]), "No Major Complications": 1 - float(pos_pred[0])}
327
 
328
- def y5_predict_cb(*args):
329
- df5 = pd.DataFrame([args], columns=x5.columns)
330
- df5 = df5.astype({col: "category" for col in categorical_columns5})
331
- pos_pred = y5_model_cb.predict(Pool(df5, cat_features = categorical_columns5), prediction_type='Probability')
332
- return {"Major Complications": float(pos_pred[0][1]), "No Major Complications": float(pos_pred[0][0])}
 
 
333
 
334
- def y5_predict_rf(*args):
335
- df5 = pd.DataFrame([args], columns=x5_rf.columns)
336
- df5 = df.astype({col: "category" for col in categorical_columns5})
337
- d5 = dict.fromkeys(df5.select_dtypes(np.int64).columns, np.int32)
338
- df5 = df5.astype(d5)
339
- pos_pred = y5_model_rf.predict_proba(df5)
340
- return {"Major Complications": float(pos_pred[0][1]), "No Major Complications": float(pos_pred[0][0])}
341
 
 
 
 
 
 
 
 
342
 
343
  #Define function for wrapping feature labels.
344
  def wrap_labels(ax, width, break_long_words=False):
@@ -406,7 +421,7 @@ def y1_interpret_cb(*args):
406
  return fig
407
 
408
  def y1_interpret_rf(*args):
409
- df1 = pd.DataFrame([args], columns=x1_rf.columns)
410
  df1 = df1.astype({col: "category" for col in categorical_columns1})
411
  shap_values1 = y1_explainer_rf.shap_values(df1)
412
  shap_values1 = np.abs(shap_values1)
@@ -479,7 +494,7 @@ def y2_interpret_cb(*args):
479
  return fig
480
 
481
  def y2_interpret_rf(*args):
482
- df2 = pd.DataFrame([args], columns=x2_rf.columns)
483
  df2 = df2.astype({col: "category" for col in categorical_columns2})
484
  shap_values2 = y2_explainer_rf.shap_values(df2)
485
  shap_values2 = np.abs(shap_values2)
@@ -552,7 +567,7 @@ def y3_interpret_cb(*args):
552
  return fig
553
 
554
  def y3_interpret_rf(*args):
555
- df3 = pd.DataFrame([args], columns=x3_rf.columns)
556
  df3 = df3.astype({col: "category" for col in categorical_columns3})
557
  shap_values3 = y3_explainer_rf.shap_values(df3)
558
  shap_values3 = np.abs(shap_values3)
@@ -627,7 +642,7 @@ def y4_interpret_cb(*args):
627
  def y4_interpret_rf(*args):
628
  df4 = pd.DataFrame([args], columns=x4_rf.columns)
629
  df4 = df4.astype({col: "category" for col in categorical_columns4})
630
- shap_values4 = y4_explainer_rf.shap_values(df4)
631
  shap_values4 = np.abs(shap_values4)
632
  shap.bar_plot(shap_values4[0][0], max_display = 10, show = False, feature_names = f4_names)
633
  fig = plt.gcf()
@@ -698,7 +713,7 @@ def y5_interpret_cb(*args):
698
  return fig
699
 
700
  def y5_interpret_rf(*args):
701
- df5 = pd.DataFrame([args], columns=x5_rf.columns)
702
  df5 = df5.astype({col: "category" for col in categorical_columns5})
703
  shap_values = y5_explainer_rf.shap_values(df5)
704
  shap_values1 = np.abs(shap_values5)
 
235
  def y2_predict_xgb(*args):
236
  df2 = pd.DataFrame([args], columns=x2.columns)
237
  df2 = df2.astype({col: "category" for col in categorical_columns2})
238
+ d2 = dict.fromkeys(df2.select_dtypes(np.int64).columns, np.int32)
239
+ df2 = df2.astype(d2)
240
+ pos_pred = y2_model_xgb.predict_proba(df2)
241
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
242
 
243
  def y2_predict_lgb(*args):
244
+ df2 = pd.DataFrame([args], columns=x2.columns)
245
  df2 = df2.astype({col: "category" for col in categorical_columns2})
246
+ d2 = dict.fromkeys(df2.select_dtypes(np.int64).columns, np.int32)
247
+ df2 = df2.astype(d2)
248
+ pos_pred = y2_model_lgb.predict_proba(df2)
249
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
250
 
251
  def y2_predict_cb(*args):
252
  df2 = pd.DataFrame([args], columns=x2.columns)
253
  df2 = df2.astype({col: "category" for col in categorical_columns2})
254
  pos_pred = y2_model_cb.predict(Pool(df2, cat_features = categorical_columns2), prediction_type='Probability')
255
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
256
 
257
  def y2_predict_rf(*args):
258
  df2 = pd.DataFrame([args], columns=x2_rf.columns)
 
260
  d2 = dict.fromkeys(df2.select_dtypes(np.int64).columns, np.int32)
261
  df2 = df2.astype(d2)
262
  pos_pred = y2_model_rf.predict_proba(df2)
263
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
264
 
265
  #Define predict for y3 (LOS).
266
  def y3_predict_xgb(*args):
267
  df3 = pd.DataFrame([args], columns=x3.columns)
268
  df3 = df3.astype({col: "category" for col in categorical_columns3})
269
+ d3 = dict.fromkeys(df3.select_dtypes(np.int64).columns, np.int33)
270
+ df3 = df3.astype(d3)
271
+ pos_pred = y3_model_xgb.predict_proba(df3)
272
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
273
 
274
  def y3_predict_lgb(*args):
275
+ df3 = pd.DataFrame([args], columns=x3.columns)
276
  df3 = df3.astype({col: "category" for col in categorical_columns3})
277
+ d3 = dict.fromkeys(df3.select_dtypes(np.int64).columns, np.int33)
278
+ df3 = df3.astype(d3)
279
+ pos_pred = y3_model_lgb.predict_proba(df3)
280
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
281
 
282
  def y3_predict_cb(*args):
283
  df3 = pd.DataFrame([args], columns=x3.columns)
284
  df3 = df3.astype({col: "category" for col in categorical_columns3})
285
  pos_pred = y3_model_cb.predict(Pool(df3, cat_features = categorical_columns3), prediction_type='Probability')
286
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
287
 
288
  def y3_predict_rf(*args):
289
  df3 = pd.DataFrame([args], columns=x3_rf.columns)
290
  df3 = df3.astype({col: "category" for col in categorical_columns3})
291
+ d3 = dict.fromkeys(df3.select_dtypes(np.int64).columns, np.int33)
292
+ df3 = df3.astype(d3)
293
  pos_pred = y3_model_rf.predict_proba(df3)
294
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
295
 
296
  #Define predict for y4 (ICU LOS).
297
  def y4_predict_xgb(*args):
298
  df4 = pd.DataFrame([args], columns=x4.columns)
299
  df4 = df4.astype({col: "category" for col in categorical_columns4})
300
+ d4 = dict.fromkeys(df4.select_dtypes(np.int64).columns, np.int34)
301
+ df4 = df4.astype(d4)
302
+ pos_pred = y4_model_xgb.predict_proba(df4)
303
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
304
 
305
  def y4_predict_lgb(*args):
306
+ df4 = pd.DataFrame([args], columns=x4.columns)
307
  df4 = df4.astype({col: "category" for col in categorical_columns4})
308
+ d4 = dict.fromkeys(df4.select_dtypes(np.int64).columns, np.int34)
309
+ df4 = df4.astype(d4)
310
+ pos_pred = y4_model_lgb.predict_proba(df4)
311
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
312
 
313
  def y4_predict_cb(*args):
314
+ df4 = pd.DataFrame([args], columns=x4.columns)
315
  df4 = df4.astype({col: "category" for col in categorical_columns4})
316
  pos_pred = y4_model_cb.predict(Pool(df4, cat_features = categorical_columns4), prediction_type='Probability')
317
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
318
 
319
  def y4_predict_rf(*args):
320
  df4 = pd.DataFrame([args], columns=x4_rf.columns)
321
  df4 = df4.astype({col: "category" for col in categorical_columns4})
322
+ d4 = dict.fromkeys(df4.select_dtypes(np.int64).columns, np.int34)
323
  df4 = df4.astype(d4)
324
  pos_pred = y4_model_rf.predict_proba(df4)
325
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
326
 
327
  #Define predict for y5 (complications).
328
+ def y4_predict_xgb(*args):
329
+ df4 = pd.DataFrame([args], columns=x4.columns)
330
+ df4 = df4.astype({col: "category" for col in categorical_columns4})
331
+ d4 = dict.fromkeys(df4.select_dtypes(np.int64).columns, np.int34)
332
+ df4 = df4.astype(d4)
333
+ pos_pred = y4_model_xgb.predict_proba(df4)
334
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
 
 
 
 
335
 
336
+ def y4_predict_lgb(*args):
337
+ df4 = pd.DataFrame([args], columns=x4.columns)
338
+ df4 = df4.astype({col: "category" for col in categorical_columns4})
339
+ d4 = dict.fromkeys(df4.select_dtypes(np.int64).columns, np.int34)
340
+ df4 = df4.astype(d4)
341
+ pos_pred = y4_model_lgb.predict_proba(df4)
342
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
343
 
344
+ def y4_predict_cb(*args):
345
+ df4 = pd.DataFrame([args], columns=x4.columns)
346
+ df4 = df4.astype({col: "category" for col in categorical_columns4})
347
+ pos_pred = y4_model_cb.predict(Pool(df4, cat_features = categorical_columns4), prediction_type='Probability')
348
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
 
 
349
 
350
+ def y4_predict_rf(*args):
351
+ df4 = pd.DataFrame([args], columns=x4_rf.columns)
352
+ df4 = df4.astype({col: "category" for col in categorical_columns4})
353
+ d4 = dict.fromkeys(df4.select_dtypes(np.int64).columns, np.int34)
354
+ df4 = df4.astype(d4)
355
+ pos_pred = y4_model_rf.predict_proba(df4)
356
+ return {"Mortality": float(pos_pred[0][1]), "No Mortality": float(pos_pred[0][0])}
357
 
358
  #Define function for wrapping feature labels.
359
  def wrap_labels(ax, width, break_long_words=False):
 
421
  return fig
422
 
423
  def y1_interpret_rf(*args):
424
+ df1 = pd.DataFrame([args], columns=x1.columns)
425
  df1 = df1.astype({col: "category" for col in categorical_columns1})
426
  shap_values1 = y1_explainer_rf.shap_values(df1)
427
  shap_values1 = np.abs(shap_values1)
 
494
  return fig
495
 
496
  def y2_interpret_rf(*args):
497
+ df2 = pd.DataFrame([args], columns=x2.columns)
498
  df2 = df2.astype({col: "category" for col in categorical_columns2})
499
  shap_values2 = y2_explainer_rf.shap_values(df2)
500
  shap_values2 = np.abs(shap_values2)
 
567
  return fig
568
 
569
  def y3_interpret_rf(*args):
570
+ df3 = pd.DataFrame([args], columns=x3.columns)
571
  df3 = df3.astype({col: "category" for col in categorical_columns3})
572
  shap_values3 = y3_explainer_rf.shap_values(df3)
573
  shap_values3 = np.abs(shap_values3)
 
642
  def y4_interpret_rf(*args):
643
  df4 = pd.DataFrame([args], columns=x4_rf.columns)
644
  df4 = df4.astype({col: "category" for col in categorical_columns4})
645
+ shap_values4 = y4_explainer.shap_values(df4)
646
  shap_values4 = np.abs(shap_values4)
647
  shap.bar_plot(shap_values4[0][0], max_display = 10, show = False, feature_names = f4_names)
648
  fig = plt.gcf()
 
713
  return fig
714
 
715
  def y5_interpret_rf(*args):
716
+ df5 = pd.DataFrame([args], columns=x5.columns)
717
  df5 = df5.astype({col: "category" for col in categorical_columns5})
718
  shap_values = y5_explainer_rf.shap_values(df5)
719
  shap_values1 = np.abs(shap_values5)