#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define safe_xgboost(err) \
if ((err) != 0) { \
fprintf(stderr, "%s:%d: error in %s: %s\n", __FILE__, __LINE__, #err, \
XGBGetLastError()); \
exit(1); \
}
#define safe_malloc(ptr) \
if ((ptr) == NULL) { \
fprintf(stderr, "%s:%d: Failed to allocate memory.\n", __FILE__, \
__LINE__); \
exit(1); \
}
#define N_SAMPLES 128
#define N_FEATURES 16
typedef BoosterHandle Booster;
float *data;
size_t shape[2];
char _array_intrerface[256];
};
void Matrix_Create(
Matrix *self,
float const *data,
size_t n_samples,
size_t n_features) {
if (self == NULL) {
fprintf(stderr, "Invalid pointer to %s\n", __func__);
exit(-1);
}
safe_malloc(*self);
(*self)->data = (float *)malloc(n_samples * n_features * sizeof(float));
safe_malloc((*self)->data);
(*self)->shape[0] = n_samples;
(*self)->shape[1] = n_features;
if (data != NULL) {
memcpy((*self)->data, data,
(*self)->shape[0] * (*self)->shape[1] * sizeof(float));
}
}
void Matrix_Random(
Matrix *self,
size_t n_samples,
size_t n_features) {
Matrix_Create(self, NULL, n_samples, n_features);
for (size_t i = 0; i < n_samples * n_features; ++i) {
float x = (float)rand() / (float)(RAND_MAX);
(*self)->data[i] = x;
}
}
char const *Matrix_ArrayInterface(
Matrix self) {
char const template[] = "{\"data\": [%lu, true], \"shape\": [%lu, %lu], "
"\"typestr\": \"<f4\", \"version\": 3}";
memset(self->
_array_intrerface,
'\0',
sizeof(self->_array_intrerface));
sprintf(self->_array_intrerface,
template, (
size_t)self->
data, self->
shape[0],
self->shape[1]);
return self->_array_intrerface;
}
size_t Matrix_NSamples(
Matrix self) {
return self->shape[0]; }
size_t Matrix_NFeatures(
Matrix self) {
return self->shape[1]; }
float Matrix_At(
Matrix self,
size_t i,
size_t j) {
return self->data[i * self->shape[1] + j];
}
void Matrix_Print(
Matrix self) {
for (size_t i = 0; i < Matrix_NSamples(self); i++) {
for (size_t j = 0; j < Matrix_NFeatures(self); ++j) {
printf("%f, ", Matrix_At(self, i, j));
}
}
printf("\n");
}
void Matrix_Free(
Matrix self) {
if (self != NULL) {
if (self->data != NULL) {
self->shape[0] = 0;
self->shape[1] = 0;
free(self->data);
self->data = NULL;
}
free(self);
}
}
int main() {
Matrix_Random(&X, N_SAMPLES, N_FEATURES);
Matrix_Random(&y, N_SAMPLES, 1);
char const *X_interface = Matrix_ArrayInterface(X);
char config[] = "{\"nthread\": 16, \"missing\": NaN}";
DMatrix Xy;
DMatrix cache[] = {Xy};
Booster booster;
size_t n_rounds = 10;
for (size_t i = 0; i < n_rounds; ++i) {
}
{
char const config[] =
"{\"training\": false, \"type\": 0, "
"\"iteration_begin\": 0, \"iteration_end\": 0, \"strict_shape\": true}";
uint64_t const *out_shape;
uint64_t out_dim;
float const *out_results;
&out_dim, &out_results));
if (out_dim != 2 || out_shape[0] != N_SAMPLES || out_shape[1] != 1) {
fprintf(stderr, "Regression model should output prediction as vector.");
exit(-1);
}
Matrix_Create(&predt, out_results, out_shape[0], out_shape[1]);
printf("Results from prediction\n");
Matrix_Print(predt);
Matrix_Free(predt);
}
{
char const config[] = "{\"type\": 0, \"iteration_begin\": 0, "
"\"iteration_end\": 0, \"strict_shape\": true, "
"\"cache_id\": 0, \"missing\": NaN}";
uint64_t const *out_shape;
uint64_t out_dim;
float const *out_results;
char const *X_interface = Matrix_ArrayInterface(X);
&out_shape, &out_dim, &out_results));
if (out_dim != 2 || out_shape[0] != N_SAMPLES || out_shape[1] != 1) {
fprintf(stderr,
"Regression model should output prediction as vector, %lu, %lu",
out_dim, out_shape[0]);
exit(-1);
}
Matrix_Create(&predt, out_results, out_shape[0], out_shape[1]);
printf("Results from inplace prediction\n");
Matrix_Print(predt);
Matrix_Free(predt);
}
Matrix_Free(X);
Matrix_Free(y);
return 0;
}
XGB_DLL int XGBoosterUpdateOneIter(BoosterHandle handle, int iter, DMatrixHandle dtrain)
update the model in one round using dtrain
Definition c_api.cc:960
XGB_DLL int XGBoosterFree(BoosterHandle handle)
free obj in handle
Definition c_api.cc:896
XGB_DLL int XGBoosterCreate(const DMatrixHandle dmats[], bst_ulong len, BoosterHandle *out)
create xgboost learner
Definition c_api.cc:882
XGB_DLL int XGDMatrixCreateFromDense(char const *data, char const *config, DMatrixHandle *out)
Create a matrix from dense array.
Definition c_api.cc:451
XGB_DLL int XGDMatrixSetDenseInfo(DMatrixHandle handle, const char *field, void const *data, bst_ulong size, int type)
Set meta info from dense matrix. Valid field names are:
Definition c_api.cc:672
XGB_DLL int XGDMatrixFree(DMatrixHandle handle)
free space in data matrix
Definition c_api.cc:585
void * DMatrixHandle
handle to DMatrix
Definition c_api.h:49
XGB_DLL int XGBoosterPredictFromDMatrix(BoosterHandle handle, DMatrixHandle dmat, char const *config, bst_ulong const **out_shape, bst_ulong *out_dim, float const **out_result)
Make prediction from DMatrix, replacing XGBoosterPredict.
Definition c_api.cc:1051
XGB_DLL 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)
Inplace prediction from CPU dense matrix.
Definition c_api.cc:1149
XGB_DLL int XGBoosterLoadModel(BoosterHandle handle, const char *fname)
Load model from existing file.
Definition c_api.cc:1212
XGB_DLL int XGBoosterSaveModel(BoosterHandle handle, const char *fname)
Save model into existing file.
Definition c_api.cc:1246
Definition inference.c:32
Copyright 2015~2023 by XGBoost Contributors.