DEV Community

shreya
shreya

Posted on

SHAP

SHAP: Understanding Machine Learning Predictions Using Game Theory

Introduction

Machine Learning models can make predictions with impressive accuracy, but understanding how they arrive at those predictions is equally important.

For example, a bank may use a machine learning model to decide whether a customer is eligible for a loan. The prediction alone may not be enough. The bank may also need to understand which factors influenced the decision.

SHAP (SHapley Additive exPlanations) is an Explainable AI technique that helps us understand the contribution of individual features to a model's prediction.

What is SHAP?

SHAP stands for SHapley Additive exPlanations.

It is based on Shapley values from cooperative game theory.

In game theory, Shapley values are used to fairly distribute the total reward among participants in a team.

In machine learning, SHAP applies this idea to features:

  • The prediction is treated like the final outcome.
  • Features are treated like contributors.
  • Each feature receives a value representing its contribution to the prediction.

Why Do We Need SHAP?

Imagine a model predicting house prices.

The input features include:

  • Location
  • Number of bedrooms
  • House size
  • Age of the property
  • Nearby facilities

The model predicts a house price of ₹80 lakh.

SHAP helps explain how each feature contributed to moving the prediction away from the model's average prediction.

How Does SHAP Work?

SHAP calculates feature contributions by considering how a feature changes the prediction across different combinations of other features.

The main steps are:

  1. Start with a model prediction and a baseline value.
  2. Consider different combinations of input features.
  3. Measure how adding a feature changes the prediction.
  4. Average that feature's marginal contribution across the combinations.
  5. Assign a SHAP value to each feature.

A positive SHAP value pushes the prediction higher, while a negative SHAP value pushes it lower, relative to the chosen baseline and output scale.

Understanding SHAP Values

Consider this illustrative example:

Feature SHAP value
House size +₹12 lakh
Location +₹8 lakh
Number of bedrooms +₹4 lakh
Property age -₹3 lakh

The positive values increase the prediction, while the negative value decreases it.

The model's output can be represented as:

Prediction = Baseline + Sum of SHAP values

The exact output scale depends on the model and SHAP explainer being used.

Python Implementation

Install SHAP:

pip install shap scikit-learn matplotlib
Enter fullscreen mode Exit fullscreen mode

Example using a Random Forest model:

import shap
import matplotlib.pyplot as plt

from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

# Load dataset
data = load_breast_cancer()

X_train, X_test, y_train, y_test = train_test_split(
    data.data,
    data.target,
    test_size=0.2,
    random_state=42
)

# Train model
model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)

# Create SHAP explainer
explainer = shap.TreeExplainer(model)

# Calculate SHAP values
shap_values = explainer(X_test)

# Global feature importance
shap.plots.bar(shap_values)

# Explain one prediction
shap.plots.waterfall(shap_values[0, :, 1])

plt.show()
Enter fullscreen mode Exit fullscreen mode

This example uses a tree-based model. SHAP explainers and output shapes can differ depending on the model and SHAP version.

Important SHAP Visualizations

1. Summary Plot

Shows how features influence predictions across multiple observations.

2. Bar Plot

Displays overall feature importance based on average absolute SHAP values.

3. Waterfall Plot

Explains how individual features move a particular prediction away from the baseline.

4. Dependence Plot

Helps visualize the relationship between a feature's value and its SHAP contribution.

Advantages of SHAP

  • Provides explanations for individual predictions.
  • Helps understand overall feature importance.
  • Has a strong theoretical foundation in game theory.
  • Supports several model types.
  • Provides useful visualizations for model interpretation.

Limitations of SHAP

  • Exact SHAP calculations can be computationally expensive.
  • Different explainers use different approximation methods.
  • Results can be affected by assumptions about feature dependence.
  • SHAP values explain model behavior, not necessarily real-world causation.

LIME vs SHAP

LIME SHAP
Uses a local surrogate model Uses Shapley-value-based feature attribution
Explains individual predictions Supports local and global analysis
Uses perturbations and local weighting Uses feature contributions across feature coalitions
Can vary with sampling Has a game-theoretic foundation

Conclusion

SHAP is a powerful Explainable AI technique that helps us understand how individual features contribute to machine learning predictions.

It is especially useful when working with complex models where prediction accuracy alone is not enough.

In simple words: SHAP tells us how much each feature contributes to a model's prediction.

References

Top comments (0)