| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/xgb.Booster.R |
| 3 |
\name{xgb.copy.Booster} |
| 4 |
\alias{xgb.copy.Booster} |
| 5 |
\title{Deep-copies a Booster Object} |
| 6 |
\usage{ |
| 7 |
xgb.copy.Booster(model) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{model}{An 'xgb.Booster' object.} |
| 11 |
} |
| 12 |
\value{ |
| 13 |
A deep copy of \code{model} - it will be identical in every way, but C-level |
| 14 |
functions called on that copy will not affect the \code{model} variable. |
| 15 |
} |
| 16 |
\description{ |
| 17 |
Creates a deep copy of an 'xgb.Booster' object, such that the |
| 18 |
C object pointer contained will be a different object, and hence functions |
| 19 |
like \code{\link[=xgb.attr]{xgb.attr()}} will not affect the object from which it was copied. |
| 20 |
} |
| 21 |
\examples{ |
| 22 |
library(xgboost) |
| 23 |
|
| 24 |
data(mtcars) |
| 25 |
|
| 26 |
y <- mtcars$mpg |
| 27 |
x <- mtcars[, -1] |
| 28 |
|
| 29 |
dm <- xgb.DMatrix(x, label = y, nthread = 1) |
| 30 |
|
| 31 |
model <- xgb.train( |
| 32 |
data = dm, |
| 33 |
params = xgb.params(nthread = 1), |
| 34 |
nrounds = 3 |
| 35 |
) |
| 36 |
|
| 37 |
# Set an arbitrary attribute kept at the C level |
| 38 |
xgb.attr(model, "my_attr") <- 100 |
| 39 |
print(xgb.attr(model, "my_attr")) |
| 40 |
|
| 41 |
# Just assigning to a new variable will not create |
| 42 |
# a deep copy - C object pointer is shared, and in-place |
| 43 |
# modifications will affect both objects |
| 44 |
model_shallow_copy <- model |
| 45 |
xgb.attr(model_shallow_copy, "my_attr") <- 333 |
| 46 |
# 'model' was also affected by this change: |
| 47 |
print(xgb.attr(model, "my_attr")) |
| 48 |
|
| 49 |
model_deep_copy <- xgb.copy.Booster(model) |
| 50 |
xgb.attr(model_deep_copy, "my_attr") <- 444 |
| 51 |
# 'model' was NOT affected by this change |
| 52 |
# (keeps previous value that was assigned before) |
| 53 |
print(xgb.attr(model, "my_attr")) |
| 54 |
|
| 55 |
# Verify that the new object was actually modified |
| 56 |
print(xgb.attr(model_deep_copy, "my_attr")) |
| 57 |
} |