| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.save.R |
| 3 |
\name{xgb.save} |
| 4 |
\alias{xgb.save} |
| 5 |
\title{Save XGBoost model to binary file} |
| 6 |
\usage{ |
| 7 |
xgb.save(model, fname) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{model}{Model object of \code{xgb.Booster} class.} |
| 11 |
|
| 12 |
\item{fname}{Name of the file to write. Its extension determines the serialization format: |
| 13 |
\itemize{ |
| 14 |
\item ".ubj": Use the universal binary JSON format (recommended). |
| 15 |
This format uses binary types for e.g. floating point numbers, thereby preventing any loss |
| 16 |
of precision when converting to a human-readable JSON text or similar. |
| 17 |
\item ".json": Use plain JSON, which is a human-readable format. |
| 18 |
\item ".deprecated": Use \strong{deprecated} binary format. This format will |
| 19 |
not be able to save attributes introduced after v1 of XGBoost, such as the "best_iteration" |
| 20 |
attribute that boosters might keep, nor feature names or user-specifiec attributes. |
| 21 |
\item If the format is not specified by passing one of the file extensions above, will |
| 22 |
default to UBJ. |
| 23 |
}} |
| 24 |
} |
| 25 |
\description{ |
| 26 |
Save XGBoost model to a file in binary or JSON format. |
| 27 |
} |
| 28 |
\details{ |
| 29 |
This methods allows to save a model in an XGBoost-internal binary or text format which is universal |
| 30 |
among the various xgboost interfaces. In R, the saved model file could be read later |
| 31 |
using either the \code{\link[=xgb.load]{xgb.load()}} function or the \code{xgb_model} parameter of \code{\link[=xgb.train]{xgb.train()}}. |
| 32 |
|
| 33 |
Note: a model can also be saved as an R object (e.g., by using \code{\link[=readRDS]{readRDS()}} |
| 34 |
or \code{\link[=save]{save()}}). However, it would then only be compatible with R, and |
| 35 |
corresponding R methods would need to be used to load it. Moreover, persisting the model with |
| 36 |
\code{\link[=readRDS]{readRDS()}} or \code{\link[=save]{save()}} might cause compatibility problems in |
| 37 |
future versions of XGBoost. Consult \link{a-compatibility-note-for-saveRDS-save} to learn |
| 38 |
how to persist models in a future-proof way, i.e., to make the model accessible in future |
| 39 |
releases of XGBoost. |
| 40 |
} |
| 41 |
\examples{ |
| 42 |
\dontshow{RhpcBLASctl::omp_set_num_threads(1)} |
| 43 |
data(agaricus.train, package = "xgboost") |
| 44 |
data(agaricus.test, package = "xgboost") |
| 45 |
|
| 46 |
## Keep the number of threads to 1 for examples |
| 47 |
nthread <- 1 |
| 48 |
data.table::setDTthreads(nthread) |
| 49 |
|
| 50 |
train <- agaricus.train |
| 51 |
test <- agaricus.test |
| 52 |
|
| 53 |
bst <- xgb.train( |
| 54 |
data = xgb.DMatrix(train$data, label = train$label, nthread = 1), |
| 55 |
nrounds = 2, |
| 56 |
params = xgb.params( |
| 57 |
max_depth = 2, |
| 58 |
nthread = nthread, |
| 59 |
objective = "binary:logistic" |
| 60 |
) |
| 61 |
) |
| 62 |
|
| 63 |
fname <- file.path(tempdir(), "xgb.ubj") |
| 64 |
xgb.save(bst, fname) |
| 65 |
bst <- xgb.load(fname) |
| 66 |
} |
| 67 |
\seealso{ |
| 68 |
\code{\link[=xgb.load]{xgb.load()}} |
| 69 |
} |