Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sparse matrix (dgCMatrix) used with xgboost giving errors in train() #593

Closed
dkincaid opened this issue Feb 6, 2017 · 2 comments
Closed

Comments

@dkincaid
Copy link

dkincaid commented Feb 6, 2017

Using caret version 6.0-73, xgboost version 0.6-4.

I have a dgCMatrix that I'm using in the x argument for train(). I'm getting errors like this:

Error in as.data.frame.default(x) : 
  cannot coerce class "structure("dgCMatrix", package = "Matrix")" to a data.frame

I also tried to convert the matrix myself to an xdb.DMatrix (dmatrix <- xgb.DMatrix(train_text_features)) but when I do that then I get this error during training:

Error in `[.xgb.DMatrix`(x, modelIndex, , drop = FALSE) : 
  unused argument (drop = FALSE)

am I doing something wrong or is there a bug?

My code looks like this (train_text_features is a dgCMatrix)

itControl <- trainControl(## 10-fold CV
   method = "repeatedcv",
   number = 10,
   repeats = 1,
   sampling = "up")

dmatrix <- xgb.DMatrix(train_text_features)

gbmFit1 <- train(x = train_text_features, # dmatrix gives the second error above.
                 y = train$class, 
                 method = "xgbTree", 
                 trControl = fitControl,
                 nrounds = 100,
                 max.depth = 11,
                 objective = "binary:logistic")
@topepo
Copy link
Owner

topepo commented Apr 6, 2017

It would help to have a small reproducible example (the issues template is there for a reason).

Also, you should have seen warnings about nrounds and max.depth. These shouldn't be specified in the train call (and should be in a tuning grid).

The main problem is that you are using up-sampling. upSample and downSample require a matrix or data frame. Those functions try to convert to a matrix or data frame but

> class(x)
[1] "xgb.DMatrix"
> as.matrix(x)
Error in as.vector(x, mode) : 
  cannot coerce type 'externalptr' to vector of type 'any'
> as.data.frame(x)
Error in as.data.frame.default(x) : 
  cannot coerce class ""xgb.DMatrix"" to a data.frame

The proper solution is to make classes for each of the sampling techniques and create methods for each data type (matrix, data frames, and sparse matrices). SMOTE and ROSE will have the same issue. That will take a little time and won't be in the imminent release.

There are a few other small issues that I'll fix in the short term. xgb.DMatrix is kind of a pain to work with (e.g. arcane error messages like "xgb.DMatrix: does not support to construct from externalptr")

@topepo
Copy link
Owner

topepo commented Apr 13, 2017

This should be fixed now in the devel version and the one that I just sent to CRAN. But please test and reopen if you find any problems

@topepo topepo closed this as completed Apr 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants