Debian Codegraph @

man/xgb.ExtMemDMatrix.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/xgb.DMatrix.R
3 \name{xgb.ExtMemDMatrix}
4 \alias{xgb.ExtMemDMatrix}
5 \title{DMatrix from External Data}
6 \usage{
7 xgb.ExtMemDMatrix(
8 data_iterator,
9 cache_prefix = tempdir(),
10 missing = NA,
11 nthread = NULL
12 )
13 }
14 \arguments{
15 \item{data_iterator}{A data iterator structure as returned by \code{\link[=xgb.DataIter]{xgb.DataIter()}},
16 which includes an environment shared between function calls, and functions to access
17 the data in batches on-demand.}
18
19 \item{cache_prefix}{The path of cache file, caller must initialize all the directories in this path.}
20
21 \item{missing}{A float value to represents missing values in data.
22
23 Note that, while functions like \code{\link[=xgb.DMatrix]{xgb.DMatrix()}} can take a generic \code{NA} and interpret it
24 correctly for different types like \code{numeric} and \code{integer}, if an \code{NA} value is passed here,
25 it will not be adapted for different input types.
26
27 For example, in R \code{integer} types, missing values are represented by integer number \code{-2147483648}
28 (since machine 'integer' types do not have an inherent 'NA' value) - hence, if one passes \code{NA},
29 which is interpreted as a floating-point NaN by \code{\link[=xgb.ExtMemDMatrix]{xgb.ExtMemDMatrix()}} and by
30 \code{\link[=xgb.QuantileDMatrix.from_iterator]{xgb.QuantileDMatrix.from_iterator()}}, these integer missing values will not be treated as missing.
31 This should not pose any problem for \code{numeric} types, since they do have an inheret NaN value.}
32
33 \item{nthread}{Number of threads used for creating DMatrix.}
34 }
35 \value{
36 An 'xgb.DMatrix' object, with subclass 'xgb.ExtMemDMatrix', in which the data is not
37 held internally but accessed through the iterator when needed.
38 }
39 \description{
40 Create a special type of XGBoost 'DMatrix' object from external data
41 supplied by an \code{\link[=xgb.DataIter]{xgb.DataIter()}} object, potentially passed in batches from a
42 bigger set that might not fit entirely in memory.
43
44 The data supplied by the iterator is accessed on-demand as needed, multiple times,
45 without being concatenated, but note that fields like 'label' \strong{will} be
46 concatenated from multiple calls to the data iterator.
47
48 For more information, see the guide 'Using XGBoost External Memory Version':
49 \url{https://xgboost.readthedocs.io/en/stable/tutorials/external_memory.html}
50 }
51 \details{
52 Be aware that construction of external data DMatrices \bold{will cache data on disk}
53 in a compressed format, under the path supplied in \code{cache_prefix}.
54
55 External data is not supported for the exact tree method.
56 }
57 \examples{
58 data(mtcars)
59
60 # This custom environment will be passed to the iterator
61 # functions at each call. It is up to the user to keep
62 # track of the iteration number in this environment.
63 iterator_env <- as.environment(
64 list(
65 iter = 0,
66 x = mtcars[, -1],
67 y = mtcars[, 1]
68 )
69 )
70
71 # Data is passed in two batches.
72 # In this example, batches are obtained by subsetting the 'x' variable.
73 # This is not advantageous to do, since the data is already loaded in memory
74 # and can be passed in full in one go, but there can be situations in which
75 # only a subset of the data will fit in the computer's memory, and it can
76 # be loaded in batches that are accessed one-at-a-time only.
77 iterator_next <- function(iterator_env) {
78 curr_iter <- iterator_env[["iter"]]
79 if (curr_iter >= 2) {
80 # there are only two batches, so this signals end of the stream
81 return(NULL)
82 }
83
84 if (curr_iter == 0) {
85 x_batch <- iterator_env[["x"]][1:16, ]
86 y_batch <- iterator_env[["y"]][1:16]
87 } else {
88 x_batch <- iterator_env[["x"]][17:32, ]
89 y_batch <- iterator_env[["y"]][17:32]
90 }
91 on.exit({
92 iterator_env[["iter"]] <- curr_iter + 1
93 })
94
95 # Function 'xgb.DataBatch' must be called manually
96 # at each batch with all the appropriate attributes,
97 # such as feature names and feature types.
98 return(xgb.DataBatch(data = x_batch, label = y_batch))
99 }
100
101 # This moves the iterator back to its beginning
102 iterator_reset <- function(iterator_env) {
103 iterator_env[["iter"]] <- 0
104 }
105
106 data_iterator <- xgb.DataIter(
107 env = iterator_env,
108 f_next = iterator_next,
109 f_reset = iterator_reset
110 )
111 cache_prefix <- tempdir()
112
113 # DMatrix will be constructed from the iterator's batches
114 dm <- xgb.ExtMemDMatrix(data_iterator, cache_prefix, nthread = 1)
115
116 # After construction, can be used as a regular DMatrix
117 params <- xgb.params(nthread = 1, objective = "reg:squarederror")
118 model <- xgb.train(data = dm, nrounds = 2, params = params)
119
120 # Predictions can also be called on it, and should be the same
121 # as if the data were passed differently.
122 pred_dm <- predict(model, dm)
123 pred_mat <- predict(model, as.matrix(mtcars[, -1]))
124 }
125 \seealso{
126 \code{\link[=xgb.DataIter]{xgb.DataIter()}}, \code{\link[=xgb.DataBatch]{xgb.DataBatch()}}, \code{\link[=xgb.QuantileDMatrix.from_iterator]{xgb.QuantileDMatrix.from_iterator()}}
127 }