Spaces:
Runtime error
Runtime error
add feature importance & decision plots
Browse files- app.py +12 -18
- requirements.txt +2 -1
app.py
CHANGED
@@ -6,6 +6,7 @@ import matplotlib.pyplot as plt
|
|
6 |
import numpy as np
|
7 |
import shap
|
8 |
from sklearn.pipeline import make_pipeline
|
|
|
9 |
|
10 |
feature_names = ["Age", "BMI", "HbA1c", "Blood Glucose"]
|
11 |
|
@@ -67,9 +68,9 @@ with gr.Blocks() as demo:
|
|
67 |
summary_plot = gr.Plot()
|
68 |
with gr.Row():
|
69 |
with gr.Column():
|
70 |
-
|
71 |
with gr.Column():
|
72 |
-
|
73 |
|
74 |
def submit_inputs(
|
75 |
age_input,
|
@@ -171,25 +172,18 @@ with gr.Blocks() as demo:
|
|
171 |
)
|
172 |
|
173 |
fig4 = plt.figure(figsize=(3, 3))
|
174 |
-
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
|
177 |
explainer.expected_value[predicted_class],
|
178 |
shap_values_for_predicted_class,
|
179 |
df.iloc[0],
|
180 |
-
feature_names=["age", "bmi", "hba1c", "glucose"],
|
181 |
-
matplotlib=True,
|
182 |
)
|
183 |
|
184 |
-
# fig4 = plt.figure(figsize=(3, 3))
|
185 |
-
# plt.title("SHAP Interaction Plot")
|
186 |
-
# shap_interaction_values = explainer.shap_interaction_values(transformed_df)
|
187 |
-
# shap.summary_plot(
|
188 |
-
# shap_interaction_values,
|
189 |
-
# features=transformed_df,
|
190 |
-
# feature_names=["age", "bmi", "hba1c", "glucose"],
|
191 |
-
# )
|
192 |
-
|
193 |
## save user's data in hopsworks
|
194 |
if consent_input == True:
|
195 |
user_data_fg = fs.get_or_create_feature_group(
|
@@ -202,7 +196,7 @@ with gr.Blocks() as demo:
|
|
202 |
user_data_df["diabetes"] = existent_info_input
|
203 |
user_data_fg.insert(user_data_df)
|
204 |
print("inserted new user data to hopsworks", user_data_df)
|
205 |
-
return res, fig, fig2, fig3, fig4
|
206 |
|
207 |
btn.click(
|
208 |
submit_inputs,
|
@@ -214,7 +208,7 @@ with gr.Blocks() as demo:
|
|
214 |
existent_info_input,
|
215 |
consent_input,
|
216 |
],
|
217 |
-
outputs=[output, mean_plot, waterfall_plot,
|
218 |
)
|
219 |
|
220 |
demo.launch()
|
|
|
6 |
import numpy as np
|
7 |
import shap
|
8 |
from sklearn.pipeline import make_pipeline
|
9 |
+
import seaborn as sns
|
10 |
|
11 |
feature_names = ["Age", "BMI", "HbA1c", "Blood Glucose"]
|
12 |
|
|
|
68 |
summary_plot = gr.Plot()
|
69 |
with gr.Row():
|
70 |
with gr.Column():
|
71 |
+
importance_plot = gr.Plot()
|
72 |
with gr.Column():
|
73 |
+
decision_plot = gr.Plot()
|
74 |
|
75 |
def submit_inputs(
|
76 |
age_input,
|
|
|
172 |
)
|
173 |
|
174 |
fig4 = plt.figure(figsize=(3, 3))
|
175 |
+
feature_importances = rf_classifier.feature_importances_
|
176 |
+
plt.title("Feature Importances")
|
177 |
+
sns.barplot(x=feature_importances, y=["age", "bmi", "hba1c", "glucose"])
|
178 |
+
|
179 |
+
fig5 = plt.figure(figsize=(3, 3))
|
180 |
+
plt.title("SHAP Interaction Plot")
|
181 |
+
shap.decision_plot(
|
182 |
explainer.expected_value[predicted_class],
|
183 |
shap_values_for_predicted_class,
|
184 |
df.iloc[0],
|
|
|
|
|
185 |
)
|
186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
## save user's data in hopsworks
|
188 |
if consent_input == True:
|
189 |
user_data_fg = fs.get_or_create_feature_group(
|
|
|
196 |
user_data_df["diabetes"] = existent_info_input
|
197 |
user_data_fg.insert(user_data_df)
|
198 |
print("inserted new user data to hopsworks", user_data_df)
|
199 |
+
return res, fig, fig2, fig3, fig4, fig5
|
200 |
|
201 |
btn.click(
|
202 |
submit_inputs,
|
|
|
208 |
existent_info_input,
|
209 |
consent_input,
|
210 |
],
|
211 |
+
outputs=[output, mean_plot, waterfall_plot, importance_plot, decision_plot],
|
212 |
)
|
213 |
|
214 |
demo.launch()
|
requirements.txt
CHANGED
@@ -5,4 +5,5 @@ pandas
|
|
5 |
scikit-learn==1.1.1
|
6 |
matplotlib
|
7 |
numpy
|
8 |
-
shap
|
|
|
|
5 |
scikit-learn==1.1.1
|
6 |
matplotlib
|
7 |
numpy
|
8 |
+
shap
|
9 |
+
seaborn
|