跳过内容

获取一个包含原始 xgb.DMatrix 对象中指定行的新 DMatrix。

用法

xgb.slice.DMatrix(object, idxset, allow_groups = FALSE)

# S3 method for class 'xgb.DMatrix'
object[idxset, colset = NULL]

参数

object

xgb.DMatrix 类对象。

idxset

一个整数向量,包含所需行的索引(基于 1 的索引)。

allow_groups

是否允许对带有 group(或等价的 qid)字段的 xgb.DMatrix 进行切片。请注意,在这种情况下,结果将不再包含组信息 - 需要通过 setinfo() 手动设置。

colset

当前未使用(列子集不可用)。

示例

data(agaricus.train, package = "xgboost")

dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))

dsub <- xgb.slice.DMatrix(dtrain, 1:42)
labels1 <- getinfo(dsub, "label")

dsub <- dtrain[1:42, ]
labels2 <- getinfo(dsub, "label")
all.equal(labels1, labels2)