2016-09-05 118 views
2

我正在接觸所有的SO C++天才。c + +中的xgboost加載模型(python - > C++預測分數不匹配)

我訓練(和測試成功)在python的xgboost模型,像這樣:

dtrain 
=xgb.DMatrix(np.asmatrix(X_train),label=np.asarray(y_train,dtype=np.int), feature_names=feat_names) 

optimal_model = xgb.train(plst, dtrain) 

dtest = xgb.DMatrix(np.asmatrix(X_test),feature_names=feat_names) 

optimal_model.save_model('sigdet.model') 

我已經按照在XgBoost後(see link),它介紹了正確的方式來加載和應用預測在C++:

// Load Model 
g_learner = std::make_unique<Learner>(Learner::Create({})); 
     std::unique_ptr<dmlc::Stream> fi(
      dmlc::Stream::Create(filename, "r")); 
     g_learner->Load(fi.get()); 

// Predict 
    DMatrixHandle h_test; 
     XGDMatrixCreateFromMat((float *)features, 1, numFeatures , -999.9f, &h_test); 
     xgboost::bst_ulong out_len; 


     std::vector<float> preds; 
     g_learner->Predict((DMatrix*)h_test,true, &preds); 

我的問題(1):我需要創建一個DMatrix *,但我只有一個DMatrixHandle。我如何正確創建一個包含我的數據的DMatrix?

我的問題(2):當我嘗試了以下預測方法:

DMatrixHandle h_test; 
XGDMatrixCreateFromMat((float *)features, 1, numFeatures , -999.9f, &h_test); 
xgboost::bst_ulong out_len; 


int res = XGBoosterPredict(g_modelHandle, h_test, 1, 0, &out_len, (const float**)&scores); 

我得到完全不同的分數比加載完全相同的模型,並用它來預測(以蟒蛇)。無論是誰幫助我在C++和python中獲得一致的結果,都可能會進入天堂。順便說一句,我需要應用C++預測實時應用程序,否則我會使用不同的語言。

回答

2

要獲得DMatrix你可以這樣做:

g_learner->Predict(static_cast<std::shared_ptr<xgboost::DMatrix>*>(h_test)->get(), true, &pred); 

對於問題(2),我沒有答案。這實際上是我有同樣的問題。我在python中有一個XGBRegression,我用C++中的相同特性獲得了不同的結果。

0

下面是一個例子,但該程序的預測是一致的:

const int cols=3,rows=100; 
float train[rows][cols]; 
for (int i=0;i<rows;i++) 
    for (int j=0;j<cols;j++) 
     train[i][j] = (i+1) * (j+1); 

float train_labels[rows]; 
for (int i=0;i<50;i++) 
    train_labels[i] = 0; 
for (int i=50;i<rows;i++) 
    train_labels[i] = 1; 


// convert to DMatrix 
DMatrixHandle h_train[1]; 
XGDMatrixCreateFromMat((float *) train, rows, cols, -1, &h_train[0]); 

// load the labels 
XGDMatrixSetFloatInfo(h_train[0], "label", train_labels, rows); 

// read back the labels, just a sanity check 
bst_ulong bst_result; 
const float *out_floats; 
XGDMatrixGetFloatInfo(h_train[0], "label" , &bst_result, &out_floats); 
for (unsigned int i=0;i<bst_result;i++) 
    std::cout << "label[" << i << "]=" << out_floats[i] << std::endl; 

// create the booster and load some parameters 
BoosterHandle h_booster; 
XGBoosterCreate(h_train, 1, &h_booster); 
XGBoosterSetParam(h_booster, "objective", "binary:logistic"); 
XGBoosterSetParam(h_booster, "eval_metric", "error"); 
XGBoosterSetParam(h_booster, "silent", "0"); 
XGBoosterSetParam(h_booster, "max_depth", "9"); 
XGBoosterSetParam(h_booster, "eta", "0.1"); 
XGBoosterSetParam(h_booster, "min_child_weight", "3"); 
XGBoosterSetParam(h_booster, "gamma", "0.6"); 
XGBoosterSetParam(h_booster, "colsample_bytree", "1"); 
XGBoosterSetParam(h_booster, "subsample", "1"); 
XGBoosterSetParam(h_booster, "reg_alpha", "10"); 

// perform 200 learning iterations 
for (int iter=0; iter<10; iter++) 
    XGBoosterUpdateOneIter(h_booster, iter, h_train[0]); 

// predict 
const int sample_rows = 100; 
float test[sample_rows][cols]; 
for (int i=0;i<sample_rows;i++) 
    for (int j=0;j<cols;j++) 
     test[i][j] = (i+1) * (j+1); 
DMatrixHandle h_test; 
XGDMatrixCreateFromMat((float *) test, sample_rows, cols, -1, &h_test); 
bst_ulong out_len; 
const float *f; 
XGBoosterPredict(h_booster, h_test, 0,0,&out_len,&f); 

for (unsigned int i=0;i<out_len;i++) 
    std::cout << "prediction[" << i << "]=" << f[i] << std::endl; 


// free xgboost internal structures 
XGDMatrixFree(h_train[0]); 
XGDMatrixFree(h_test); 
XGBoosterFree(h_booster);