@@ -14,9 +14,9 @@ bg_X <- diamonds[seq(1, nrow(diamonds), 450), ]
14
14
# Subset of 1018 diamonds to explain
15
15
X_small <- diamonds [seq(1 , nrow(diamonds ), 53 ), c(" carat" , ord )]
16
16
17
- # Exact KernelSHAP (5s )
17
+ # Exact KernelSHAP (2s )
18
18
system.time(
19
- ks <- kernelshap(fit , X_small , bg_X = bg_X )
19
+ ks <- kernelshap(fit , X_small , bg_X = bg_X )
20
20
)
21
21
ks
22
22
25
25
# [1,] -2.050074 -0.28048747 0.1281222 0.01587382
26
26
# [2,] -2.085838 0.04050415 0.1283010 0.03731644
27
27
28
- # Pure sampling version takes a bit longer (12 seconds)
28
+ # Pure sampling version takes a bit longer (7 seconds)
29
29
system.time(
30
- ks2 <- kernelshap(fit , X_small , bg_X = bg_X , exact = FALSE , hybrid_degree = 0 )
30
+ ks2 <- kernelshap(fit , X_small , bg_X = bg_X , exact = FALSE , hybrid_degree = 0 )
31
31
)
32
32
ks2
33
33
36
36
# [1,] -2.050074 -0.28048747 0.1281222 0.01587382
37
37
# [2,] -2.085838 0.04050415 0.1283010 0.03731644
38
38
39
- # Using parallel backend
40
- library(" doFuture" )
41
-
42
- registerDoFuture()
43
- plan(multisession , workers = 2 ) # Windows
44
- # plan(multicore, workers = 2) # Linux, macOS, Solaris
45
-
46
- # 3 seconds
47
- system.time(
48
- ks3 <- kernelshap(fit , X_small , bg_X = bg_X , parallel = TRUE )
49
- )
50
- ks3
51
39
52
40
library(shapviz )
53
41
@@ -58,18 +46,17 @@ sv_dependence(sv, "carat")
58
46
# More features (but non-sensical model)
59
47
# Fit model
60
48
fit <- lm(
61
- log(price ) ~ log(carat ) * (clarity + color + cut ) + x + y + z + table + depth ,
49
+ log(price ) ~ log(carat ) * (clarity + color + cut ) + x + y + z + table + depth ,
62
50
data = diamonds
63
51
)
64
52
65
53
# Subset of 1018 diamonds to explain
66
54
X_small <- diamonds [seq(1 , nrow(diamonds ), 53 ), setdiff(names(diamonds ), " price" )]
67
55
68
- # Exact KernelSHAP on X_small, using X_small as background data
69
- # (58/67(?) seconds for exact, 25/18 for hybrid deg 2, 16/9 for hybrid deg 1,
70
- # 26/17 for pure sampling; second number with 2 parallel sessions on Windows)
56
+ # Exact KernelSHAP on X_small, using X_small as background data
57
+ # (39s for exact, 15s for hybrid deg 2, 8s for hybrid deg 1, 16s for sampling)
71
58
system.time(
72
- ks <- kernelshap(fit , X_small , bg_X = bg_X )
59
+ ks <- kernelshap(fit , X_small , bg_X = bg_X )
73
60
)
74
61
ks
75
62
@@ -98,7 +85,7 @@ X = diamonds[x].to_numpy()
98
85
99
86
# Fit model with interactions and dummy variables
100
87
fit = ols(
101
- " np.log(price) ~ np.log(carat) * (C(clarity) + C(cut) + C(color))" , # + x + y + z + table + depth",
88
+ " np.log(price) ~ np.log(carat) * (C(clarity) + C(cut) + C(color))" , # + x + y + z + table + depth",
102
89
data = diamonds
103
90
).fit()
104
91
@@ -110,7 +97,7 @@ X_small = X[0:len(X):53]
110
97
111
98
# Calculate KernelSHAP values
112
99
ks = KernelExplainer(
113
- model = lambda X : fit.predict(pd.DataFrame(X , columns = x )),
100
+ model = lambda X : fit.predict(pd.DataFrame(X , columns = x )),
114
101
data = bg_X
115
102
)
116
103
sv = ks.shap_values(X_small ) # 11 minutes
@@ -127,4 +114,4 @@ sv[0:2]
127
114
# -1.72078182e-01, 1.33027467e-03, -6.44569296e-03],
128
115
# [-1.87670887e+00, 3.93291219e-02, 1.26654599e-01,
129
116
# 3.85695742e-02, -4.87177593e-04, -4.20263565e-04,
130
- # -1.73988040e-01, 1.39779179e-03, -6.56062359e-03]])
117
+ # -1.73988040e-01, 1.39779179e-03, -6.56062359e-03]])
0 commit comments