跳到内容

以文本格式转储XGBoost模型。

用法

xgb.dump(
  model,
  fname = NULL,
  fmap = "",
  with_stats = FALSE,
  dump_format = c("text", "json", "dot"),
  ...
)

参数

model

模型对象。

fname

用于保存模型文本转储的文本文件的名称。如果未提供或设置为NULL,则模型将作为字符向量返回。

fmap

表示特征类型的特征映射文件。请参阅demo/以获取R中的演练示例,并参阅https://github.com/dmlc/xgboost/blob/master/demo/data/featmap.txt以查看该值的示例。

with_stats

是否转储有关拆分的一些额外统计信息。当此选项打开时,模型转储包含两个额外的值:gain是我们在每次拆分中获得的近似损失函数增益;cover是每个节点中二阶梯度的总和。

dump_format

可以指定“text”、“json”或“dot”(graphviz)格式。

单个树的“dot”格式可以直接传递给使用此格式进行图可视化的包,例如函数DiagrammeR::grViz()

...

未使用。

在以前的 XGBoost 版本中,此函数的一些参数已被弃用或重命名。如果传递已弃用或重命名的参数,将(默认)抛出警告并改用其当前等效项。如果使用 '严格模式'选项,此警告将变为错误。

如果传递了既不是当前函数参数也不是已弃用或重命名参数的附加参数,则根据“严格模式”选项,将抛出警告或错误。

重要: ... 将在未来版本中删除,所有当前的弃用警告将变为错误。请仅使用构成函数签名的参数。

如果未提供fname或将其设置为NULL,则函数将把模型作为字符向量返回。否则,它将返回TRUE

示例

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

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 = 2,
    objective = "binary:logistic"
  )
)

# save the model in file 'xgb.model.dump'
dump_path = file.path(tempdir(), 'model.dump')
xgb.dump(bst, dump_path, with_stats = TRUE)

# print the model without saving it to a file
print(xgb.dump(bst, with_stats = TRUE))

# print in JSON format:
cat(xgb.dump(bst, with_stats = TRUE, dump_format = "json"))

# plot first tree leveraging the 'dot' format
if (requireNamespace('DiagrammeR', quietly = TRUE)) {
  DiagrammeR::grViz(xgb.dump(bst, dump_format = "dot")[[1L]])
}