Debian Codegraph @

R/xgb.save.R

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