Search code examples
pythonh2ogbmshap

Negative SHAP values in H2O in Python using predict_contributions


I have been trying to compute SHAP values for a Gradient Boosting Classifier in H2O module in Python. Below there is the adapted example in the documentation for the predict_contibutions method (adapted from https://github.com/h2oai/h2o-3/blob/master/h2o-py/demos/predict_contributionsShap.ipynb).

import h2o
import shap
from h2o.estimators.gbm import H2OGradientBoostingEstimator
from h2o import H2OFrame

# initialize H2O
h2o.init()

# load JS visualization code to notebook
shap.initjs()

# Import the prostate dataset
h2o_df = h2o.import_file("https://raw.github.com/h2oai/h2o/master/smalldata/logreg/prostate.csv")

# Split the data into Train/Test/Validation with Train having 70% and test and validation 15% each
train,test,valid = h2o_df.split_frame(ratios=[.7, .15])

# Convert the response column to a factor
h2o_df["CAPSULE"] = h2o_df["CAPSULE"].asfactor()

# Generate a GBM model using the training dataset
model = H2OGradientBoostingEstimator(distribution="bernoulli",
                                     ntrees=100,
                                     max_depth=4,
                                     learn_rate=0.1)

model.train(y="CAPSULE", x=["AGE","RACE","PSA","GLEASON"],training_frame=h2o_df)

# calculate SHAP values using function predict_contributions
contributions = model.predict_contributions(h2o_df)

# convert the H2O Frame to use with shap's visualization functions
contributions_matrix = contributions.as_data_frame().to_numpy() # the original method is as_matrix()

# shap values are calculated for all features
shap_values = contributions_matrix[:,0:4]

# expected values is the last returned column
expected_value = contributions_matrix[:,4].min()

# force plot for one observation
X=["AGE","RACE","PSA","GLEASON"]
shap.force_plot(expected_value, shap_values[0,:], X)

The image I get from the code above is: force plot for one observation

What does the output means? Considering the problem above is a classification problem, the predicted value should be a probability (or even the category predicted - 0 or 1), right? Both the base value and the predicted value are negative.

Can anyone help me with this?


Solution

  • What you got is most likely log-odds and not a probability itself. In order to get a probability, you need to transform each log-odds to the probability space, i.e.

    p=e^x/(1 + e^x)
    

    when you use SHAP directly you can achieve this by specifying model_output parameter:

    shap.TreeExplainer(model, data, model_output='probability')