R xgb.importance显示错误-“ feature_names元素少于模型中使用的要素”

时间:2019-12-01 03:01:25

标签: r machine-learning xgboost

我正在探索R中的XGBoost。 训练模型后,我想查看特征重要性数据。

xgb.importance(model = bst)

上面的调用显示以下错误。怎么了?

Error in xgb.model.dt.tree(feature_names = feature_names, text = model_text_dump, : feature_names has less elements than there are features used in the model

PN-我检查了xgboost库代码的以下部分,但仍无法弄清实际问题。

# assign feature_names when available
  if (!is.null(feature_names)) {
    if (length(feature_names) <= max(as.numeric(td$Feature), na.rm = TRUE))
      stop("feature_names has less elements than there are features used in the model")
    td[isLeaf == FALSE, Feature := feature_names[as.numeric(Feature) + 1] ]
  }

Ref-https://github.com/dmlc/xgboost/blob/master/R-package/R/xgb.model.dt.tree.R

我看到训练后的模型的nfeatures变量与传递给该模型的要素数量相同。

1 个答案:

答案 0 :(得分:0)

您的模型具有feature_names功能吗? 也许尝试xgb.importance(feature_names=colnames(bst$feature_names), model = bst)。为我工作。