Example 3: Custom value functions
These examples are from section 5.1 of the Faith-Shap paper: https://arxiv.org/abs/2203.00870
[1]:
import seaborn as sns
sns.set_style("whitegrid")
sns.set_context("notebook", rc={'axes.linewidth': 2, 'grid.linewidth': 1}, font_scale=1.5)
import numpy as np
import math
import nshap
Example 1
A value function takes two arguments: A single data point x (a numpy.ndarray) and a python list S with the indices the the coordinates that belong to the coaltion.
In this example, the value does not actually depend on the point x
[7]:
p = 0.1
def v_func(x, S):
""" The value function from Example 1 in the Faith-Shap paper.
"""
if len(S) <= 1:
return 0
return len(S) - p * math.comb(len(S), 2)
v_func(np.zeros((1,11)), [1,2,3,4])
[7]:
3.4
Equipped with the value function, we can compute different kinds of interaction indices
[ ]:
faith_shap = nshap.faith_shap(np.zeros((1,11)), v_func, 1)
faith_shap[(0,1)], faith_shap[(1,2)]
We can replicate Table 1 in the Faith-Shap paper
[13]:
print('Table 1 in the Faith-Shap paper:\n')
for p in [0.1, 0.2]:
for l in [1, 2]:
print(f'p={p} l={l}\n')
# define the value function
def v_func(x, S):
if len(S) <= 1:
return 0
return len(S) - p * math.comb(len(S), 2)
# compute interaction indices
faith_shap = nshap.faith_shap(np.zeros((1,11)), v_func, l)
shapley_taylor = nshap.shapley_taylor(np.zeros((1,11)), v_func, l)
shapley_interaction = nshap.shapley_interaction_index(np.zeros((1,11)), v_func, l)
banzhaf_interaction = nshap.banzhaf_interaction_index(np.zeros((1,11)), v_func, l)
faith_banzhaf = nshap.faith_banzhaf(np.zeros((1,11)), v_func, l)
# print result
if l == 1:
print('Faith-Shap: ', faith_shap[(0,)])
print('Shapley Taylor: ', shapley_taylor[(0,)])
print('Interaction Shapley: ', shapley_interaction[(0,)])
print('Banzhaf Interaction: ', banzhaf_interaction[(0,)])
print('Faith-Banzhaf: ', faith_banzhaf[(0,)])
else:
print('Faith-Shap: ', faith_shap[(0,)], faith_shap[(0,1)])
print('Shapley Taylor: ', shapley_taylor[(0,)], shapley_taylor[(0,1)])
print('Interaction Shapley: ', shapley_interaction[(0,)], shapley_interaction[(0,1)])
print('Banzhaf Interaction: ', banzhaf_interaction[(0,)], banzhaf_interaction[(0,1)])
print('Faith-Banzhaf: ', faith_banzhaf[(0,)], faith_banzhaf[(0,1)])
print('')
Table 1 in the Faith-Shap paper:
p=0.1 l=1
Faith-Shap: 0.5000000000001867
Shapley Taylor: 0.500000000000185
Interaction Shapley: 0.4999999999999939
Banzhaf Interaction: 0.5087890624999979
Faith-Banzhaf: 0.5087890624999989
p=0.1 l=2
Faith-Shap: 0.9545454545463214 -0.09090909090923938
Shapley Taylor: 0 0.10000000000002415
Interaction Shapley: 0.4999999999999939 -5.759281940243e-16
Banzhaf Interaction: 0.5087890624999979 -0.11367187500000073
Faith-Banzhaf: 1.0771484374999503 -0.11367187499999737
p=0.2 l=1
Faith-Shap: 1.6486811915683575e-13
Shapley Taylor: 1.7552626019323725e-13
Interaction Shapley: 2.7755575615628914e-17
Banzhaf Interaction: 0.0087890625
Faith-Banzhaf: 0.008789062500008604
p=0.2 l=2
Faith-Shap: 0.9545454545460967 -0.19090909090925617
Shapley Taylor: 0 -3.7941871866564725e-14
Interaction Shapley: 2.7755575615628914e-17 -0.10000000000000021
Banzhaf Interaction: 0.0087890625 -0.21367187500000198
Faith-Banzhaf: 1.0771484374999574 -0.21367187499998952