xgboost
Modules | Functions
Booster

The Booster class is the gradient-boosted model for XGBoost. More...

Collaboration diagram for Booster:

Modules

 Prediction
 These functions are used for running prediction and explanation algorithms.
 
 Serialization
 There are multiple ways to serialize a Booster object depending on the use case.
 

Functions

int XGBoosterCreate (const DMatrixHandle dmats[], bst_ulong len, BoosterHandle *out)
 create xgboost learner More...
 
int XGBoosterFree (BoosterHandle handle)
 free obj in handle More...
 
int XGBoosterSlice (BoosterHandle handle, int begin_layer, int end_layer, int step, BoosterHandle *out)
 Slice a model using boosting index. The slice m:n indicates taking all trees that were fit during the boosting rounds m, (m+1), (m+2), ..., (n-1). More...
 
int XGBoosterBoostedRounds (BoosterHandle handle, int *out)
 Get number of boosted rounds from gradient booster. When process_type is update, this number might drop due to removed tree. More...
 
int XGBoosterSetParam (BoosterHandle handle, const char *name, const char *value)
 set parameters More...
 
int XGBoosterGetNumFeature (BoosterHandle handle, bst_ulong *out)
 get number of features More...
 
int XGBoosterUpdateOneIter (BoosterHandle handle, int iter, DMatrixHandle dtrain)
 update the model in one round using dtrain More...
 
int XGBoosterBoostOneIter (BoosterHandle handle, DMatrixHandle dtrain, float *grad, float *hess, bst_ulong len)
 
int XGBoosterTrainOneIter (BoosterHandle handle, DMatrixHandle dtrain, int iter, char const *grad, char const *hess)
 Update a model with gradient and Hessian. This is used for training with a custom objective function. More...
 
int XGBoosterEvalOneIter (BoosterHandle handle, int iter, DMatrixHandle dmats[], const char *evnames[], bst_ulong len, const char **out_result)
 get evaluation statistics for xgboost More...
 
int XGBoosterDumpModel (BoosterHandle handle, const char *fmap, int with_stats, bst_ulong *out_len, const char ***out_dump_array)
 dump model, return array of strings representing model dump More...
 
int XGBoosterDumpModelEx (BoosterHandle handle, const char *fmap, int with_stats, const char *format, bst_ulong *out_len, const char ***out_dump_array)
 dump model, return array of strings representing model dump More...
 
int XGBoosterDumpModelWithFeatures (BoosterHandle handle, int fnum, const char **fname, const char **ftype, int with_stats, bst_ulong *out_len, const char ***out_models)
 dump model, return array of strings representing model dump More...
 
int XGBoosterDumpModelExWithFeatures (BoosterHandle handle, int fnum, const char **fname, const char **ftype, int with_stats, const char *format, bst_ulong *out_len, const char ***out_models)
 dump model, return array of strings representing model dump More...
 
int XGBoosterGetAttr (BoosterHandle handle, const char *key, const char **out, int *success)
 Get string attribute from Booster. More...
 
int XGBoosterSetAttr (BoosterHandle handle, const char *key, const char *value)
 Set or delete string attribute. More...
 
int XGBoosterGetAttrNames (BoosterHandle handle, bst_ulong *out_len, const char ***out)
 Get the names of all attribute from Booster. More...
 
int XGBoosterSetStrFeatureInfo (BoosterHandle handle, const char *field, const char **features, const bst_ulong size)
 Set string encoded feature info in Booster, similar to the feature info in DMatrix. More...
 
int XGBoosterGetStrFeatureInfo (BoosterHandle handle, const char *field, bst_ulong *len, const char ***out_features)
 Get string encoded feature info from Booster, similar to feature info in DMatrix. More...
 
int XGBoosterFeatureScore (BoosterHandle handle, const char *config, bst_ulong *out_n_features, char const ***out_features, bst_ulong *out_dim, bst_ulong const **out_shape, float const **out_scores)
 Calculate feature scores for tree models. When used on linear model, only the weight importance type is defined, and output scores is a row major matrix with shape [n_features, n_classes] for multi-class model. For tree model, out_n_feature is always equal to out_n_scores and has multiple definitions of importance type. More...
 

Detailed Description

The Booster class is the gradient-boosted model for XGBoost.

Function Documentation

◆ XGBoosterBoostedRounds()

int XGBoosterBoostedRounds ( BoosterHandle  handle,
int *  out 
)

Get number of boosted rounds from gradient booster. When process_type is update, this number might drop due to removed tree.

Parameters
handleHandle to booster.
outPointer to output integer.
Returns
0 when success, -1 when failure happens

◆ XGBoosterBoostOneIter()

int XGBoosterBoostOneIter ( BoosterHandle  handle,
DMatrixHandle  dtrain,
float *  grad,
float *  hess,
bst_ulong  len 
)
Deprecated:
since 2.1.0

◆ XGBoosterCreate()

int XGBoosterCreate ( const DMatrixHandle  dmats[],
bst_ulong  len,
BoosterHandle out 
)

create xgboost learner

Parameters
dmatsmatrices that are set to be cached
lenlength of dmats
outhandle to the result booster
Returns
0 when success, -1 when failure happens
Examples
c-api-demo.c, external_memory.c, and inference.c.

◆ XGBoosterDumpModel()

int XGBoosterDumpModel ( BoosterHandle  handle,
const char *  fmap,
int  with_stats,
bst_ulong out_len,
const char ***  out_dump_array 
)

dump model, return array of strings representing model dump

Parameters
handlehandle
fmapname to fmap can be empty string
with_statswhether to dump with statistics
out_lenlength of output array
out_dump_arraypointer to hold representing dump of each model
Returns
0 when success, -1 when failure happens

◆ XGBoosterDumpModelEx()

int XGBoosterDumpModelEx ( BoosterHandle  handle,
const char *  fmap,
int  with_stats,
const char *  format,
bst_ulong out_len,
const char ***  out_dump_array 
)

dump model, return array of strings representing model dump

Parameters
handlehandle
fmapname to fmap can be empty string
with_statswhether to dump with statistics
formatthe format to dump the model in
out_lenlength of output array
out_dump_arraypointer to hold representing dump of each model
Returns
0 when success, -1 when failure happens

◆ XGBoosterDumpModelExWithFeatures()

int XGBoosterDumpModelExWithFeatures ( BoosterHandle  handle,
int  fnum,
const char **  fname,
const char **  ftype,
int  with_stats,
const char *  format,
bst_ulong out_len,
const char ***  out_models 
)

dump model, return array of strings representing model dump

Parameters
handlehandle
fnumnumber of features
fnamenames of features
ftypetypes of features
with_statswhether to dump with statistics
formatthe format to dump the model in
out_lenlength of output array
out_modelspointer to hold representing dump of each model
Returns
0 when success, -1 when failure happens

◆ XGBoosterDumpModelWithFeatures()

int XGBoosterDumpModelWithFeatures ( BoosterHandle  handle,
int  fnum,
const char **  fname,
const char **  ftype,
int  with_stats,
bst_ulong out_len,
const char ***  out_models 
)

dump model, return array of strings representing model dump

Parameters
handlehandle
fnumnumber of features
fnamenames of features
ftypetypes of features
with_statswhether to dump with statistics
out_lenlength of output array
out_modelspointer to hold representing dump of each model
Returns
0 when success, -1 when failure happens

◆ XGBoosterEvalOneIter()

int XGBoosterEvalOneIter ( BoosterHandle  handle,
int  iter,
DMatrixHandle  dmats[],
const char *  evnames[],
bst_ulong  len,
const char **  out_result 
)

get evaluation statistics for xgboost

Parameters
handlehandle
itercurrent iteration rounds
dmatspointers to data to be evaluated
evnamespointers to names of each data
lenlength of dmats
out_resultthe string containing evaluation statistics
Returns
0 when success, -1 when failure happens
Examples
c-api-demo.c, and external_memory.c.

◆ XGBoosterFeatureScore()

int XGBoosterFeatureScore ( BoosterHandle  handle,
const char *  config,
bst_ulong out_n_features,
char const ***  out_features,
bst_ulong out_dim,
bst_ulong const **  out_shape,
float const **  out_scores 
)

Calculate feature scores for tree models. When used on linear model, only the weight importance type is defined, and output scores is a row major matrix with shape [n_features, n_classes] for multi-class model. For tree model, out_n_feature is always equal to out_n_scores and has multiple definitions of importance type.

Parameters
handleAn instance of Booster
configParameters for computing scores encoded as JSON. Accepted JSON keys are:
  • importance_type: A JSON string with following possible values:
    • 'weight': the number of times a feature is used to split the data across all trees.
    • 'gain': the average gain across all splits the feature is used in.
    • 'cover': the average coverage across all splits the feature is used in.
    • 'total_gain': the total gain across all splits the feature is used in.
    • 'total_cover': the total coverage across all splits the feature is used in.
  • feature_map: An optional JSON string with URI or path to the feature map file.
  • feature_names: An optional JSON array with string names for each feature.
out_n_featuresLength of output feature names.
out_featuresAn array of string as feature names, ordered the same as output scores.
out_dimDimension of output feature scores.
out_shapeShape of output feature scores with length of out_dim.
out_scoresAn array of floating point as feature scores with shape of out_shape.
Returns
0 when success, -1 when failure happens

◆ XGBoosterFree()

int XGBoosterFree ( BoosterHandle  handle)

free obj in handle

Parameters
handlehandle to be freed
Returns
0 when success, -1 when failure happens
Examples
c-api-demo.c, external_memory.c, and inference.c.

◆ XGBoosterGetAttr()

int XGBoosterGetAttr ( BoosterHandle  handle,
const char *  key,
const char **  out,
int *  success 
)

Get string attribute from Booster.

Parameters
handlehandle
keyThe key of the attribute.
outThe result attribute, can be NULL if the attribute do not exist.
successWhether the result is contained in out.
Returns
0 when success, -1 when failure happens

◆ XGBoosterGetAttrNames()

int XGBoosterGetAttrNames ( BoosterHandle  handle,
bst_ulong out_len,
const char ***  out 
)

Get the names of all attribute from Booster.

Parameters
handlehandle
out_lenthe argument to hold the output length
outpointer to hold the output attribute stings
Returns
0 when success, -1 when failure happens

◆ XGBoosterGetNumFeature()

int XGBoosterGetNumFeature ( BoosterHandle  handle,
bst_ulong out 
)

get number of features

Parameters
handleHandle to booster.
outnumber of features
Returns
0 when success, -1 when failure happens
Examples
c-api-demo.c.

◆ XGBoosterGetStrFeatureInfo()

int XGBoosterGetStrFeatureInfo ( BoosterHandle  handle,
const char *  field,
bst_ulong len,
const char ***  out_features 
)

Get string encoded feature info from Booster, similar to feature info in DMatrix.

Accepted fields are:

  • feature_name
  • feature_type

Caller is responsible for copying out the data, before next call to any API function of XGBoost.

Parameters
handleAn instance of Booster
fieldField name
lenSize of output pointer features (number of strings returned).
out_featuresAddress of a pointer to array of strings. Result is stored in thread local memory.
Returns
0 when success, -1 when failure happens

◆ XGBoosterSetAttr()

int XGBoosterSetAttr ( BoosterHandle  handle,
const char *  key,
const char *  value 
)

Set or delete string attribute.

Parameters
handlehandle
keyThe key of the attribute.
valueThe value to be saved. If nullptr, the attribute would be deleted.
Returns
0 when success, -1 when failure happens

◆ XGBoosterSetParam()

int XGBoosterSetParam ( BoosterHandle  handle,
const char *  name,
const char *  value 
)

set parameters

Parameters
handlehandle
nameparameter name
valuevalue of parameter
Returns
0 when success, -1 when failure happens
Examples
c-api-demo.c, and external_memory.c.

◆ XGBoosterSetStrFeatureInfo()

int XGBoosterSetStrFeatureInfo ( BoosterHandle  handle,
const char *  field,
const char **  features,
const bst_ulong  size 
)

Set string encoded feature info in Booster, similar to the feature info in DMatrix.

Accepted fields are:

  • feature_name
  • feature_type
Parameters
handleAn instance of Booster
fieldField name
featuresPointer to array of strings.
sizeSize of features pointer (number of strings passed in).
Returns
0 when success, -1 when failure happens

◆ XGBoosterSlice()

int XGBoosterSlice ( BoosterHandle  handle,
int  begin_layer,
int  end_layer,
int  step,
BoosterHandle out 
)

Slice a model using boosting index. The slice m:n indicates taking all trees that were fit during the boosting rounds m, (m+1), (m+2), ..., (n-1).

Parameters
handleBooster to be sliced.
begin_layerstart of the slice
end_layerend of the slice; end_layer=0 is equivalent to end_layer=num_boost_round
stepstep size of the slice
outSliced booster.
Returns
0 when success, -1 when failure happens, -2 when index is out of bound.

◆ XGBoosterTrainOneIter()

int XGBoosterTrainOneIter ( BoosterHandle  handle,
DMatrixHandle  dtrain,
int  iter,
char const *  grad,
char const *  hess 
)

Update a model with gradient and Hessian. This is used for training with a custom objective function.

Since
2.0.0
Parameters
handlehandle
dtrainThe training data.
iterThe current iteration round. When training continuation is used, the count should restart.
gradJson encoded __(cuda)_array_interface__ for gradient.
hessJson encoded __(cuda)_array_interface__ for Hessian.
Returns
0 when success, -1 when failure happens

◆ XGBoosterUpdateOneIter()

int XGBoosterUpdateOneIter ( BoosterHandle  handle,
int  iter,
DMatrixHandle  dtrain 
)

update the model in one round using dtrain

Parameters
handlehandle
itercurrent iteration rounds
dtraintraining data
Returns
0 when success, -1 when failure happens
Examples
c-api-demo.c, external_memory.c, and inference.c.