XGBoost predict()具有多个功能的错误

XGBoost predict()具有多个功能的错误,r,predict,xgboost,R,Predict,Xgboost,我看不出XGBoost的预测方法是如何使用多个功能进行预测的 library(xgboost) library(MASS) sp500=data.frame(SP500) label=sp500[,1] lag1=sp500[-1,] lag2=lag1[-1] lag3=lag2[-1] train=cbind(lag1,lag2,lag3) model=xgboost(data=train[50:1000,],label=label[50:1000], objective="reg:li

我看不出XGBoost的预测方法是如何使用多个功能进行预测的

library(xgboost)
library(MASS)

sp500=data.frame(SP500)
label=sp500[,1]
lag1=sp500[-1,]
lag2=lag1[-1]
lag3=lag2[-1]
train=cbind(lag1,lag2,lag3)

model=xgboost(data=train[50:1000,],label=label[50:1000],
objective="reg:linear",booster="gbtree",nround=50)

predict(model,train[1,]) #returns an error, because it will not accept multiple columns



predict(model,t(train[1,]))
转置我的测试集不会返回错误,但是这是错误地使用预测器,因为

predict(model,t(train[1:5,]))
仅预测三个值,而不是预期的五个值


所以我的问题是,如何使用XGBoost进行预测,使用与构建模型相同的功能?在本例中,我构建了一个具有三个特征的模型,即lag1、lag2和lag3,以预测响应、返回。但是,当尝试使用
predict
进行预测时,函数的行为就好像它只使用一个功能一样,如果它使用多个值,比如当我转换测试集时,则不知道它是如何使用这些值的。

你真的很接近。。。跟我呆在这里

> dim(train)
[1] 2779    3
好的,你训练了三个特点。。这并不奇怪

当你这么做的时候

> predict(model,train[1,])
Error in xgb.DMatrix(newdata) : 
  xgb.DMatrix: does not support to construct from  double
xboost
正在寻找一个矩阵,你给了它一个向量,继续

##this works 

> predict(model,t(train[1,]))
[1] -0.09167647
> dim(t(train[1,]))
[1] 1 3
因为你变换了一个向量,它构成了一个1*3的矩阵

但这是一团糟

> predict(model, t(train[1:5,]))
[1] -0.09167647  0.31090808 -0.10482860
> dim(t(train[1:5,]))
[1] 3 5
### Xgboost used the 3 rows and the first three columns only to predict
## the transpose didn't do the same thing here
错误是因为转置(列)向量和转置矩阵是不同的事情

你真正想要的是这个

> predict(model,train[1:5,]) 
[1] -0.09167647  0.31090808 -0.10482860 -0.02773660  0.33554882
> dim(train[1:5,]) ## five rows of three columns
[1] 5 3
同时

你必须非常小心,因为如果你没有给它足够的列
xgboost
会像这样回收这些列

 predict(model,train[1:5,1:2])
[1] -0.07803667 -0.25330877  0.10844088 -0.04510367 -0.27979547
 ## only gave it two columns and it made a prediction :)

只要确保你给它一个相同列数的矩阵,否则所有的麻烦都会散开:)

谢谢。正是我需要的