简体   繁体   English

将 xgb.DMatrix 转换为 dataframe 或矩阵

[英]transform xgb.DMatrix to dataframe or matrix

I have a xgb.Dmatrix which I want to convert to dataframe or matrix format, I would like to know if it is possible to convert.我有一个 xgb.Dmatrix,我想将其转换为 dataframe 或矩阵格式,我想知道是否可以转换。

data(agaricus.train, package='xgboost')
train <- agaricus.train
dtrain <- xgb.DMatrix(train$data, label=train$label)
class(dtrain)

when I try to convert dtrain to matrix or dataframe I get an error, can someone suggest what can be done当我尝试将 dtrain 转换为矩阵或 dataframe 时出现错误,有人可以建议可以做什么

在此处输入图像描述

I need the dtrain to be in matrix or dataframe format我需要 dtrain 为矩阵或 dataframe 格式

This is not possible, got response from lead maintainer of Xgboost library这是不可能的,得到了 Xgboost 库的主要维护者的回应

This seems to be a problem of exporting data to Cpp.这个好像是导出数据到cpp的问题。 Where xgboost authors did not implement a getter sugar. xgboost 作者没有实现 getter 糖的地方。 At least dimnames and dim could be retrieve.至少可以检索 dimnames 和 dim。 Implemented methods:实施方法:

> methods(class="xgb.DMatrix")
[1] [          dim        dimnames   dimnames<- getinfo    print      setinfo
[8] slice
see '?methods' for accessing help and source code

colnames works too colnames也有效

Example:例子:

> dd <- xgb.DMatrix(cbind(a = 1:10, b = 1:10))
> colnames(dd)
[1] "a" "b"
> dim(dd)
[1] 10  2
> dimnames(dd)
[[1]]
NULL

[[2]]
[1] "a" "b"
> slice(dd, 1L)
xgb.DMatrix  dim: 1 x 2  info: NA  colnames: no

This is part of code (xgb.DMatrix body) where a data is exported to Cpp:这是将数据导出到 Cpp 的代码(xgb.DMatrix 主体)的一部分:

if (length(data) > 1)
            stop("'data' has class 'character' and length ",
                length(data), ".\n  'data' accepts either a numeric matrix or a single filename.")
        handle <- .Call(XGDMatrixCreateFromFile_R, data, as.integer(silent))
    }
    else if (is.matrix(data)) {
        handle <- .Call(XGDMatrixCreateFromMat_R, data, missing)
        cnames <- colnames(data)
    }
    else if (inherits(data, "dgCMatrix")) {
        handle <- .Call(XGDMatrixCreateFromCSC_R, data@p, data@i,
            data@x, nrow(data))
        cnames <- colnames(data)
    }
    else {
        stop("xgb.DMatrix does not support construction from ",
            typeof(data))
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM