Note
Go to the end to download the full example code.
SPEX¶
Sparse Explainer via Fourier transforms using
SPEX Kang et al.[1].
from __future__ import annotations
import numpy as np
import shapiq
N_PLAYERS = 8
BUDGET = 200
feature_names = [f"x{i}" for i in range(N_PLAYERS)]
weights = np.array([0.4, 0.3, 0.2, 0.1, 0.05, -0.1, -0.2, -0.3])
def game_fun(coalitions: np.ndarray) -> np.ndarray:
coalitions = np.atleast_2d(coalitions)
return (coalitions @ weights) + 0.5 * coalitions[:, 0] * coalitions[:, 1]
Approximate interaction values¶
approximator = shapiq.SPEX(n=N_PLAYERS, max_order=2, random_state=42)
iv = approximator.approximate(500, game_fun) # SPEX requires a larger budget
print(iv)
InteractionValues(
index=k-SII, max_order=2, min_order=0, estimated=True, estimation_budget=384,
n_players=8, baseline_value=-6.938893903907228e-18,
Top 10 interactions:
(np.int64(0), np.int64(1)): 0.5
(np.int64(2),): 0.20000000000000004
(np.int64(0),): 0.15000000000000002
(np.int64(3),): 0.1
(np.int64(1),): 0.050000000000000044
(np.int64(4),): 0.049999999999999975
(): -6.938893903907228e-18
(np.int64(5),): -0.10000000000000005
(np.int64(6),): -0.2
(np.int64(7),): -0.30000000000000004
)
Force plot¶
iv.plot_force(feature_names=feature_names)

Network plot¶
iv.plot_network(feature_names=feature_names)

References¶
Total running time of the script: (0 minutes 12.629 seconds)