[Python] export SHAP waterfall plot to dataframe

Discussão em 'Python' iniciado por Stack, Maio 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a binary classification using random forest model, neural networks in which am using SHAP to explain the model predictions. I followed the tutorial and wrote the below code to get the waterfall plot shown below

    row_to_show = 20
    data_for_prediction = ord_test_t.iloc[row_to_show] # use 1 row of data here. Could use multiple rows if desired
    data_for_prediction_array = data_for_prediction.values.reshape(1, -1)
    rf_boruta.predict_proba(data_for_prediction_array)
    explainer = shap.TreeExplainer(rf_boruta)
    # Calculate Shap values
    shap_values = explainer.shap_values(data_for_prediction)
    shap.plots._waterfall.waterfall_legacy(explainer.expected_value[0], shap_values[0],ord_test_t.iloc[row_to_show])


    This generated the plot as shown below

    [​IMG]

    However, I want to export this to dataframe and how can I do it?

    I expect my output to be like as shown below. I want to export this for the full dataframe. Can you help me please?

    [​IMG]

    Continue reading...

Compartilhe esta Página