Note
Go to the end to download the full example code.
RegressionMSR¶
Proxy model-based Shapley value approximation using
RegressionMSR.
from __future__ import annotations
import numpy as np
from shapiq.approximator import RegressionMSR
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 Shapley values¶
approximator = RegressionMSR(n=N_PLAYERS, index="SV", random_state=42)
iv = approximator.approximate(BUDGET, game_fun)
print(iv)
InteractionValues(
index=SV, max_order=1, min_order=0, estimated=False, estimation_budget=200,
n_players=8, baseline_value=0.0,
Top 10 interactions:
(0,): 0.6464761982147177
(1,): 0.5515021821526129
(2,): 0.19488992615953163
(3,): 0.09609131788141716
(4,): 0.048648088084358776
(): 0.0
(5,): -0.09461249252463451
(6,): -0.19591253229119593
(7,): -0.2970827534764757
)
Force plot¶
iv.plot_force(feature_names=feature_names)

Stacked bar plot¶
iv.plot_stacked_bar(feature_names=feature_names)

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