Note
Go to the end to download the full example code.
kADDSHAP¶
k-additive SHAP approximation using
kADDSHAP Pelegrina 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 k-SII values¶
approximator = shapiq.kADDSHAP(n=N_PLAYERS, max_order=2, random_state=42)
iv = approximator.approximate(BUDGET, game_fun)
print(iv)
InteractionValues(
index=kADD-SHAP, max_order=2, min_order=0, estimated=True, estimation_budget=200,
n_players=8, baseline_value=0.0,
Top 10 interactions:
(0,): 0.6499998966140745
(1,): 0.5499999342652836
(0, 1): 0.5000000150714768
(2,): 0.19999997819261187
(3,): 0.09999997684854402
(4,): 0.05000002263696405
(1, 6): 4.51538482735569e-08
(5,): -0.09999993894871723
(6,): -0.19999995832833192
(7,): -0.2999999112804287
)
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 0.688 seconds)