UnbiasedKernelSHAP¶

Unbiased Monte Carlo variant of KernelSHAP using UnbiasedKernelSHAP.

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 Shapley values¶

approximator = shapiq.UnbiasedKernelSHAP(n=N_PLAYERS, random_state=42)
iv = approximator.approximate(BUDGET, game_fun)
print(iv)
InteractionValues(
    index=SV, max_order=1, min_order=0, estimated=True, estimation_budget=200,
    n_players=8, baseline_value=0.0,
    Top 10 interactions:
        (0,): 0.620357886904762
        (1,): 0.512624751984127
        (2,): 0.19381919642857143
        (3,): 0.09848883928571431
        (4,): 0.051639632936507904
        (): 0.0
        (5,): -0.062796875
        (6,): -0.2172036210317461
        (7,): -0.24692981150793655
)

Visualize the result¶

iv.plot_force(feature_names=feature_names)
plot unbiased kernelshap

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