python - 在C++ API中加载经过python训练的xgboost,预测结果为空

标签 python c++ xgboost

我在python xgboost上训练了13个类别的分类,我的特征昏暗的是3207,在python xgbsoot训练后保存了模型,我检查了一些情况,结果是正常的。
但是,当我在c++ xgboost中加载模型并进行预测时,结果什么都没有。这是我的c++代码演示:

  BoosterHandle booster_ = nullptr;
  std::cout << "befor:" << booster_ << std::endl;
  if (XGBoosterCreate(NULL, 0, &booster_) == 0 &&
       XGBoosterLoadModel(booster_, model_path.c_str()) == 0) {
    LOG(INFO) << "load xgboost model success !";
  } else {
    LOG(ERROR) << "load xgboost model error !";
    booster_ = NULL;
  }

  // build Dmatrix data, two fake example, the expect predict label is 1, prob = 0.927.
  float d_values[2][3207] = {0};
  d_values[0][0] = 1.0;
  d_values[0][8] = 1.0;
  d_values[0][6] = 1.0;
  d_values[0][14] = 1.0;
  d_values[1][0] = 1.0;
  d_values[1][8] = 1.0;
  d_values[1][6] = 1.0;
  d_values[1][14] = 1.0;
  std::cout <<"predict:" << booster_ << std::endl;
  int r = XGDMatrixCreateFromMat(&d_values[0][0], 2, 3207, 0.0, &data);
  if (r != 0) {
    LOG(ERROR) << "build DMatrix failed!!";
  }
  std::cout <<"r=" << r << std::endl;
  const float* out;
  uint64_t len;
  auto ret = XGBoosterPredict(booster_, data, 0, 0, &len, &out);
  if (ret < 0) {
    LOG(ERROR) << "xgboost inference error !";
    return -1;
  }
  std::cout << "len=" << len << std::endl;
  XGDMatrixFree(data);
  return 0;
结果是:
  r=0
  len=0
在此演示的每一步中,返回状态码均等于0,但最终len = 0,这是什么问题?

最佳答案

我找到了原因,我的python xgboost是经过培训的anaconda,xgboost与c++ xgboost源代码版本不同。

关于python - 在C++ API中加载经过python训练的xgboost,预测结果为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63450441/

相关文章:

Python 正则表达式未返回所需结果

Python 3 sqlite 参数化 SQL 查询

c++ - 将 double 转换为 LSBF (little Endian) 十六进制字符串

c++ - 使用标签调度时如何反转模板参数?

machine-learning - XGBoost 模型上的 GridSearchCV 给出错误

python - 检查路径中是否存在某个文件夹

python - 如何在 python run_in_executor 方法调用中捕获异常

c++ - 如何在 OpenCV 中训练/使用 HOGDescriptor 类

machine-learning - 尽管已编译为 GPU 运行,但通过 Scikit learn API 的 XGB 似乎并未在 GPU 中运行

machine-learning - 使用稀疏二元矩阵进行二元分类