如何使R IML FeatureImp()函数正常工作?

时间:2019-03-13 11:36:08

标签: r machine-learning

我正在尝试从IML包中获取FeatureImp函数,但是它一直抛出错误。下面是钻石数据集中的一个示例,我在该示例上训练了一个随机森林模型。

library(iml)
library(caret)
library(randomForest)
data(diamonds)
# create some binary classification target (without specific meaning)
diamonds$target <- as.factor(ifelse(diamonds$color %in% c("D", "E", "F"), "X", "Y"))
# drop categorical variables (to keep it simple for demonstration purposes)
diamonds <- subset(diamonds, select = -c(color, clarity, cut))
# train model
mdl_diamonds <- train(target ~ ., method = "rf", data = diamonds)
# create iml predictor
x_pred <- Predictor$new(model = mdl_diamonds, data = diamonds[, 1:7], y = diamonds$target, type = "prob")
# calculate feature importance
x_imp <- FeatureImp$new(x_pred, loss = "mae")

这以以下错误结尾:

Error in if (self$original.error == 0) { :
  missing value where TRUE/FALSE needed
In addition: Warning message:
In Ops.factor(actual, predicted) : ‘-’ not meaningful for factors

我不明白我在做什么错。谁能给我提示?

我正在研究R版本3.5.1,iml软件包版本0.9.0。

2 个答案:

答案 0 :(得分:0)

我发现了问题。我使用“ mae”作为损失函数,这是-我可能知道-不适用于分类目标。使用“ ce”或“ f1”返回预期的输出。

答案 1 :(得分:-1)

因为它是随机森林。因此请尝试loss ='ce'。