xgboost
c_api.h
Go to the documentation of this file.
1 
7 #ifndef XGBOOST_C_API_H_
8 #define XGBOOST_C_API_H_
9 
10 #ifdef __cplusplus
11 #define XGB_EXTERN_C extern "C"
12 #include <cstdio>
13 #include <cstdint>
14 #else
15 #define XGB_EXTERN_C
16 #include <stdio.h>
17 #include <stdint.h>
18 #endif // __cplusplus
19 
20 #if defined(_MSC_VER) || defined(_WIN32)
21 #define XGB_DLL XGB_EXTERN_C __declspec(dllexport)
22 #else
23 #define XGB_DLL XGB_EXTERN_C
24 #endif // defined(_MSC_VER) || defined(_WIN32)
25 
26 // manually define unsigned long
27 typedef uint64_t bst_ulong; // NOLINT(*)
28 
29 
31 typedef void *DMatrixHandle; // NOLINT(*)
33 typedef void *BoosterHandle; // NOLINT(*)
35 typedef void *DataIterHandle; // NOLINT(*)
37 typedef void *DataHolderHandle; // NOLINT(*)
38 
40 typedef struct { // NOLINT(*)
42  size_t size;
44 #ifdef __APPLE__
45  /* Necessary as Java on MacOS defines jlong as long int
46  * and gcc defines int64_t as long long int. */
47  long* offset; // NOLINT(*)
48 #else
49  int64_t* offset; // NOLINT(*)
50 #endif // __APPLE__
51 
52  float* label;
54  float* weight;
56  int* index;
58  float* value;
60 
61 
67 XGB_EXTERN_C typedef int XGBCallbackSetData( // NOLINT(*)
68  DataHolderHandle handle, XGBoostBatchCSR batch);
69 
81 XGB_EXTERN_C typedef int XGBCallbackDataIterNext( // NOLINT(*)
82  DataIterHandle data_handle, XGBCallbackSetData *set_function,
83  DataHolderHandle set_function_handle);
84 
95 XGB_DLL const char *XGBGetLastError(void);
96 
104 XGB_DLL int XGBRegisterLogCallback(void (*callback)(const char*));
105 
113 XGB_DLL int XGDMatrixCreateFromFile(const char *fname,
114  int silent,
115  DMatrixHandle *out);
116 
126  DataIterHandle data_handle,
127  XGBCallbackDataIterNext* callback,
128  const char* cache_info,
129  DMatrixHandle *out);
130 
142 XGB_DLL int XGDMatrixCreateFromCSREx(const size_t* indptr,
143  const unsigned* indices,
144  const float* data,
145  size_t nindptr,
146  size_t nelem,
147  size_t num_col,
148  DMatrixHandle* out);
160 XGB_DLL int XGDMatrixCreateFromCSCEx(const size_t* col_ptr,
161  const unsigned* indices,
162  const float* data,
163  size_t nindptr,
164  size_t nelem,
165  size_t num_row,
166  DMatrixHandle* out);
167 
177 XGB_DLL int XGDMatrixCreateFromMat(const float *data,
178  bst_ulong nrow,
179  bst_ulong ncol,
180  float missing,
181  DMatrixHandle *out);
192 XGB_DLL int XGDMatrixCreateFromMat_omp(const float *data, // NOLINT
193  bst_ulong nrow, bst_ulong ncol,
194  float missing, DMatrixHandle *out,
195  int nthread);
206 XGB_DLL int XGDMatrixCreateFromDT(void** data,
207  const char ** feature_stypes,
208  bst_ulong nrow,
209  bst_ulong ncol,
210  DMatrixHandle* out,
211  int nthread);
221  const int *idxset,
222  bst_ulong len,
223  DMatrixHandle *out);
237  const char *fname, int silent);
247  const char *field,
248  const float *array,
249  bst_ulong len);
259  const char *field,
260  const unsigned *array,
261  bst_ulong len);
270  const unsigned *group,
271  bst_ulong len);
281  const char *field,
282  bst_ulong* out_len,
283  const float **out_dptr);
293  const char *field,
294  bst_ulong* out_len,
295  const unsigned **out_dptr);
303  bst_ulong *out);
311  bst_ulong *out);
312 // --- start XGBoost class
320 XGB_DLL int XGBoosterCreate(const DMatrixHandle dmats[],
321  bst_ulong len,
322  BoosterHandle *out);
329 
338  const char *name,
339  const char *value);
340 
349  int iter,
350  DMatrixHandle dtrain);
362  DMatrixHandle dtrain,
363  float *grad,
364  float *hess,
365  bst_ulong len);
377  int iter,
378  DMatrixHandle dmats[],
379  const char *evnames[],
380  bst_ulong len,
381  const char **out_result);
398  DMatrixHandle dmat,
399  int option_mask,
400  unsigned ntree_limit,
401  bst_ulong *out_len,
402  const float **out_result);
403 
411  const char *fname);
419  const char *fname);
428  const void *buf,
429  bst_ulong len);
439  bst_ulong *out_len,
440  const char **out_dptr);
451  const char *fmap,
452  int with_stats,
453  bst_ulong *out_len,
454  const char ***out_dump_array);
455 
467  const char *fmap,
468  int with_stats,
469  const char *format,
470  bst_ulong *out_len,
471  const char ***out_dump_array);
472 
485  int fnum,
486  const char **fname,
487  const char **ftype,
488  int with_stats,
489  bst_ulong *out_len,
490  const char ***out_models);
491 
505  int fnum,
506  const char **fname,
507  const char **ftype,
508  int with_stats,
509  const char *format,
510  bst_ulong *out_len,
511  const char ***out_models);
512 
522  const char* key,
523  const char** out,
524  int *success);
535  const char* key,
536  const char* value);
545  bst_ulong* out_len,
546  const char*** out);
547 
548 // --- Distributed training API----
549 // NOTE: functions in rabit/c_api.h will be also available in libxgboost.so
558  BoosterHandle handle,
559  int* version);
560 
567 
568 #endif // XGBOOST_C_API_H_
void * BoosterHandle
handle to Booster
Definition: c_api.h:33
XGB_DLL int XGBoosterSetParam(BoosterHandle handle, const char *name, const char *value)
set parameters
XGB_DLL int XGBoosterGetModelRaw(BoosterHandle handle, bst_ulong *out_len, const char **out_dptr)
save model into binary raw bytes, return header of the array user must copy the result out...
#define XGB_DLL
Definition: c_api.h:23
int64_t * offset
row pointer to the rows in the data
Definition: c_api.h:49
void * DataIterHandle
handle to a data iterator
Definition: c_api.h:35
XGB_DLL 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
XGB_DLL 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
XGB_DLL int XGBoosterEvalOneIter(BoosterHandle handle, int iter, DMatrixHandle dmats[], const char *evnames[], bst_ulong len, const char **out_result)
get evaluation statistics for xgboost
XGB_DLL int XGDMatrixCreateFromDataIter(DataIterHandle data_handle, XGBCallbackDataIterNext *callback, const char *cache_info, DMatrixHandle *out)
Create a DMatrix from a data iterator.
void * DMatrixHandle
handle to DMatrix
Definition: c_api.h:31
XGB_DLL int XGBoosterFree(BoosterHandle handle)
free obj in handle
XGB_DLL int XGDMatrixSetUIntInfo(DMatrixHandle handle, const char *field, const unsigned *array, bst_ulong len)
set uint32 vector to a content in info
Mini batch used in XGBoost Data Iteration.
Definition: c_api.h:40
XGB_DLL 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
XGB_DLL int XGDMatrixFree(DMatrixHandle handle)
free space in data matrix
XGB_DLL int XGDMatrixSetGroup(DMatrixHandle handle, const unsigned *group, bst_ulong len)
set label of the training matrix
uint64_t bst_ulong
Definition: c_api.h:27
XGB_DLL int XGDMatrixCreateFromDT(void **data, const char **feature_stypes, bst_ulong nrow, bst_ulong ncol, DMatrixHandle *out, int nthread)
create matrix content from python data table
XGB_DLL int XGBoosterPredict(BoosterHandle handle, DMatrixHandle dmat, int option_mask, unsigned ntree_limit, bst_ulong *out_len, const float **out_result)
make prediction based on dmat
size_t size
number of rows in the minibatch
Definition: c_api.h:42
void * DataHolderHandle
handle to a internal data holder.
Definition: c_api.h:37
XGB_EXTERN_C typedef int XGBCallbackSetData(DataHolderHandle handle, XGBoostBatchCSR batch)
Callback to set the data to handle,.
XGB_DLL int XGDMatrixSetFloatInfo(DMatrixHandle handle, const char *field, const float *array, bst_ulong len)
set float vector to a content in info
#define XGB_EXTERN_C
Definition: c_api.h:15
XGB_DLL int XGBoosterLoadModel(BoosterHandle handle, const char *fname)
load model from existing file
XGB_DLL int XGDMatrixCreateFromFile(const char *fname, int silent, DMatrixHandle *out)
load a data matrix
XGB_DLL int XGBoosterUpdateOneIter(BoosterHandle handle, int iter, DMatrixHandle dtrain)
update the model in one round using dtrain
XGB_DLL int XGBoosterGetAttr(BoosterHandle handle, const char *key, const char **out, int *success)
Get string attribute from Booster.
XGB_DLL int XGDMatrixCreateFromCSREx(const size_t *indptr, const unsigned *indices, const float *data, size_t nindptr, size_t nelem, size_t num_col, DMatrixHandle *out)
create a matrix content from CSR format
float * weight
weight of each instance, can be NULL
Definition: c_api.h:54
XGB_DLL int XGDMatrixSaveBinary(DMatrixHandle handle, const char *fname, int silent)
load a data matrix into binary file
XGB_DLL int XGDMatrixCreateFromCSCEx(const size_t *col_ptr, const unsigned *indices, const float *data, size_t nindptr, size_t nelem, size_t num_row, DMatrixHandle *out)
create a matrix content from CSC format
XGB_DLL int XGDMatrixGetUIntInfo(const DMatrixHandle handle, const char *field, bst_ulong *out_len, const unsigned **out_dptr)
get uint32 info vector from matrix
XGB_DLL const char * XGBGetLastError(void)
get string message of the last error
XGB_DLL int XGBoosterSaveModel(BoosterHandle handle, const char *fname)
save model into existing file
XGB_DLL int XGBoosterSetAttr(BoosterHandle handle, const char *key, const char *value)
Set or delete string attribute.
XGB_DLL int XGBoosterCreate(const DMatrixHandle dmats[], bst_ulong len, BoosterHandle *out)
create xgboost learner
float * label
labels of each instance
Definition: c_api.h:52
XGB_DLL int XGDMatrixSliceDMatrix(DMatrixHandle handle, const int *idxset, bst_ulong len, DMatrixHandle *out)
create a new dmatrix from sliced content of existing matrix
float * value
feature values
Definition: c_api.h:58
XGB_DLL int XGDMatrixGetFloatInfo(const DMatrixHandle handle, const char *field, bst_ulong *out_len, const float **out_dptr)
get float info vector from matrix
XGB_DLL int XGDMatrixCreateFromMat(const float *data, bst_ulong nrow, bst_ulong ncol, float missing, DMatrixHandle *out)
create matrix content from dense matrix
XGB_DLL int XGBRegisterLogCallback(void(*callback)(const char *))
register callback function for LOG(INFO) messages – helpful messages that are not errors...
XGB_DLL 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
XGB_DLL int XGBoosterLoadRabitCheckpoint(BoosterHandle handle, int *version)
Initialize the booster from rabit checkpoint. This is used in distributed training API...
XGB_DLL int XGDMatrixNumRow(DMatrixHandle handle, bst_ulong *out)
get number of rows.
XGB_DLL int XGBoosterLoadModelFromBuffer(BoosterHandle handle, const void *buf, bst_ulong len)
load model from in memory buffer
XGB_DLL int XGDMatrixNumCol(DMatrixHandle handle, bst_ulong *out)
get number of columns
int * index
feature index
Definition: c_api.h:56
XGB_EXTERN_C typedef int XGBCallbackDataIterNext(DataIterHandle data_handle, XGBCallbackSetData *set_function, DataHolderHandle set_function_handle)
The data reading callback function. The iterator will be able to give subset of batch in the data...
XGB_DLL int XGBoosterSaveRabitCheckpoint(BoosterHandle handle)
Save the current checkpoint to rabit.
XGB_DLL int XGBoosterGetAttrNames(BoosterHandle handle, bst_ulong *out_len, const char ***out)
Get the names of all attribute from Booster.
XGB_DLL int XGDMatrixCreateFromMat_omp(const float *data, bst_ulong nrow, bst_ulong ncol, float missing, DMatrixHandle *out, int nthread)
create matrix content from dense matrix
XGB_DLL int XGBoosterBoostOneIter(BoosterHandle handle, DMatrixHandle dtrain, float *grad, float *hess, bst_ulong len)
update the model, by directly specify gradient and second order gradient, this can be used to replace...