xgboost
函数
预测

这些函数用于运行预测和解释算法。更多...

预测的协作图

函数

int XGBoosterPredict (BoosterHandle handle, DMatrixHandle dmat, int option_mask, unsigned ntree_limit, int training, bst_ulong *out_len, const float **out_result)
 基于 dmat 进行预测(已废弃,请改用 XGBoosterPredictFromDMatrix更多...
 
int XGBoosterPredictFromDMatrix (BoosterHandle handle, DMatrixHandle dmat, char const *config, bst_ulong const **out_shape, bst_ulong *out_dim, float const **out_result)
 从 DMatrix 进行预测,取代 XGBoosterPredict更多...
 
int XGBoosterPredictFromDense (BoosterHandle handle, char const *values, char const *config, DMatrixHandle m, bst_ulong const **out_shape, bst_ulong *out_dim, const float **out_result)
 从 CPU 密集矩阵进行原地预测。更多...
 
int XGBoosterPredictFromColumnar (BoosterHandle handle, char const *values, char const *config, DMatrixHandle m, bst_ulong const **out_shape, bst_ulong *out_dim, const float **out_result)
 从 CPU 列式数据(表格)进行原地预测。更多...
 
int XGBoosterPredictFromCSR (BoosterHandle handle, char const *indptr, char const *indices, char const *values, bst_ulong ncol, char const *config, DMatrixHandle m, bst_ulong const **out_shape, bst_ulong *out_dim, const float **out_result)
 从 CPU CSR 矩阵进行原地预测。更多...
 
int XGBoosterPredictFromCudaArray (BoosterHandle handle, char const *values, char const *config, DMatrixHandle proxy, bst_ulong const **out_shape, bst_ulong *out_dim, const float **out_result)
 从 CUDA 密集矩阵(Python 中的 cupy)进行原地预测。更多...
 
int XGBoosterPredictFromCudaColumnar (BoosterHandle handle, char const *data, char const *config, DMatrixHandle proxy, bst_ulong const **out_shape, bst_ulong *out_dim, const float **out_result)
 从 CUDA 密集数据帧(Python 中的 cuDF)进行原地预测。更多...
 

详细描述

这些函数用于运行预测和解释算法。

函数文档

◆ XGBoosterPredict()

int XGBoosterPredict ( BoosterHandle  句柄,
DMatrixHandle  dmat,
int  选项掩码,
unsigned  树限制数量,
int  训练,
bst_ulong 输出长度,
const float **  输出结果 
)

基于 dmat 进行预测(已废弃,请改用 XGBoosterPredictFromDMatrix

已废弃
另请参见
XGBoosterPredictFromDMatrix()
参数
句柄句柄
dmat数据矩阵
选项掩码预测时使用的选项的位掩码,可能的值 0: 常规预测 1: 输出边距而非转换值 2: 输出树的叶子索引而非叶子值,注意叶子索引在每棵树中是唯一的 4: 输出特征对个体预测的贡献
树限制数量用于预测的树数量限制,这仅对提升树有效,当参数设为 0 时,将使用所有树
训练预测函数是否作为训练循环的一部分使用。预测可以在两种场景下运行:
  1. 给定数据矩阵 X,从模型获取预测 y_pred。
  2. 获取用于计算梯度的预测。例如,DART booster 在训练期间执行 dropout,由于树被丢弃,预测结果将与正常推理步骤获得的结果不同。第一种场景设置 training=false。第二种场景设置 training=true。第二种场景适用于您定义自定义目标函数时。
输出长度用于存储返回结果的长度
输出结果用于设置指向数组的指针
返回值
成功时返回 0,失败时返回 -1

◆ XGBoosterPredictFromColumnar()

int XGBoosterPredictFromColumnar ( BoosterHandle  句柄,
char const *  ,
char const *  配置,
DMatrixHandle  m,
bst_ulong const **  输出形状,
bst_ulong 输出维度,
const float **  输出结果 
)

从 CPU 列式数据(表格)进行原地预测。

注意
如果 booster 配置为在 CUDA 设备上运行,XGBoost 将回退到使用 DMatrix 运行预测,并带有性能警告。
参数
句柄Booster 句柄。
数据有关更多信息,请参见 XGDMatrixCreateFromColumnar
配置有关更多信息,请参见 XGBoosterPredictFromDMatrix。原地预测的其他字段为
  • "missing": float
m可选的(如果不可用则为 NULL)代理 DMatrix 实例,存储元信息。
输出形状有关更多信息,请参见 XGBoosterPredictFromDMatrix
输出维度有关更多信息,请参见 XGBoosterPredictFromDMatrix
输出结果有关更多信息,请参见 XGBoosterPredictFromDMatrix
返回值
成功时返回 0,失败时返回 -1

◆ XGBoosterPredictFromCSR()

int XGBoosterPredictFromCSR ( BoosterHandle  句柄,
char const *  行指针,
char const *  列索引,
char const *  ,
bst_ulong  列数,
char const *  配置,
DMatrixHandle  m,
bst_ulong const **  输出形状,
bst_ulong 输出维度,
const float **  输出结果 
)

从 CPU CSR 矩阵进行原地预测。

注意
如果 booster 配置为在 CUDA 设备上运行,XGBoost 将回退到使用 DMatrix 运行预测,并带有性能警告。
参数
句柄Booster 句柄。
行指针JSON 编码的 CSR 行指针的 array_interface
列索引JSON 编码的 CSR 列索引的 array_interface
JSON 编码的 CSR 值的 array_interface
列数数据中的特征数量。
配置有关更多信息,请参见 XGBoosterPredictFromDMatrix。原地预测的其他字段为
  • "missing": float
m可选的(如果不可用则为 NULL)代理 DMatrix 实例,存储元信息。
输出形状有关更多信息,请参见 XGBoosterPredictFromDMatrix
输出维度有关更多信息,请参见 XGBoosterPredictFromDMatrix
输出结果有关更多信息,请参见 XGBoosterPredictFromDMatrix
返回值
成功时返回 0,失败时返回 -1

◆ XGBoosterPredictFromCudaArray()

int XGBoosterPredictFromCudaArray ( BoosterHandle  句柄,
char const *  ,
char const *  配置,
DMatrixHandle  代理,
bst_ulong const **  输出形状,
bst_ulong 输出维度,
const float **  输出结果 
)

从 CUDA 密集矩阵(Python 中的 cupy)进行原地预测。

注意
如果 booster 配置为在 CPU 上运行,XGBoost 将回退到使用 DMatrix 运行预测,并带有性能警告。
参数
句柄Booster 句柄
JSON 编码的 CUDA 值的 cuda_array_interface
配置有关更多信息,请参见 XGBoosterPredictFromDMatrix。原地预测的其他字段为
  • "missing": float
代理可选的(如果不可用则为 NULL)代理 DMatrix 实例,存储元信息。
输出形状有关更多信息,请参见 XGBoosterPredictFromDMatrix
输出维度有关更多信息,请参见 XGBoosterPredictFromDMatrix
输出结果有关更多信息,请参见 XGBoosterPredictFromDMatrix
返回值
成功时返回 0,失败时返回 -1

◆ XGBoosterPredictFromCudaColumnar()

int XGBoosterPredictFromCudaColumnar ( BoosterHandle  句柄,
char const *  数据,
char const *  配置,
DMatrixHandle  代理,
bst_ulong const **  输出形状,
bst_ulong 输出维度,
const float **  输出结果 
)

从 CUDA 密集数据帧(Python 中的 cuDF)进行原地预测。

注意
如果 booster 配置为在 CPU 上运行,XGBoost 将回退到使用 DMatrix 运行预测,并带有性能警告。
参数
句柄Booster 句柄
数据有关更多信息,请参见 XGDMatrixCreateFromColumnar
配置有关更多信息,请参见 XGBoosterPredictFromDMatrix。原地预测的其他字段为
  • "missing": float
代理可选的(如果不可用则为 NULL)代理 DMatrix 实例,存储元信息。
输出形状有关更多信息,请参见 XGBoosterPredictFromDMatrix
输出维度有关更多信息,请参见 XGBoosterPredictFromDMatrix
输出结果有关更多信息,请参见 XGBoosterPredictFromDMatrix
返回值
成功时返回 0,失败时返回 -1

◆ XGBoosterPredictFromDense()

int XGBoosterPredictFromDense ( BoosterHandle  句柄,
char const *  ,
char const *  配置,
DMatrixHandle  m,
bst_ulong const **  输出形状,
bst_ulong 输出维度,
const float **  输出结果 
)

从 CPU 密集矩阵进行原地预测。

注意
如果 booster 配置为在 CUDA 设备上运行,XGBoost 将回退到使用 DMatrix 运行预测,并带有性能警告。
参数
句柄Booster 句柄。
JSON 编码的 值的 array_interface
配置有关更多信息,请参见 XGBoosterPredictFromDMatrix。原地预测的其他字段为
  • "missing": float
m可选的(如果不可用则为 NULL)代理 DMatrix 实例,存储元信息。
输出形状有关更多信息,请参见 XGBoosterPredictFromDMatrix
输出维度有关更多信息,请参见 XGBoosterPredictFromDMatrix
输出结果有关更多信息,请参见 XGBoosterPredictFromDMatrix
返回值
成功时返回 0,失败时返回 -1
示例
inference.c.

◆ XGBoosterPredictFromDMatrix()

int XGBoosterPredictFromDMatrix ( BoosterHandle  句柄,
DMatrixHandle  dmat,
char const *  配置,
bst_ulong const **  输出形状,
bst_ulong 输出维度,
float const **  输出结果 
)

从 DMatrix 进行预测,取代 XGBoosterPredict

参数
句柄Booster 句柄
dmatDMatrix 句柄
配置以 JSON 格式编码的预测配置字符串,JSON 对象中包含以下可用字段:

"type": [0, 6]

  • 0: 常规预测
  • 1: 输出边距
  • 2: 预测贡献
  • 3: 预测近似贡献
  • 4: 预测特征交互
  • 5: 预测近似特征交互
  • 6: 预测叶子 "training": bool 预测函数是否作为训练循环的一部分使用。不用于原地预测

预测可以在两种场景下运行:

  1. 给定数据矩阵 X,从模型获取预测 y_pred。
  2. 获取用于计算梯度的预测。例如,DART booster 在训练期间执行 dropout,由于树被丢弃,预测结果将与正常推理步骤获得的结果不同。第一种场景设置 training=false。第二种场景设置 training=true。第二种场景适用于您定义自定义目标函数时。"iteration_begin": int 预测的起始迭代次数。"iteration_end": int 预测的结束迭代次数。设为 0 则为模型树的大小(所有树)。"strict_shape": bool 是否应该以更严格的规则重塑输出形状。如果设为 true,常规/边距/贡献/交互预测将输出一致的形状,忽略多分类模型的使用,叶子预测将输出 4 维数组,表示:(样本数,迭代次数,类别数,森林中的树数量)

运行带有严格输出形状(对于 softprob 为 2 维,对于其他为 1 维)的常规预测的 JSON 输入示例。

{
"type": 0,
"training": false,
"iteration_begin": 0,
"iteration_end": 0,
"strict_shape": true
}
参数
输出形状输出预测的形状(使用前请复制)。
输出维度输出预测的维度。
输出结果存储预测值的缓冲区(使用前请复制)。
返回值
成功时返回 0,失败时返回 -1
另请参见
XGBoosterPredictFromDense XGBoosterPredictFromCSR XGBoosterPredictFromCudaArray XGBoosterPredictFromCudaColumnar
示例
c-api-demo.c,以及 inference.c