| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.DMatrix.R |
| 3 |
\name{xgb.DMatrix} |
| 4 |
\alias{xgb.DMatrix} |
| 5 |
\alias{xgb.QuantileDMatrix} |
| 6 |
\title{Construct xgb.DMatrix object} |
| 7 |
\usage{ |
| 8 |
xgb.DMatrix( |
| 9 |
data, |
| 10 |
label = NULL, |
| 11 |
weight = NULL, |
| 12 |
base_margin = NULL, |
| 13 |
missing = NA, |
| 14 |
silent = FALSE, |
| 15 |
feature_names = colnames(data), |
| 16 |
feature_types = NULL, |
| 17 |
nthread = NULL, |
| 18 |
group = NULL, |
| 19 |
qid = NULL, |
| 20 |
label_lower_bound = NULL, |
| 21 |
label_upper_bound = NULL, |
| 22 |
feature_weights = NULL, |
| 23 |
data_split_mode = "row", |
| 24 |
... |
| 25 |
) |
| 26 |
|
| 27 |
xgb.QuantileDMatrix( |
| 28 |
data, |
| 29 |
label = NULL, |
| 30 |
weight = NULL, |
| 31 |
base_margin = NULL, |
| 32 |
missing = NA, |
| 33 |
feature_names = colnames(data), |
| 34 |
feature_types = NULL, |
| 35 |
nthread = NULL, |
| 36 |
group = NULL, |
| 37 |
qid = NULL, |
| 38 |
label_lower_bound = NULL, |
| 39 |
label_upper_bound = NULL, |
| 40 |
feature_weights = NULL, |
| 41 |
ref = NULL, |
| 42 |
max_bin = NULL |
| 43 |
) |
| 44 |
} |
| 45 |
\arguments{ |
| 46 |
\item{data}{Data from which to create a DMatrix, which can then be used for fitting models or |
| 47 |
for getting predictions out of a fitted model. |
| 48 |
|
| 49 |
Supported input types are as follows: |
| 50 |
\itemize{ |
| 51 |
\item \code{matrix} objects, with types \code{numeric}, \code{integer}, or \code{logical}. |
| 52 |
\item \code{data.frame} objects, with columns of types \code{numeric}, \code{integer}, \code{logical}, or \code{factor} |
| 53 |
} |
| 54 |
|
| 55 |
Note that xgboost uses base-0 encoding for categorical types, hence \code{factor} types (which use base-1 |
| 56 |
encoding') will be converted inside the function call. Be aware that the encoding used for \code{factor} |
| 57 |
types is not kept as part of the model, so in subsequent calls to \code{predict}, it is the user's |
| 58 |
responsibility to ensure that factor columns have the same levels as the ones from which the DMatrix |
| 59 |
was constructed. |
| 60 |
|
| 61 |
Other column types are not supported. |
| 62 |
\itemize{ |
| 63 |
\item CSR matrices, as class \code{dgRMatrix} from package \code{Matrix}. |
| 64 |
\item CSC matrices, as class \code{dgCMatrix} from package \code{Matrix}. |
| 65 |
} |
| 66 |
|
| 67 |
These are \strong{not} supported by \code{xgb.QuantileDMatrix}. |
| 68 |
\itemize{ |
| 69 |
\item XGBoost's own binary format for DMatrices, as produced by \code{\link[=xgb.DMatrix.save]{xgb.DMatrix.save()}}. |
| 70 |
\item Single-row CSR matrices, as class \code{dsparseVector} from package \code{Matrix}, which is interpreted |
| 71 |
as a single row (only when making predictions from a fitted model). |
| 72 |
}} |
| 73 |
|
| 74 |
\item{label}{Label of the training data. For classification problems, should be passed encoded as |
| 75 |
integers with numeration starting at zero.} |
| 76 |
|
| 77 |
\item{weight}{Weight for each instance. |
| 78 |
|
| 79 |
Note that, for ranking task, weights are per-group. In ranking task, one weight |
| 80 |
is assigned to each group (not each data point). This is because we |
| 81 |
only care about the relative ordering of data points within each group, |
| 82 |
so it doesn't make sense to assign weights to individual data points.} |
| 83 |
|
| 84 |
\item{base_margin}{Base margin used for boosting from existing model. |
| 85 |
|
| 86 |
In the case of multi-output models, one can also pass multi-dimensional base_margin.} |
| 87 |
|
| 88 |
\item{missing}{A float value to represents missing values in data (not used when creating DMatrix |
| 89 |
from text files). It is useful to change when a zero, infinite, or some other |
| 90 |
extreme value represents missing values in data.} |
| 91 |
|
| 92 |
\item{silent}{whether to suppress printing an informational message after loading from a file.} |
| 93 |
|
| 94 |
\item{feature_names}{Set names for features. Overrides column names in data frame and matrix. |
| 95 |
|
| 96 |
Note: columns are not referenced by name when calling \code{predict}, so the column order there |
| 97 |
must be the same as in the DMatrix construction, regardless of the column names.} |
| 98 |
|
| 99 |
\item{feature_types}{Set types for features. |
| 100 |
|
| 101 |
If \code{data} is a \code{data.frame} and passing \code{feature_types} is not supplied, |
| 102 |
feature types will be deduced automatically from the column types. |
| 103 |
|
| 104 |
Otherwise, one can pass a character vector with the same length as number of columns in \code{data}, |
| 105 |
with the following possible values: |
| 106 |
\itemize{ |
| 107 |
\item "c", which represents categorical columns. |
| 108 |
\item "q", which represents numeric columns. |
| 109 |
\item "int", which represents integer columns. |
| 110 |
\item "i", which represents logical (boolean) columns. |
| 111 |
} |
| 112 |
|
| 113 |
Note that, while categorical types are treated differently from the rest for model fitting |
| 114 |
purposes, the other types do not influence the generated model, but have effects in other |
| 115 |
functionalities such as feature importances. |
| 116 |
|
| 117 |
\strong{Important}: Categorical features, if specified manually through \code{feature_types}, must |
| 118 |
be encoded as integers with numeration starting at zero, and the same encoding needs to be |
| 119 |
applied when passing data to \code{\link[=predict]{predict()}}. Even if passing \code{factor} types, the encoding will |
| 120 |
not be saved, so make sure that \code{factor} columns passed to \code{predict} have the same \code{levels}.} |
| 121 |
|
| 122 |
\item{nthread}{Number of threads used for creating DMatrix.} |
| 123 |
|
| 124 |
\item{group}{Group size for all ranking group.} |
| 125 |
|
| 126 |
\item{qid}{Query ID for data samples, used for ranking.} |
| 127 |
|
| 128 |
\item{label_lower_bound}{Lower bound for survival training.} |
| 129 |
|
| 130 |
\item{label_upper_bound}{Upper bound for survival training.} |
| 131 |
|
| 132 |
\item{feature_weights}{Set feature weights for column sampling.} |
| 133 |
|
| 134 |
\item{data_split_mode}{Not used yet. This parameter is for distributed training, which is not yet available for the R package.} |
| 135 |
|
| 136 |
\item{...}{Not used. |
| 137 |
|
| 138 |
Some arguments that were part of this function in previous XGBoost versions are currently |
| 139 |
deprecated or have been renamed. If a deprecated or renamed argument is passed, will throw |
| 140 |
a warning (by default) and use its current equivalent instead. This warning will become an |
| 141 |
error if using the \link[=xgboost-options]{'strict mode' option}. |
| 142 |
|
| 143 |
If some additional argument is passed that is neither a current function argument nor |
| 144 |
a deprecated or renamed argument, a warning or error will be thrown depending on the |
| 145 |
'strict mode' option. |
| 146 |
|
| 147 |
\bold{Important:} \code{...} will be removed in a future version, and all the current |
| 148 |
deprecation warnings will become errors. Please use only arguments that form part of |
| 149 |
the function signature.} |
| 150 |
|
| 151 |
\item{ref}{The training dataset that provides quantile information, needed when creating |
| 152 |
validation/test dataset with \code{\link[=xgb.QuantileDMatrix]{xgb.QuantileDMatrix()}}. Supplying the training DMatrix |
| 153 |
as a reference means that the same quantisation applied to the training data is |
| 154 |
applied to the validation/test data} |
| 155 |
|
| 156 |
\item{max_bin}{The number of histogram bin, should be consistent with the training parameter |
| 157 |
\code{max_bin}. |
| 158 |
|
| 159 |
This is only supported when constructing a QuantileDMatrix.} |
| 160 |
} |
| 161 |
\value{ |
| 162 |
An 'xgb.DMatrix' object. If calling \code{xgb.QuantileDMatrix}, it will have additional |
| 163 |
subclass \code{xgb.QuantileDMatrix}. |
| 164 |
} |
| 165 |
\description{ |
| 166 |
Construct an 'xgb.DMatrix' object from a given data source, which can then be passed to functions |
| 167 |
such as \code{\link[=xgb.train]{xgb.train()}} or \code{\link[=predict]{predict()}}. |
| 168 |
} |
| 169 |
\details{ |
| 170 |
Function \code{xgb.QuantileDMatrix()} will construct a DMatrix with quantization for the histogram |
| 171 |
method already applied to it, which can be used to reduce memory usage (compared to using a |
| 172 |
a regular DMatrix first and then creating a quantization out of it) when using the histogram |
| 173 |
method (\code{tree_method = "hist"}, which is the default algorithm), but is not usable for the |
| 174 |
sorted-indices method (\code{tree_method = "exact"}), nor for the approximate method |
| 175 |
(\code{tree_method = "approx"}). |
| 176 |
|
| 177 |
Note that DMatrix objects are not serializable through R functions such as \code{\link[=saveRDS]{saveRDS()}} or \code{\link[=save]{save()}}. |
| 178 |
If a DMatrix gets serialized and then de-serialized (for example, when saving data in an R session or caching |
| 179 |
chunks in an Rmd file), the resulting object will not be usable anymore and will need to be reconstructed |
| 180 |
from the original source of data. |
| 181 |
} |
| 182 |
\examples{ |
| 183 |
data(agaricus.train, package = "xgboost") |
| 184 |
|
| 185 |
## Keep the number of threads to 1 for examples |
| 186 |
nthread <- 1 |
| 187 |
data.table::setDTthreads(nthread) |
| 188 |
dtrain <- with( |
| 189 |
agaricus.train, xgb.DMatrix(data, label = label, nthread = nthread) |
| 190 |
) |
| 191 |
fname <- file.path(tempdir(), "xgb.DMatrix.data") |
| 192 |
xgb.DMatrix.save(dtrain, fname) |
| 193 |
dtrain <- xgb.DMatrix(fname, nthread = 1) |
| 194 |
} |