我是 R 的新手。当我尝试运行 xgb.importance 时,我得到了这个

"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

最佳答案

好像这里有两个问题。

  • 数据大小不够大。
  • 我的数据变化不大 - 例如烟雾部分我有"is"和“否”。我更新了"is"、“否”和“休闲”。

  • 然后我成功运行了以下代码。
    a = data.frame(id=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17, 18, 19,20), smoked=c('Yes','No','Casual', 'Casual', 'Casual','Yes', 'Yes', 'Yes','Yes', 'Yes', 'Yes','Yes', 'Yes', 'Yes','Yes','Yes','Yes', 'Yes', 'Casual','Casual'), highIntakeSugar=c('Yes','No','Yes', 'Yes','Yes', 'Yes', 'Yes','Yes','Yes', 'Yes','Yes','Yes', 'Yes','Yes','Yes', 'Yes','Yes', 'Yes', 'Yes','Yes'), sex=c('M','F','F', 'M','F','F', 'M','F','F', 'M','F','F', 'M','F','F','F','F', 'M','F','F'), disease=c('Yes','No','Unknown','Unknown','Yes','Unknown', 'Unknown','Yes','Yes', 'Yes','Yes','Yes', 'Yes', 'Yes','Yes', 'Yes','Yes', 'Yes', 'Yes','Yes'), age=c(20,21,45, 45, 40,45, 35, 40,45, 45, 40,45, 45,40,45,40,45,45,40,45))
    
    d <- data.table(a, keep.rownames = F)
    
    d[,id:=NULL]
    s <- sparse.model.matrix(age~.-1, data = d)
    
    ov = d[,disease] == 'Yes'
    mdl <- xgboost(data = s, label = ov, max_depth = 4, eta = 1, nthread = 2, nrounds = 10,objective = "binary:logistic")
    xgb.importance(feature_names = colnames(s), model = mdl)
    

    关于R - 检测到非树模型!此功能只能与树模型一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42670033/

    10-12 17:23