在C++中实现XGBoost C API时遇编译错误求解决
解决XGBoost C API C++编译错误问题
问题描述
我尝试实现从Stack Overflow找到的XGBoost C API的C++示例代码,代码如下:
#include <iostream> #include <include/xgboost/c_api.h> #include <stdio.h> #include <stdlib.h> using namespace std; int main() { // 创建训练数据 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; // 转换为DMatrix DMatrixHandle h_train[1]; XGDMatrixCreateFromMat((float *) train, rows, cols, -1, &h_train[0]); // 加载标签 XGDMatrixSetFloatInfo(h_train[0], "label", train_labels, rows); // 读取标签做 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; // 创建Booster并设置参数 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"); // 执行200轮训练 for (int iter=0; iter<200; iter++) XGBoosterUpdateOneIter(h_booster, iter, h_train[0]); // 预测 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; // 释放XGBoost内部结构 XGDMatrixFree(h_train[0]); XGDMatrixFree(h_test); XGBoosterFree(h_booster); return 0; }
编译时出现如下错误:
||=== Build: Debug in xgboost_demo (compiler: GNU GCC Compiler) ===| C:\Users\Documents\C++\xgboost_demo\main.cpp||In function 'int main()':| C:\Users\Documents\C++\xgboost_demo\main.cpp|63|error: invalid conversion from 'bst_ulong* {aka long long unsigned int*}' to 'int' [-fpermissive]| C:\Users\YannickLECROART\Documents\C++\xgboost_demo\main.cpp|63|error: cannot convert 'const float**' to 'bst_ulong* {aka long long unsigned int*}' for argument '6' to 'int XGBoosterPredict(BoosterHandle, DMatrixHandle, int, unsigned int, int, bst_ulong*, const float**)'| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
错误原因分析
从错误信息里的XGBoosterPredict函数签名可以看出,你使用的XGBoost版本中,这个函数需要7个参数,但你的调用只传了6个,导致后续参数的类型完全错位:
- 你把
&out_len(bst_ulong*类型)传给了原本需要int类型的第5个参数 - 把
&f(const float**类型)传给了原本需要bst_ulong*类型的第6个参数
这个缺失的第5个参数是pred_leaf,用来控制是否输出叶子节点索引(0表示输出普通预测值,1表示输出叶子节点索引)。
修改方案
只需要在XGBoosterPredict的调用中补充这个缺失的参数(传0即可,代表输出正常预测结果),修改第63行的代码:
原代码行:
XGBoosterPredict(h_booster, h_test, 0,0,&out_len,&f);
修改后代码行:
XGBoosterPredict(h_booster, h_test, 0, 0, 0, &out_len, &f);
修改后的完整代码
#include <iostream> #include <include/xgboost/c_api.h> #include <stdio.h> #include <stdlib.h> using namespace std; int main() { // 创建训练数据 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; // 转换为DMatrix DMatrixHandle h_train[1]; XGDMatrixCreateFromMat((float *) train, rows, cols, -1, &h_train[0]); // 加载标签 XGDMatrixSetFloatInfo(h_train[0], "label", train_labels, rows); // 读取标签做 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; // 创建Booster并设置参数 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"); // 执行200轮训练 for (int iter=0; iter<200; iter++) XGBoosterUpdateOneIter(h_booster, iter, h_train[0]); // 预测 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; // 补充缺失的pred_leaf参数,传0表示输出预测值 XGBoosterPredict(h_booster, h_test, 0, 0, 0, &out_len, &f); for (unsigned int i=0;i<out_len;i++) std::cout << "prediction[" << i << "]=" << f[i] << std::endl; // 释放XGBoost内部结构 XGDMatrixFree(h_train[0]); XGDMatrixFree(h_test); XGBoosterFree(h_booster); return 0; }
额外提示
如果后续还有链接问题,需要确保编译时链接XGBoost的库文件(比如-lxgboost),并指定头文件路径(比如-I/path/to/xgboost/include)。
内容的提问来源于stack exchange,提问作者Synox
相关产品推荐
相关产品推荐

