Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly Dash: How to integrate SHAP values

I am trying to make a model explainer page using Plotly Dash with information from shap values. But I don't know how to get the output from Shap

Here is the code:

#load data & model
cat_model = pickle.load(open('cat-model.saved', 'rb'))

PREPROCESS_DIR = "preprocess-data"
X_test = pickle.load(open(PREPROCESS_DIR+'/X_test_60k.saved', 'rb'))
y_test = pickle.load(open(PREPROCESS_DIR+'/y_test_60k.saved', 'rb'))

explainer = shap.TreeExplainer(cat_model)
shap_values = explainer.shap_values(X_test)


# visualize the first prediction's explanation

shap_html = shap.force_plot(explainer.expected_value, shap_values[:1000,:], X_test.iloc[:1000,:])
    
    
# TODO: Build and run dash app
app = dash.Dash(__name__)
app.config.suppress_callback_exceptions = True

app.layout = html.Div([
    html.H1('Model Explainer', style={'textAlign':'center','color':'#503D36','fontSize':'30px'}),

    html.Div([shap_html], id='output-shap')
])


if __name__ == '__main__':
    app.run_server(debug=True)
like image 672
Thuận Đoàn Vũ Avatar asked May 07 '26 17:05

Thuận Đoàn Vũ


1 Answers

Use explainerdashboard library. It allows you to investigate SHAP values, permutation importances, interaction effects, partial dependence plots, all kinds of performance plots, and even individual decision trees inside a random forest.

https://explainerdashboard.readthedocs.io/en/latest/

like image 56
Daniel Avatar answered May 10 '26 07:05

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!