2016-05-16 71 views
1

我看不到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: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,])) 

只預測三個值而不是預期的5

所以我的問題是,我如何使用與用於構建模型相同的功能來預測XGBoost?在這個例子中,我建立了一個具有三個特徵的模型,lag1,lag2和lag3,以預測響應,回報。但是,當試圖使用predict進行預測時,該函數的行爲就好像它只使用一個特徵一樣,並且如果它使用多個值(例如轉置測試集時),則不知道如何使用這些值。

回答

3

你是真的關閉...留在我這裏......

> 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 

ALSO

你得真的要小心,因爲如果你不給它足夠的列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 :) 

只要確保你給它一個具有相同列數的矩陣或所有地獄都會破散:)

+0

謝謝。正是我需要的 –