Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upImplement additive force plots #7
Comments
|
Minimal example: # Load required packages
library(fastshap) # for fast (approximate) Shapley values
library(mlbench) # for Friedman 1 benchmark data set
library(ranger) # for fast random forest algorithm
# Simulate training data
set.seed(101)
trn <- as.data.frame(mlbench.friedman1(3000))
X <- subset(trn, select = -y) # feature columns only
# Fit a random forest
set.seed(102)
rfo <- ranger(y ~ ., data = trn)
# Prediction wrapper
pfun <- function(object, newdata) {
predict(object, data = newdata)$predictions
}
# Compute fast (approximate) Shapley values using 10 Monte Carlo repetitions
system.time({ # estimate run time
set.seed(5038)
ex <- explain(rfo, X = X, pred_wrapper = pfun, nsim = 10)
})
# Construct force plot
force_plot(ex[1, ], prediction = pfun(rfo, X[1, ]),
baseline = mean(pfun(rfo, X)), feature_values = X[1, ]) |

See here for examples. Here's a cheap hack using reticulate: