R:使用 FeatureImp$new 绘制重要性特征



这是代码

library(mlr)
library(xgboost)
library(iml)
data("iris")
tsk = makeClassifTask(data = iris, target = "Species")
lrn = makeLearner("classif.xgboost",predict.type = "prob")
mod = mlr:::train(lrn, tsk)
X = iris[which(names(iris) != "Species")]
predictor = Predictor$new(mod, data = X, y = iris$Species)
imp = FeatureImp$new(predictor, loss = "ce")

我收到以下错误

imp = FeatureImp$new(predictor, loss = "ce")
Warning in predict.WrappedModel(model, newdata = newdata) :
Provided data for prediction is not a pure data.frame but from class data.table, 
hence it will be converted.

estimate.feature.imp(feature, data.sample = data.sample, y = y, 中的错误: 任务 1 失败 - "存储在objectnewdata中的功能名称不同!

我尝试检查模型和数据中的特征名称,但它们都是相似的。因此,我不明白这个错误到底是什么 "存储在objectnewdata中的功能名称是不同的!">

colnames(X)
[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 
mod$learner.model$feature_names    
[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 

这是一个xgboost问题:https://github.com/dmlc/xgboost/issues/1809

它与变量的顺序有关。

X = X[mod$learner.model$feature_names]

应该解决它。几天前我遇到了同样的问题。

编辑:错误仍然发生,可能是因为由于iml的洗牌再次出现一些混乱。但是修复绝对是要走的路,因为使用xgboost预测调用也会发生此错误。

最新更新