Reputation: 572
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.
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
I need the dtrain to be in matrix or dataframe format
Upvotes: 5
Views: 2539
Reputation: 4184
This seems to be a problem of exporting data to Cpp. Where xgboost authors did not implement a getter sugar. At least dimnames and dim could be retrieve. 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
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:
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))
}
Upvotes: 1
Reputation: 572
This is not possible , got response from lead maintainer of Xgboost library
Upvotes: 2