如何在c ++中使用XGBOOST https://github.com/dmlc/xgboost/库?我已经创建了Python和Java API,但我找不到c ++的API
Tom*_*mer 25
我最终使用了C API,请参阅下面的示例:
// create the train data
int cols=3,rows=5;
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<rows;i++)
train_labels[i] = 1+i*i*i;
// 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, "booster", "gbtree");
XGBoosterSetParam(h_booster, "objective", "reg:linear");
XGBoosterSetParam(h_booster, "max_depth", "5");
XGBoosterSetParam(h_booster, "eta", "0.1");
XGBoosterSetParam(h_booster, "min_child_weight", "1");
XGBoosterSetParam(h_booster, "subsample", "0.5");
XGBoosterSetParam(h_booster, "colsample_bytree", "1");
XGBoosterSetParam(h_booster, "num_parallel_tree", "1");
// perform 200 learning iterations
for (int iter=0; iter<200; iter++)
XGBoosterUpdateOneIter(h_booster, iter, h_train[0]);
// predict
const int sample_rows = 5;
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);
Run Code Online (Sandbox Code Playgroud)
使用 XGBoost C API。
\n\n BoosterHandle booster;\n const char *model_path = "/path/of/model";\n\n // create booster handle first\n XGBoosterCreate(NULL, 0, &booster);\n\n // by default, the seed will be set 0\n XGBoosterSetParam(booster, "seed", "0");\n\n // load model\n XGBoosterLoadModel(booster, model_path);\n\n const int feat_size = 100;\n const int num_row = 1;\n float feat[num_row][feat_size];\n\n // create some fake data for predicting\n for (int i = 0; i < num_row; ++i) {\n for(int j = 0; j < feat_size; ++j) {\n feat[i][j] = (i + 1) * (j + 1)\n }\n }\n\n // convert 2d array to DMatrix\n DMatrixHandle dtest;\n XGDMatrixCreateFromMat(reinterpret_cast<float*>(feat),\n num_row, feat_size, NAN, &dtest);\n\n // predict\n bst_ulong out_len;\n const float *f;\n XGBoosterPredict(booster, dtest, 0, 0, &out_len, &f);\n assert(out_len == num_row);\n std::cout << f[0] << std::endl;\n\n // free memory\n XGDMatrixFree(dtest);\n XGBoosterFree(booster);\n
Run Code Online (Sandbox Code Playgroud)\n\n请注意,当您想要加载现有模型(如上面的代码所示)时,您必须确保训练中的数据格式与预测中的数据格式相同。因此,如果您使用接受稠密矩阵作为参数的 XGBoosterPredict 进行预测,则必须在训练中使用稠密矩阵。
\n\n使用 libsvm 格式进行训练并使用密集矩阵进行预测可能会导致错误的预测,如XGBoost FAQ所说:
\n\n\n\n\xe2\x80\x9cSparse\xe2\x80\x9d 元素被树增强器视为 \xe2\x80\x9cmissing\xe2\x80\x9d,并被线性增强器视为零。对于树模型,在训练和评分期间使用一致的数据格式非常重要。
\n
归档时间: |
|
查看次数: |
12729 次 |
最近记录: |