How to use Shap-IQ packages to reveal and visualize feature interactions in machine learning models using Shapley Interaction Indices (SII)

Machine Learning


This tutorial explains how to use the Shap-IQ package to reveal and visualize the distinctive interactions of machine learning models using Shapley Interaction Indices (SII), built on top of the foundations of traditional Shapley values.

The Shapley value is best used to explain the contribution of individual features in the AI model, but it cannot capture feature interactions. Shapley's interactions take it a step further by separating individual effects from interactions and providing deeper insights. In this tutorial, we start with the SHAPIQ package and calculate and investigate these Shapley interactions for any model. Please check Full code is here

Dependencies Installation

!pip install shapiq overrides scikit-learn pandas numpy

Loading and preprocessing data

This tutorial uses OpenML's bike sharing dataset. After loading the data, split the data into training and test sets to prepare for model training and evaluation. Please check Full code is here

import shapiq
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
import numpy as np

# Load data
X, y = shapiq.load_bike_sharing(to_numpy=True)

# Split into training and testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Model Training and Performance Evaluation

# Train model
model = RandomForestRegressor()
model.fit(X_train, y_train)

# Predict
y_pred = model.predict(X_test)

# Evaluate
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2 = r2_score(y_test, y_pred)

print(f"R² Score: {r2:.4f}")
print(f"Mean Absolute Error: {mae:.4f}")
print(f"Root Mean Squared Error: {rmse:.4f}")

Explainer setup

Use the shapiq package to set tabularexplainer and calculate Shapley interaction values based on the K-SII (K-order Shapley Interaction Index) method. By specifying max_order = 4, the explainer can simultaneously consider the interaction of up to four features, allowing for deeper insight into how groups of features affect model predictions. Please check Full code is here

# set up an explainer with k-SII interaction values up to order 4
explainer = shapiq.TabularExplainer(
    model=model,
    data=X,
    index="k-SII",
    max_order=4
)

Local Instance Description

Select a specific test instance (index 100) to generate a local description. The code prints the true and predicted values for this instance, followed by a decomposition of the feature values. This helps you understand the exact input passed to the model and set the context to interpret the Shapley interaction description below. Please check Full code is here

from tqdm.asyncio import tqdm
# create explanations for different orders
feature_names = list(df[0].columns)  # get the feature names
n_features = len(feature_names)

# select a local instance to be explained
instance_id = 100
x_explain = X_test[instance_id]
y_true = y_test[instance_id]
y_pred = model.predict(x_explain.reshape(1, -1))[0]
print(f"Instance {instance_id}, True Value: {y_true}, Predicted Value: {y_pred}")
for i, feature in enumerate(feature_names):
    print(f"{feature}: {x_explain[i]}")

Interaction value analysis

Use the extracter.explain() method to calculate the Shapley interaction value for a particular data instance (x[100]) 256 model evaluation budget. This returns an InteractionValues object that captures how individual features and their combinations affect the output of the model. max_order = 4 means considering interactions that include up to four functions. Please check Full code is here

interaction_values = explainer.explain(X[100], budget=256)
# analyse interaction values
print(interaction_values)

Primary interaction values

To keep things simple, calculate standard Shapley values (IE, standard Shapley values) that capture only the contributions of individual features (no interaction).

By setting max_order = 1 in treeexplainer, it states:

“Please tell us how much each feature contributes to prediction without considering the interaction effects.”

These values are known as standard Shapley values. For each feature, we estimate the average limiting contribution to predictions across all possible permutations of feature inclusion. Please check Full code is here

feature_names = list(df[0].columns)
explainer = shapiq.TreeExplainer(model=model, max_order=1, index="SV")
si_order = explainer.explain(x=x_explain)
si_order

Waterfall chart plot

Waterfall charts visually decompose the model's predictions into the contributions of individual features. Starting with a baseline prediction, add/subtract the Shapley value for each feature to arrive at the final predicted output.

In this case, we use the treeexplainer output with max_order = 1 (i.e., individual contributions only) to visualize the contribution of each feature. Please check Full code is here

si_order.plot_waterfall(feature_names=feature_names, show=True)

In this case, the baseline value (i.e., the expected output of the model without feature information) is 190.717.

Adding contributions from individual features (Order-1 Shapley values) allows us to observe how each one pushes or pulls down the forecast.

  • Features such as weather and humidity have a positive contribution, increasing forecasts above the baseline.
  • Features such as temperature and year will lower their forecasts to -35.4 and -45 respectively.

Overall, waterfall charts help you understand which features are driving prediction and which direction it is. This provides valuable insight into model decision-making.


Please check Full code is here. Please feel free to check GitHub pages for tutorials, code and notebooks. Also, please feel free to follow us Twitter And don't forget to join us 100k+ ml subreddit And subscribe Our Newsletter.


I am a civil engineering graduate (2022) from Jamia Milia Islamia, New Delhi, and have a strong interest in data science, particularly neural networks and applications in a variety of fields.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *