首页 > 解决方案 > 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 失败 - “存储的特征名称object不同newdata!”

我尝试检查模型和数据中的特征名称,但它们都相似。因此,我不明白这个错误究竟是什么“存储的功能名称object不同newdata!”

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" 

标签: rxgboostmlr

解决方案


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

它关于变量的顺序。

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

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

编辑:错误仍然存​​在,可能是因为iml. 但修复绝对是要走的路,因为使用xgboost.


推荐阅读