| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.Booster.R |
| 3 |
\name{xgb.attr} |
| 4 |
\alias{xgb.attr} |
| 5 |
\alias{xgb.attr<-} |
| 6 |
\alias{xgb.attributes} |
| 7 |
\alias{xgb.attributes<-} |
| 8 |
\title{Accessors for serializable attributes of a model} |
| 9 |
\usage{ |
| 10 |
xgb.attr(object, name) |
| 11 |
|
| 12 |
xgb.attr(object, name) <- value |
| 13 |
|
| 14 |
xgb.attributes(object) |
| 15 |
|
| 16 |
xgb.attributes(object) <- value |
| 17 |
} |
| 18 |
\arguments{ |
| 19 |
\item{object}{Object of class \code{xgb.Booster}. \strong{Will be modified in-place} when assigning to it.} |
| 20 |
|
| 21 |
\item{name}{A non-empty character string specifying which attribute is to be accessed.} |
| 22 |
|
| 23 |
\item{value}{For \verb{xgb.attr<-}, a value of an attribute; for \verb{xgb.attributes<-}, |
| 24 |
it is a list (or an object coercible to a list) with the names of attributes to set |
| 25 |
and the elements corresponding to attribute values. |
| 26 |
Non-character values are converted to character. |
| 27 |
When an attribute value is not a scalar, only the first index is used. |
| 28 |
Use \code{NULL} to remove an attribute.} |
| 29 |
} |
| 30 |
\value{ |
| 31 |
\itemize{ |
| 32 |
\item \code{xgb.attr()} returns either a string value of an attribute |
| 33 |
or \code{NULL} if an attribute wasn't stored in a model. |
| 34 |
\item \code{xgb.attributes()} returns a list of all attributes stored in a model |
| 35 |
or \code{NULL} if a model has no stored attributes. |
| 36 |
} |
| 37 |
} |
| 38 |
\description{ |
| 39 |
These methods allow to manipulate the key-value attribute strings of an XGBoost model. |
| 40 |
} |
| 41 |
\details{ |
| 42 |
The primary purpose of XGBoost model attributes is to store some meta data about the model. |
| 43 |
Note that they are a separate concept from the object attributes in R. |
| 44 |
Specifically, they refer to key-value strings that can be attached to an XGBoost model, |
| 45 |
stored together with the model's binary representation, and accessed later |
| 46 |
(from R or any other interface). |
| 47 |
In contrast, any R attribute assigned to an R object of \code{xgb.Booster} class |
| 48 |
would not be saved by \code{\link[=xgb.save]{xgb.save()}} because an XGBoost model is an external memory object |
| 49 |
and its serialization is handled externally. |
| 50 |
Also, setting an attribute that has the same name as one of XGBoost's parameters wouldn't |
| 51 |
change the value of that parameter for a model. |
| 52 |
Use \code{\link[=xgb.model.parameters<-]{xgb.model.parameters<-()}} to set or change model parameters. |
| 53 |
|
| 54 |
The \verb{xgb.attributes<-} setter either updates the existing or adds one or several attributes, |
| 55 |
but it doesn't delete the other existing attributes. |
| 56 |
|
| 57 |
Important: since this modifies the booster's C object, semantics for assignment here |
| 58 |
will differ from R's, as any object reference to the same booster will be modified |
| 59 |
too, while assignment of R attributes through \verb{attributes(model)$<attr> <- <value>} |
| 60 |
will follow the usual copy-on-write R semantics (see \code{\link[=xgb.copy.Booster]{xgb.copy.Booster()}} for an |
| 61 |
example of these behaviors). |
| 62 |
} |
| 63 |
\examples{ |
| 64 |
data(agaricus.train, package = "xgboost") |
| 65 |
train <- agaricus.train |
| 66 |
|
| 67 |
bst <- xgb.train( |
| 68 |
data = xgb.DMatrix(train$data, label = train$label, nthread = 1), |
| 69 |
nrounds = 2, |
| 70 |
params = xgb.params( |
| 71 |
max_depth = 2, |
| 72 |
nthread = 2, |
| 73 |
objective = "binary:logistic" |
| 74 |
) |
| 75 |
) |
| 76 |
|
| 77 |
xgb.attr(bst, "my_attribute") <- "my attribute value" |
| 78 |
print(xgb.attr(bst, "my_attribute")) |
| 79 |
xgb.attributes(bst) <- list(a = 123, b = "abc") |
| 80 |
|
| 81 |
fname <- file.path(tempdir(), "xgb.ubj") |
| 82 |
xgb.save(bst, fname) |
| 83 |
bst1 <- xgb.load(fname) |
| 84 |
print(xgb.attr(bst1, "my_attribute")) |
| 85 |
print(xgb.attributes(bst1)) |
| 86 |
|
| 87 |
# deletion: |
| 88 |
xgb.attr(bst1, "my_attribute") <- NULL |
| 89 |
print(xgb.attributes(bst1)) |
| 90 |
xgb.attributes(bst1) <- list(a = NULL, b = NULL) |
| 91 |
print(xgb.attributes(bst1)) |
| 92 |
|
| 93 |
} |