xgboost:数据为矩阵时需要标签

时间:2018-07-16 23:22:18

标签: r machine-learning

尝试用R运行XGboost时,我已经困了几个小时。我有一个训练数据和测试数据,其中包含40列,最后一列是目标列。它是0.1的名义值。我正在运行从https://www.kaggle.com/michaelpawlus/xgboost-example-0-76178/code获得的这段代码。

require(xgboost)
library(xgboost)

train  <- read.csv(file.choose(),header = T)
test   <- read.csv(file.choose(),header = T)

feature.names <- names(train)[2:ncol(train)-1]

 clf <- xgboost(data        = data.matrix(train[,feature.names]),
               label       = train$target,
               nrounds     = 100, # 100 is better than 200
               objective   = "binary:logistic",
               eval_metric = "auc")

 cat("making predictions in batches due to 8GB memory limitation\n")
 submission <- data.frame(ID=test$ID)
 submission$target1 <- NA 
 for (rows in test) {
    submission[rows, "Succeed"] <- predict(clf, data.matrix(test[rows,feature.names]))
 }

 varimp_clf <- xgb.importance(feature_names=feature.names,model=clf)

 xgb.plot.importance(varimp_clf)

这是我遇到的错误

  

xgb.get.DMatrix(数据,标签,缺失,重量)错误:     xgboost:数据为矩阵时需要标签

     

$<-.data.frame*tmp*,target1,值= NA)中的错误:     替换有1行,数据有0

     

predict(clf,data.matrix(test [rows,feature.names]))中的错误:     找不到对象“ clf”

1 个答案:

答案 0 :(得分:1)

检查您的输入数据。您的最后一列是否命名为target?听起来好像不是。