Note
Go to the end to download the full example code.
SHAP-IQ with scikit-learn¶
This example shows how to compute second-order Shapley Interaction Index (SII) values for a scikit-learn Random Forest on the California housing dataset.
from __future__ import annotations
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
import shapiq
Load Data and Train Model¶
X, y = shapiq.load_california_housing()
X_train, X_test, y_train, y_test = train_test_split(
X.values,
y.values,
test_size=0.25,
random_state=42,
)
n_features = X_train.shape[1]
model = RandomForestRegressor(
n_estimators=100,
max_depth=n_features,
max_features=2 / 3,
max_samples=2 / 3,
random_state=42,
)
model.fit(X_train, y_train)
print(f"Train R2: {model.score(X_train, y_train):.4f}")
print(f"Test R2: {model.score(X_test, y_test):.4f}")
Train R2: 0.7965
Test R2: 0.7431
Compute Second-Order SII¶
TabularExplainer with index="SII" and max_order=2
computes pairwise Shapley interaction values.
InteractionValues(
index=SII, max_order=2, min_order=0, estimated=False, estimation_budget=256,
n_players=8, baseline_value=2.0701874006108745,
Top 10 interactions:
(6,): 0.1478250584673519
(1, 5): 0.10379041669472935
(5, 6): -0.033596353836664046
(6, 7): -0.04428551064696254
(0, 1): -0.04664913243984276
(0, 6): -0.05216939691248073
(1,): -0.080623853044805
(0, 5): -0.08271511010438869
(5,): -0.14868378081300276
(7,): -0.25600704535637764
)
Second-Order Interaction Matrix¶
print(iv.get_n_order(2).dict_values)
{(0, 1): -0.04664913243984276, (0, 2): 0.014949695777978644, (0, 3): -0.02571741839426297, (0, 4): -0.021236780262631667, (0, 5): -0.08271511010438869, (0, 6): -0.05216939691248073, (0, 7): 0.006477298635771582, (1, 2): -0.013604570158179074, (1, 3): -0.01919360818513834, (1, 4): -0.018151921875847035, (1, 5): 0.10379041669472935, (1, 6): -0.021629201167859338, (1, 7): -0.025722170690545436, (2, 3): -0.02003480667692296, (2, 4): -0.020121479554283485, (2, 5): -0.02093460938059754, (2, 6): -0.01757370978961496, (2, 7): -0.025719162079832946, (3, 4): -0.020781921324627674, (3, 5): -0.015707986140680332, (3, 6): -0.024584798934048795, (3, 7): -0.02243863552665066, (4, 5): -0.024188304709477828, (4, 6): -0.021910780145134996, (4, 7): -0.019706332513963795, (5, 6): -0.033596353836664046, (5, 7): -0.006788346740215291, (6, 7): -0.04428551064696254}
Visualization: Network Plot¶
shapiq.network_plot(interaction_values=iv, feature_names=list(X.columns))

(<Figure size 700x700 with 1 Axes>, <Axes: >)
Stacked Bar Plot (First Order)¶
shapiq.stacked_bar_plot(iv.get_n_order(1), feature_names=list(X.columns))

(<Figure size 640x480 with 1 Axes>, <Axes: xlabel='features', ylabel='SI values'>)
Stacked Bar Plot (All Orders)¶
shapiq.stacked_bar_plot(interaction_values=iv, feature_names=list(X.columns))

(<Figure size 640x480 with 1 Axes>, <Axes: xlabel='features', ylabel='SI values'>)
Force Plot¶
iv.plot_force(feature_names=list(X.columns))

Total running time of the script: (0 minutes 4.309 seconds)