Debian Codegraph @

src/xgboost_R.h

raw symbols SCIP
1 /*!
2 * Copyright 2014-2022 by XGBoost Contributors
3 * \file xgboost_R.h
4 * \author Tianqi Chen
5 * \brief R wrapper of xgboost
6 */
7 #ifndef XGBOOST_R_H_ // NOLINT(*)
8 #define XGBOOST_R_H_ // NOLINT(*)
9
10
11 #ifndef R_NO_REMAP
12 # define R_NO_REMAP
13 #endif
14 #include <R.h>
15 #include <Rinternals.h>
16 #include <R_ext/Altrep.h>
17 #include <Rmath.h>
18
19 #include <xgboost/c_api.h>
20
21 /*!
22 * \brief check whether a handle is NULL
23 * \param handle
24 * \return whether it is null ptr
25 */
26 XGB_DLL SEXP XGCheckNullPtr_R(SEXP handle);
27
28 /*!
29 * \brief set the names of the dimensions of an array in-place
30 * \param arr
31 * \param dim_names names for the dimensions to set
32 * \return NULL value
33 */
34 XGB_DLL SEXP XGSetArrayDimNamesInplace_R(SEXP arr, SEXP dim_names);
35
36 /*!
37 * \brief set the names of a vector in-place
38 * \param arr
39 * \param names names for the dimensions to set
40 * \return NULL value
41 */
42 XGB_DLL SEXP XGSetVectorNamesInplace_R(SEXP arr, SEXP names);
43
44 /*!
45 * \brief Set global configuration
46 * \param json_str a JSON string representing the list of key-value pairs
47 * \return R_NilValue
48 */
49 XGB_DLL SEXP XGBSetGlobalConfig_R(SEXP json_str);
50
51 /*!
52 * \brief Get global configuration
53 * \return JSON string
54 */
55 XGB_DLL SEXP XGBGetGlobalConfig_R();
56
57 /*!
58 * \brief load a data matrix from URI
59 * \param uri URI to the source file to read data from
60 * \param silent whether print messages
61 * \param Data split mode (0=rows, 1=columns)
62 * \return a loaded data matrix
63 */
64 XGB_DLL SEXP XGDMatrixCreateFromURI_R(SEXP uri, SEXP silent, SEXP data_split_mode);
65
66 /*!
67 * \brief create matrix content from dense matrix
68 * This assumes the matrix is stored in column major format
69 * \param data R Matrix object
70 * \param missing which value to represent missing value
71 * \param n_threads Number of threads used to construct DMatrix from dense matrix.
72 * \return created dmatrix
73 */
74 XGB_DLL SEXP XGDMatrixCreateFromMat_R(SEXP mat,
75 SEXP missing,
76 SEXP n_threads);
77
78 /**
79 * @brief Create matrix content from a data frame.
80 * @param data R data.frame object
81 * @param missing which value to represent missing value
82 * @param n_threads Number of threads used to construct DMatrix from dense matrix.
83 * @return created dmatrix
84 */
85 XGB_DLL SEXP XGDMatrixCreateFromDF_R(SEXP df, SEXP missing, SEXP n_threads);
86
87 /*!
88 * \brief create a matrix content from CSC format
89 * \param indptr pointer to column headers
90 * \param indices row indices
91 * \param data content of the data
92 * \param num_row numer of rows (when it's set to 0, then guess from data)
93 * \param missing which value to represent missing value
94 * \param n_threads Number of threads used to construct DMatrix from csc matrix.
95 * \return created dmatrix
96 */
97 XGB_DLL SEXP XGDMatrixCreateFromCSC_R(SEXP indptr, SEXP indices, SEXP data, SEXP num_row,
98 SEXP missing, SEXP n_threads);
99
100 /*!
101 * \brief create a matrix content from CSR format
102 * \param indptr pointer to row headers
103 * \param indices column indices
104 * \param data content of the data
105 * \param num_col numer of columns (when it's set to 0, then guess from data)
106 * \param missing which value to represent missing value
107 * \param n_threads Number of threads used to construct DMatrix from csr matrix.
108 * \return created dmatrix
109 */
110 XGB_DLL SEXP XGDMatrixCreateFromCSR_R(SEXP indptr, SEXP indices, SEXP data, SEXP num_col,
111 SEXP missing, SEXP n_threads);
112
113 /*!
114 * \brief create a new dmatrix from sliced content of existing matrix
115 * \param handle instance of data matrix to be sliced
116 * \param idxset index set
117 * \param allow_groups Whether to allow slicing the DMatrix if it has a 'group' field
118 * \return a sliced new matrix
119 */
120 XGB_DLL SEXP XGDMatrixSliceDMatrix_R(SEXP handle, SEXP idxset, SEXP allow_groups);
121
122 /*!
123 * \brief load a data matrix into binary file
124 * \param handle a instance of data matrix
125 * \param fname file name
126 * \param silent print statistics when saving
127 * \return R_NilValue
128 */
129 XGB_DLL SEXP XGDMatrixSaveBinary_R(SEXP handle, SEXP fname, SEXP silent);
130
131 /*!
132 * \brief set information to dmatrix
133 * \param handle a instance of data matrix
134 * \param field field name, can be label, weight
135 * \param array pointer to float vector
136 * \return R_NilValue
137 */
138 XGB_DLL SEXP XGDMatrixSetInfo_R(SEXP handle, SEXP field, SEXP array);
139
140 /*!
141 * \brief get info vector (float type) from matrix
142 * \param handle a instance of data matrix
143 * \param field field name
144 * \return info vector
145 */
146 XGB_DLL SEXP XGDMatrixGetFloatInfo_R(SEXP handle, SEXP field);
147
148 /*!
149 * \brief get info vector (uint type) from matrix
150 * \param handle a instance of data matrix
151 * \param field field name
152 * \return info vector
153 */
154 XGB_DLL SEXP XGDMatrixGetUIntInfo_R(SEXP handle, SEXP field);
155
156 /*!
157 * \brief return number of rows
158 * \param handle an instance of data matrix
159 */
160 XGB_DLL SEXP XGDMatrixNumRow_R(SEXP handle);
161
162 /*!
163 * \brief return number of columns
164 * \param handle an instance of data matrix
165 */
166 XGB_DLL SEXP XGDMatrixNumCol_R(SEXP handle);
167
168 /*!
169 <<<<<<< HEAD
170 * \brief create a ProxyDMatrix and get an R externalptr object for it
171 */
172 XGB_DLL SEXP XGProxyDMatrixCreate_R();
173
174 /*!
175 * \brief Set dense matrix data on a proxy dmatrix
176 * \param handle R externalptr pointing to a ProxyDMatrix
177 * \param R_mat R matrix to set in the proxy dmatrix
178 */
179 XGB_DLL SEXP XGProxyDMatrixSetDataDense_R(SEXP handle, SEXP R_mat);
180
181 /*!
182 * \brief Set dense matrix data on a proxy dmatrix
183 * \param handle R externalptr pointing to a ProxyDMatrix
184 * \param lst R list containing, in this order:
185 * 1. 'p' or 'indptr' vector of the CSR matrix.
186 * 2. 'j' or 'indices' vector of the CSR matrix.
187 * 3. 'x' or 'data' vector of the CSR matrix.
188 * 4. Number of columns in the CSR matrix.
189 */
190 XGB_DLL SEXP XGProxyDMatrixSetDataCSR_R(SEXP handle, SEXP lst);
191
192 /*!
193 * \brief Set dense matrix data on a proxy dmatrix
194 * \param handle R externalptr pointing to a ProxyDMatrix
195 * \param lst R list or data.frame object containing its columns as numeric vectors
196 */
197 XGB_DLL SEXP XGProxyDMatrixSetDataColumnar_R(SEXP handle, SEXP lst);
198
199 /*!
200 * \brief Create a DMatrix from a DataIter with callbacks
201 * \param expr_f_next expression for function(env, proxy_dmat) that sets the data on the proxy
202 * dmatrix and returns either zero (end of batch) or one (batch continues).
203 * \param expr_f_reset expression for function(env) that resets the data iterator to
204 * the beginning (first batch).
205 * \param calling_env R environment where to evaluate the expressions above
206 * \param proxy_dmat R externalptr holding a ProxyDMatrix.
207 * \param n_threads number of parallel threads to use for constructing the DMatrix.
208 * \param missing which value to represent missing value.
209 * \param cache_prefix path of cache file
210 * \return handle R externalptr holding the resulting DMatrix.
211 */
212 XGB_DLL SEXP XGDMatrixCreateFromCallback_R(
213 SEXP expr_f_next, SEXP expr_f_reset, SEXP calling_env, SEXP proxy_dmat,
214 SEXP n_threads, SEXP missing, SEXP cache_prefix);
215
216 /*!
217 * \brief Create a QuantileDMatrix from a DataIter with callbacks
218 * \param expr_f_next expression for function(env, proxy_dmat) that sets the data on the proxy
219 * dmatrix and returns either zero (end of batch) or one (batch continues).
220 * \param expr_f_reset expression for function(env) that resets the data iterator to
221 * the beginning (first batch).
222 * \param calling_env R environment where to evaluate the expressions above
223 * \param proxy_dmat R externalptr holding a ProxyDMatrix.
224 * \param n_threads number of parallel threads to use for constructing the QuantileDMatrix.
225 * \param missing which value to represent missing value.
226 * \param max_bin maximum number of bins to have in the resulting QuantileDMatrix.
227 * \param ref_dmat an optional reference DMatrix from which to get the bin boundaries.
228 * \return handle R externalptr holding the resulting QuantileDMatrix.
229 */
230 XGB_DLL SEXP XGQuantileDMatrixCreateFromCallback_R(
231 SEXP expr_f_next, SEXP expr_f_reset, SEXP calling_env, SEXP proxy_dmat,
232 SEXP n_threads, SEXP missing, SEXP max_bin, SEXP ref_dmat);
233
234 /*!
235 * \brief Frees a ProxyDMatrix and empties out the R externalptr object that holds it
236 * \param proxy_dmat R externalptr containing a ProxyDMatrix
237 * \return NULL
238 */
239 XGB_DLL SEXP XGDMatrixFree_R(SEXP proxy_dmat);
240
241 /*!
242 * \brief Get the value that represents missingness in R integers as a numeric non-missing value.
243 */
244 XGB_DLL SEXP XGGetRNAIntAsDouble();
245
246 /*!
247 * \brief Call R C-level function 'duplicate'
248 * \param obj Object to duplicate
249 */
250 XGB_DLL SEXP XGDuplicate_R(SEXP obj);
251
252 /*!
253 * \brief Equality comparison for two pointers
254 * \param obj1 R 'externalptr'
255 * \param obj2 R 'externalptr'
256 */
257 XGB_DLL SEXP XGPointerEqComparison_R(SEXP obj1, SEXP obj2);
258
259 /*!
260 * \brief Register the Altrep class used for the booster
261 * \param dll DLL info as provided by R_init
262 */
263 XGB_DLL void XGBInitializeAltrepClass_R(DllInfo *dll);
264
265 /*!
266 * \brief return the quantile cuts used for the histogram method
267 * \param handle an instance of data matrix
268 * \return A list with entries 'indptr' and 'data'
269 */
270 XGB_DLL SEXP XGDMatrixGetQuantileCut_R(SEXP handle);
271
272 /*!
273 * \brief get the number of non-missing entries in a dmatrix
274 * \param handle an instance of data matrix
275 * \return the number of non-missing entries
276 */
277 XGB_DLL SEXP XGDMatrixNumNonMissing_R(SEXP handle);
278
279 /*!
280 * \brief get the data in a dmatrix in CSR format
281 * \param handle an instance of data matrix
282 * \return R list with the following entries in this order:
283 * - 'indptr
284 * - 'indices
285 * - 'data'
286 * - 'ncol'
287 */
288 XGB_DLL SEXP XGDMatrixGetDataAsCSR_R(SEXP handle);
289
290 /*!
291 * \brief create xgboost learner
292 * \param dmats a list of dmatrix handles that will be cached
293 */
294 XGB_DLL SEXP XGBoosterCreate_R(SEXP dmats);
295
296 /*!
297 * \brief copy information about features from a DMatrix into a Booster
298 * \param booster R 'externalptr' pointing to a booster object
299 * \param dmat R 'externalptr' pointing to a DMatrix object
300 */
301 XGB_DLL SEXP XGBoosterCopyInfoFromDMatrix_R(SEXP booster, SEXP dmat);
302
303 /*!
304 * \brief handle R 'externalptr' holding the booster object
305 * \param field field name
306 * \param features features to set for the field
307 */
308 XGB_DLL SEXP XGBoosterSetStrFeatureInfo_R(SEXP handle, SEXP field, SEXP features);
309
310 /*!
311 * \brief handle R 'externalptr' holding the booster object
312 * \param field field name
313 */
314 XGB_DLL SEXP XGBoosterGetStrFeatureInfo_R(SEXP handle, SEXP field);
315
316 /*!
317 * \brief Get the number of boosted rounds from a model
318 * \param handle R 'externalptr' holding the booster object
319 */
320 XGB_DLL SEXP XGBoosterBoostedRounds_R(SEXP handle);
321
322 /*!
323 * \brief Get the number of features to which the model was fitted
324 * \param handle R 'externalptr' holding the booster object
325 */
326 XGB_DLL SEXP XGBoosterGetNumFeature_R(SEXP handle);
327
328 /*!
329 * \brief set parameters
330 * \param handle handle
331 * \param name parameter name
332 * \param val value of parameter
333 * \return R_NilValue
334 */
335 XGB_DLL SEXP XGBoosterSetParam_R(SEXP handle, SEXP name, SEXP val);
336
337 /*!
338 * \brief update the model in one round using dtrain
339 * \param handle handle
340 * \param iter current iteration rounds
341 * \param dtrain training data
342 * \return R_NilValue
343 */
344 XGB_DLL SEXP XGBoosterUpdateOneIter_R(SEXP ext, SEXP iter, SEXP dtrain);
345
346 /*!
347 * \brief update the model, by directly specify gradient and second order gradient,
348 * this can be used to replace UpdateOneIter, to support customized loss function
349 * \param handle handle
350 * \param iter The current training iteration.
351 * \param dtrain training data
352 * \param grad gradient statistics
353 * \param hess second order gradient statistics
354 * \return R_NilValue
355 */
356 XGB_DLL SEXP XGBoosterTrainOneIter_R(SEXP handle, SEXP dtrain, SEXP iter, SEXP grad, SEXP hess);
357
358 /*!
359 * \brief get evaluation statistics for xgboost
360 * \param handle handle
361 * \param iter current iteration rounds
362 * \param dmats list of handles to dmatrices
363 * \param evname name of evaluation
364 * \return the string containing evaluation stats
365 */
366 XGB_DLL SEXP XGBoosterEvalOneIter_R(SEXP handle, SEXP iter, SEXP dmats, SEXP evnames);
367
368 /*!
369 * \brief Run prediction on DMatrix, replacing `XGBoosterPredict_R`
370 * \param handle handle
371 * \param dmat data matrix
372 * \param json_config See `XGBoosterPredictFromDMatrix` in xgboost c_api.h
373 *
374 * \return A list containing 2 vectors, first one for shape while second one for prediction result.
375 */
376 XGB_DLL SEXP XGBoosterPredictFromDMatrix_R(SEXP handle, SEXP dmat, SEXP json_config);
377
378 /*!
379 * \brief Run prediction on R dense matrix
380 * \param handle handle
381 * \param R_mat R matrix
382 * \param missing missing value
383 * \param json_config See `XGBoosterPredictFromDense` in xgboost c_api.h. Doesn't include 'missing'
384 * \param base_margin base margin for the prediction
385 *
386 * \return A list containing 2 vectors, first one for shape while second one for prediction result.
387 */
388 XGB_DLL SEXP XGBoosterPredictFromDense_R(SEXP handle, SEXP R_mat, SEXP missing,
389 SEXP json_config, SEXP base_margin);
390
391 /*!
392 * \brief Run prediction on R CSR matrix
393 * \param handle handle
394 * \param lst An R list, containing, in this order:
395 * (a) 'p' array (a.k.a. indptr)
396 * (b) 'j' array (a.k.a. indices)
397 * (c) 'x' array (a.k.a. data / values)
398 * (d) number of columns
399 * \param missing missing value
400 * \param json_config See `XGBoosterPredictFromCSR` in xgboost c_api.h. Doesn't include 'missing'
401 * \param base_margin base margin for the prediction
402 *
403 * \return A list containing 2 vectors, first one for shape while second one for prediction result.
404 */
405 XGB_DLL SEXP XGBoosterPredictFromCSR_R(SEXP handle, SEXP lst, SEXP missing,
406 SEXP json_config, SEXP base_margin);
407
408 /*!
409 * \brief Run prediction on R data.frame
410 * \param handle handle
411 * \param R_df R data.frame
412 * \param missing missing value
413 * \param json_config See `XGBoosterPredictFromDense` in xgboost c_api.h. Doesn't include 'missing'
414 * \param base_margin base margin for the prediction
415 *
416 * \return A list containing 2 vectors, first one for shape while second one for prediction result.
417 */
418 XGB_DLL SEXP XGBoosterPredictFromColumnar_R(SEXP handle, SEXP R_df, SEXP missing,
419 SEXP json_config, SEXP base_margin);
420
421 /*!
422 * \brief load model from existing file
423 * \param handle handle
424 * \param fname file name
425 * \return R_NilValue
426 */
427 XGB_DLL SEXP XGBoosterLoadModel_R(SEXP handle, SEXP fname);
428
429 /*!
430 * \brief save model into existing file
431 * \param handle handle
432 * \param fname file name
433 * \return R_NilValue
434 */
435 XGB_DLL SEXP XGBoosterSaveModel_R(SEXP handle, SEXP fname);
436
437 /*!
438 * \brief load model from raw array
439 * \param handle handle
440 * \return R_NilValue
441 */
442 XGB_DLL SEXP XGBoosterLoadModelFromRaw_R(SEXP handle, SEXP raw);
443
444 /*!
445 * \brief Save model into R's raw array
446 *
447 * \param handle handle
448 * \param json_config JSON encoded string storing parameters for the function. Following
449 * keys are expected in the JSON document:
450 *
451 * "format": str
452 * - json: Output booster will be encoded as JSON.
453 * - ubj: Output booster will be encoded as Univeral binary JSON.
454 * - deprecated: Output booster will be encoded as old custom binary format. Do now use
455 * this format except for compatibility reasons.
456 *
457 * \return Raw array
458 */
459 XGB_DLL SEXP XGBoosterSaveModelToRaw_R(SEXP handle, SEXP json_config);
460
461 /*!
462 * \brief Save internal parameters as a JSON string
463 * \param handle handle
464 * \return JSON string
465 */
466
467 XGB_DLL SEXP XGBoosterSaveJsonConfig_R(SEXP handle);
468 /*!
469 * \brief Load the JSON string returnd by XGBoosterSaveJsonConfig_R
470 * \param handle handle
471 * \param value JSON string
472 * \return R_NilValue
473 */
474 XGB_DLL SEXP XGBoosterLoadJsonConfig_R(SEXP handle, SEXP value);
475
476 /*!
477 * \brief Memory snapshot based serialization method. Saves everything states
478 * into buffer.
479 * \param handle handle to booster
480 */
481 XGB_DLL SEXP XGBoosterSerializeToBuffer_R(SEXP handle);
482
483 /*!
484 * \brief Memory snapshot based serialization method. Loads the buffer returned
485 * from `XGBoosterSerializeToBuffer'.
486 * \param handle handle to booster
487 * \return raw byte array
488 */
489 XGB_DLL SEXP XGBoosterUnserializeFromBuffer_R(SEXP handle, SEXP raw);
490
491 /*!
492 * \brief dump model into a string
493 * \param handle handle
494 * \param fmap name to fmap can be empty string
495 * \param with_stats whether dump statistics of splits
496 * \param dump_format the format to dump the model in
497 */
498 XGB_DLL SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats, SEXP dump_format);
499
500 /*!
501 * \brief get learner attribute value
502 * \param handle handle
503 * \param name attribute name
504 * \return character containing attribute value
505 */
506 XGB_DLL SEXP XGBoosterGetAttr_R(SEXP handle, SEXP name);
507
508 /*!
509 * \brief set learner attribute value
510 * \param handle handle
511 * \param name attribute name
512 * \param val attribute value; NULL value would delete an attribute
513 * \return R_NilValue
514 */
515 XGB_DLL SEXP XGBoosterSetAttr_R(SEXP handle, SEXP name, SEXP val);
516
517 /*!
518 * \brief get the names of learner attributes
519 * \return string vector containing attribute names
520 */
521 XGB_DLL SEXP XGBoosterGetAttrNames_R(SEXP handle);
522
523 /*!
524 * \brief Get feature scores from the model.
525 * \param json_config See `XGBoosterFeatureScore` in xgboost c_api.h
526 * \return A vector with the first element as feature names, second element as shape of
527 * feature scores and thrid element as feature scores.
528 */
529 XGB_DLL SEXP XGBoosterFeatureScore_R(SEXP handle, SEXP json_config);
530
531 /*!
532 * \brief Slice a fitted booster model (by rounds)
533 * \param handle handle to the fitted booster
534 * \param begin_layer start of the slice
535 * \param end_later end of the slice; end_layer=0 is equivalent to end_layer=num_boost_round
536 * \param step step size of the slice
537 * \return The sliced booster with the requested rounds only
538 */
539 XGB_DLL SEXP XGBoosterSlice_R(SEXP handle, SEXP begin_layer, SEXP end_layer, SEXP step);
540
541 /*!
542 * \brief Slice a fitted booster model (by rounds), and replace its handle with the result
543 * \param handle handle to the fitted booster
544 * \param begin_layer start of the slice
545 * \param end_later end of the slice; end_layer=0 is equivalent to end_layer=num_boost_round
546 * \param step step size of the slice
547 * \return NULL
548 */
549 XGB_DLL SEXP XGBoosterSliceAndReplace_R(SEXP handle, SEXP begin_layer, SEXP end_layer, SEXP step);
550
551 #endif // XGBOOST_WRAPPER_R_H_ // NOLINT(*)