giuseppec / iml

iml: interpretable machine learning R package
https://giuseppec.github.io/iml/
Other
491 stars 88 forks source link

can't calculate Accumulated local effects or Shapley values for a Random Forest model generated from `ranger` package? #159

Closed shihuang047 closed 3 years ago

shihuang047 commented 3 years ago

R package ranger is a fast implementation of Random Forests using C++ (https://github.com/imbs-hl/ranger), which I have been used a lot in my large datasets. I just quickly ran the example code you provided on the front page and replaced the model from RandomForest package with one by ranger.
library("iml") library("randomForest") data("Boston", package = "MASS") rf = randomForest(medv ~ ., data = Boston, ntree = 50) X = Boston[which(names(Boston) != "medv")] model = Predictor$new(rf, data = X, y = Boston$medv) effect = FeatureEffects$new(model) effect$plot(features = c("lstat", "age", "rm"))

rf = ranger::ranger(medv ~ ., data = Boston, ntree = 50) X = Boston[which(names(Boston) != "medv")] model = Predictor$new(rf, data = X, y = Boston$medv) effect = FeatureEffects$new(model) Error in feature_effect(feature, predictor = predictor, method = method, : task 1 failed - "cannot coerce class ‘"ranger.prediction"’ to a data.frame"

corneliagru commented 3 years ago

Hi, you need to manually set the predict function like this:

pfun <- function(object, newdata) predict(object, data = newdata)$predictions

model = Predictor$new(rf, data = X, y = Boston$medv, predict.fun = pfun)