Get startedGet started for free

SHAP vs. model-specific approaches

You will compare the explanatory power of SHAP values from a Kernel Explainer with the logistic regression coefficients, both trained on the income dataset. A helper function plot_importances() is called at the end of the script to plot importances on the same plot.

X containing the features and y containing the labels, and the logistic regression model have been pre-loaded for you. matplotlib.pyplot has been imported as plt.

This exercise is part of the course

Explainable AI in Python

View Course

Exercise instructions

  • Compute the coefficients of the logistic regression model.
  • Create Kernel Explainer to compute shap_values using the logistic regression model and a k-means summary of 10 samples from X.
  • Compute the mean absolute SHAP values to estimate the impact of each feature.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

import shap

# Extract model coefficients
coefficients = ____

# Compute SHAP values
explainer = ____
shap_values = explainer.shap_values(X)

# Calculate mean absolute SHAP values
mean_abs_shap = ____

plot_importances(coefficients, mean_abs_shap)
Edit and Run Code