跳到内容

从二进制模型文件加载 XGBoost 模型。

用法

xgb.load(modelfile)

参数

modelfile

二进制输入文件的名称。

返回值

一个 xgb.Booster 类的对象。

详细信息

输入文件应包含使用 xgb.save() 在 R 中保存的模型,或使用其他 XGBoost 接口中的适当方法保存的模型。例如,在 Python 中训练并以 XGBoost 格式保存的模型,可以在 R 中加载。

注意:保存为 R 对象的模型必须使用相应的 R 方法加载,而不是使用 xgb.load()

另请参阅

示例

DONTSHOW({RhpcBLASctl::omp_set_num_threads(1)})
data(agaricus.train, package = "xgboost")
data(agaricus.test, package = "xgboost")

## Keep the number of threads to 1 for examples
nthread <- 1
data.table::setDTthreads(nthread)

train <- agaricus.train
test <- agaricus.test

bst <- xgb.train(
  data = xgb.DMatrix(train$data, label = train$label, nthread = 1),
  nrounds = 2,
  params = xgb.params(
    max_depth = 2,
    nthread = nthread,
    objective = "binary:logistic"
  )
)

fname <- file.path(tempdir(), "xgb.ubj")
xgb.save(bst, fname)
bst <- xgb.load(fname)