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

error when predict probabilities on model trained on a dgCMatrix #342

Closed
anmiko opened this issue Dec 20, 2015 · 1 comment
Closed

error when predict probabilities on model trained on a dgCMatrix #342

anmiko opened this issue Dec 20, 2015 · 1 comment

Comments

@anmiko
Copy link

@anmiko anmiko commented Dec 20, 2015

when a model is trained on a dgCMatrix (e.g. created by hashed.model.matrix of FeatureHashing )

an attempt to call predict with type ="prob" raises error

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

but predict works fine when type='raw". Also as a workaround probFunction can be called directly.

code example

library(glmnet)
library(FeatureHashing)
library(caret)
tmp.df <- data.frame(A=1:12, B=3:14, Y = c('a','a','b','c','b','a','a','a','c','c','b','a'))

mat.fh <- hashed.model.matrix(~ A+B, tmp.df, 2^10, create.mapping = TRUE)

model <- train(x = mat.fh, y = as.factor(tmp.df$Y), method = 'glmnet')

probs <- predict(model, newdata = mat.fh, type = "prob")

#a workaround  
probs <- probFunction(model$modelInfo, model$finalModel, mat.fh, model$preProcess)

caret version '6.0.62'

topepo added a commit that referenced this issue Jan 1, 2016
`predict' now avoids `extractPrediction` and `extractProb` and goes
straight to `probFunction` and `predictionFunction`
@topepo
Copy link
Owner

@topepo topepo commented Jan 1, 2016

Thanks. This is a bug and has been fixed.

@topepo topepo closed this Jan 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.