mayer79 / flashlight

Machine learning explanations
https://mayer79.github.io/flashlight/
GNU General Public License v2.0
22 stars 4 forks source link

Ranger Classification #38

Closed p-schaefer closed 3 years ago

p-schaefer commented 3 years ago

I am having trouble running the functions for classifications using Ranger, i.e.,

library(ranger)
library(flashlight)
library(MetricsWeighted)

test1<-data.frame(
  resp=as.logical(round(runif(100))),
  x1=rnorm(100),
  x3=rnorm(100,10,5)
)

m1<-ranger(resp~.,
            data=test1,
            probability =F
)

fl <- flashlight(model = m1, data = test1, label = "rf",
                 metrics = list(logLoss = logLoss, AUC = AUC),
                 predict_function = function(mod, X) predict(mod, X)$predictions)

plot(light_importance(fl))

#Error: Problem with `summarise()` input `..1`.
#x all(predicted > 0 & predicted < 1) is not TRUE
#i Input `..1` is `core_fun(cur_data())`.
#i The error occurred in group 1: label = "rf".
#Run `rlang::last_error()` to see where the error occurred.
mayer79 commented 3 years ago

Hello. You would need to specify y = "Resp" in building the explainer and use a metric compatible with 0/1 predictions (logLoss is a metric used when predictions are probabilities, not classes).

fl <- flashlight(model = m1, data = test1, label = "rf", 
                 metrics = list(AUC = AUC),
                 predict_function = function(mod, X) predict(mod, X)$predictions,
                 y = "resp")

plot(light_importance(fl, lower_is_better = FALSE, m_repetitions = 10), 
     fill = "orange")

image