R - 检测到非树模型!此功能只能与树模型一起使用 [英] R - Getting Non-tree model detected! This function can only be used with tree models
问题描述
我是 R 的新手.当我尝试运行 xgb.importance
时,我得到了这个
I'm a newbie in R. When I tried running xgb.importance
, I am getting this
"Error in xgb.model.dt.tree(feature_names = feature_names, text = text) :
Non-tree model detected! This function can only be used with tree models".
任何帮助将不胜感激.
require(xgboost)
require(Matrix)
require(data.table)
if (!require('vcd')) install.packages('vcd')
a = data.frame(id=c(1,2,3,4,5), smoke=c('Yes','No','Yes', 'Yes', 'Yes'), sugar=c('Yes','No','Yes', 'Yes','Yes'), sex=c('M','F','F', 'M','F'), diseased=c('Yes','No','Yes', 'Yes','Yes'), age=c(20,21,45, 45, 40))
d <- data.table(a, keep.rownames = F)
head(d[,AgeDiscret := as.factor(round(age/10,0))])
head(d[,AgeCat:= as.factor(ifelse(age > 30, "Old", "Young"))])
s <- sparse.model.matrix(age~.-1, data = d)
ov = d[,diseased] == 'Yes'
mdl <- xgboost(data = s, label = ov, max_depth = 4, eta = 1, nthread = 2, nrounds = 10,objective = "binary:logistic")
importance <- xgb.importance(feature_names = colnames(s), model = mdl) #<-- error message
推荐答案
当模型参数是一个在所有预测变量之间具有完美共线性的数据上训练的模型时,我遇到了这个错误(即 0,1,0,1,0,1,0,1,0).
I experienced this error when the model argument was a model trained on data that had perfect collinearity between all predictor variables and a target that was consistently alternating between 0 and 1 (i.e. 0,1,0,1,0,1,0,1,0).
我使用的数据是为了对包的一部分进行单元测试而生成的测试数据.我通过使用 rnorm() 为预测变量和 sample() 为目标变量生成数据解决了这个问题.
The data I used was test data generated for the purpose of unit testing part of a package. I solved the problem by generating data using rnorm() for the predictor variables and sample() for the target variable.
这篇关于R - 检测到非树模型!此功能只能与树模型一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!