etweedy commited on
Commit
0a88cc2
1 Parent(s): 5d396e9

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +14 -7
  2. study.pkl +2 -2
app.py CHANGED
@@ -46,8 +46,7 @@ def load_study():
46
  Load the trained classifier pipeline
47
  """
48
  with open('study.pkl', 'rb') as file:
49
- study = pickle.load(file)
50
- return study
51
 
52
 
53
 
@@ -518,9 +517,14 @@ with model_result_container:
518
  sample.loc[0,feat]=1
519
 
520
  # Predict and report result
521
- study.predict_proba_pipeline(X_test=sample)
 
 
 
 
 
522
 
523
- st.write(f'**BikeSaferPA predicts a :red[{100*float(study.y_predict_proba):.2f}%] probability that a cyclist suffers serious injury or fatality under these conditions.**')
524
 
525
  ### SHAP values ####
526
 
@@ -536,17 +540,20 @@ The force plot will update as you adjust input features in the menu above.
536
  """)
537
 
538
  # SHAP will just explain classifier, so need transformed X_train and X_test
539
- pipe = study.pipe_fitted
540
- sample_trans = pipe[:-1].transform(sample)
541
 
542
  # # Need masker for linear model
543
  # masker = shap.maskers.Independent(data=X_train_trans)
544
 
545
  # Initialize explainer and compute and store SHAP values as an explainer object
 
 
 
 
 
 
546
  explainer = shap.TreeExplainer(pipe[-1], feature_names = pipe['col'].get_feature_names_out())
547
  shap_values = explainer(sample_trans)
548
  sample_trans = pd.DataFrame(sample_trans,columns=pipe['col'].get_feature_names_out())
549
-
550
  # def st_shap(plot, height=None):
551
  # shap_html = f"<head>{shap.getjs()}</head><body>{plot.html()}</body>"
552
  # components.html(shap_html, height=height)
 
46
  Load the trained classifier pipeline
47
  """
48
  with open('study.pkl', 'rb') as file:
49
+ return pickle.load(file)
 
50
 
51
 
52
 
 
517
  sample.loc[0,feat]=1
518
 
519
  # Predict and report result
520
+ # study.predict_proba_pipeline(X_test=sample)
521
+ feature_names = study.pipe_fitted[-2].get_feature_names_out()
522
+ pipe = study.pipe_fitted
523
+ sample_trans = pipe[:-1].transform(sample)
524
+ st.write(sample)
525
+ y_predict_proba = pipe.predict_proba(sample)[0,1]
526
 
527
+ st.write(f'**BikeSaferPA predicts a :red[{100*y_predict_proba:.2f}%] probability that a cyclist suffers serious injury or fatality under these conditions.**')
528
 
529
  ### SHAP values ####
530
 
 
540
  """)
541
 
542
  # SHAP will just explain classifier, so need transformed X_train and X_test
 
 
543
 
544
  # # Need masker for linear model
545
  # masker = shap.maskers.Independent(data=X_train_trans)
546
 
547
  # Initialize explainer and compute and store SHAP values as an explainer object
548
+ # shap_values_list = []
549
+ # for calibrated_classifier in clf.calibrated_classifiers_:
550
+ # explainer = shap.TreeExplainer(calibrated_classifier.estimator,feature_names = pipe['col'].get_feature_names_out())
551
+ # shap_values = explainer(sample_trans)
552
+ # shap_values_list.append(shap_values.values)
553
+ # shap_values = np.array(shap_values_list).sum(axis=0) / len(shap_values_list)
554
  explainer = shap.TreeExplainer(pipe[-1], feature_names = pipe['col'].get_feature_names_out())
555
  shap_values = explainer(sample_trans)
556
  sample_trans = pd.DataFrame(sample_trans,columns=pipe['col'].get_feature_names_out())
 
557
  # def st_shap(plot, height=None):
558
  # shap_html = f"<head>{shap.getjs()}</head><body>{plot.html()}</body>"
559
  # components.html(shap_html, height=height)
study.pkl CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:b1f563b55025766dc82eb2f00b0e4ae468caa7368f87dd309be9d8047eace6d9
3
- size 9935040
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:06ddfe748638a38e34509926709c5064800dba3c9c850c959921b19b0b01fd47
3
+ size 9938500