2
$\begingroup$

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. My dataset shape is 977,6 and 77:23 is class proportion

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

enter image description here

But one thing that I realized was that for all row indices my SHAP base value is 0.5. Do you know what does that mean? Is this waterfall plot specific or data specific? My feature contribution adds on top of 0.5. sometimes my feature contribution takes it to 0.80 or sometimes, it ends up at 0.36. The point is expected value is always 0.5. But why is that?

When I did explainer.model_output, I see that it gives raw as output. Does it mean it uses a scale of 0 to 1 and outputs 0.5 as random base value?

Please note that this question is not code related question. Instead, I would like to know why does SHAP base value is always 0.5?

update

I found the below text from SHAP documentation here

enter image description here

$\endgroup$

1 Answer 1

3
$\begingroup$

The baseline of Shapley values shown ($0.50$) is the average of all predictions. It is not a random base value. To quote from the original 2017 SHAP paper "A Unified Approach to Interpreting Model Predictions": "They (SHAP values) explain how to get from the base value $E[f(z)]$ that would be predicted if we did not know any features to the current output $f(x)$." This means this base-line value relates directly to the average prediction of our classifier, i.e. if we create a probabilistic prediction from our classifier $C$ for every item in our sample, and then we take the average of those predicted probabilities (i.e. $E[f(z)]$) that will be our (approximate) base value. In the example, you used that happens to be $0.50$.

Here is a quick example in Python:

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import numpy as np
import shap

iris = datasets.load_iris()
# Use only the first 80 points so we have "some imbalance"
data = iris.data[:80] 
label = iris.target[:80] 

train_x, test_x, train_y, test_y = train_test_split(data, label, random_state=0)

rfc = RandomForestClassifier(n_estimators=500)
rfc = rfc.fit(train_x, train_y)

pred_values = rfc.predict_proba(train_x)

explainer = shap.TreeExplainer(rfc) 
print(f"My base-line is: {explainer.expected_value[0]:.3f} " + 
      f"and my average prediction is: {np.mean(pred_values.T[0]):.3f}")
$\endgroup$
16
  • $\begingroup$ thanks for the help, upvoted. but why it is 0.5? Is it like tossing a coin example? where it has probability to go on either sides. So, we have 0.5? $\endgroup$
    – The Great
    Mar 31, 2022 at 9:15
  • $\begingroup$ I understand that it's a base value (if we don;t know any input features). but how is that 0.5 and why can't it be 0.2 or 0.3 for some records. How come all instances have 0.5 as base value? Is it because my model output is raw? $\endgroup$
    – The Great
    Mar 31, 2022 at 9:16
  • $\begingroup$ I made relevant changes. It "can't be 0.2 or 0.3" cause it isn't our average prediction from our classifier. $\endgroup$
    – usεr11852
    Mar 31, 2022 at 10:10
  • 1
    $\begingroup$ Appreciate your help and patience. $\endgroup$
    – The Great
    Mar 31, 2022 at 12:29
  • 1
    $\begingroup$ Okay, I split my data into train and test (using sklearn train test split). So, you suggest I should use SHAP for test dataset (30% of dataset) and explain the model to biz users? $\endgroup$
    – The Great
    Apr 1, 2022 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.