glmnet training throws error on x,y dataframe arguments: am I using it wrong?

坚强是说给别人听的谎言 提交于 2019-12-13 02:16:07

问题


I'm trying to learn a penalized logistic regression method with glmnet. I'm trying to predict if a car from the mtcars example data will have an automatic transmission or manual. I think my code is pretty straightforward, but I seem to be getting an error:

This first block simply splits mtcars into an 80% train set and a 20% test set

library(glmnet)
attach(mtcars)

smp_size <- floor(0.8 * nrow(mtcars))

set.seed(123)
train_ind <- sample(seq_len(nrow(mtcars)), size=smp_size)

train <- mtcars[train_ind,]
test <- mtcars[-train_ind,]

I know the x data is supposed to be in a matrix form without the response, so I separate the two training sets into a non-response matrix (train_x) and a response vector (train_y)

train_x <- train[,!(names(train) %in% c("am"))]
train_y <- train$am

But when trying to train the model,

p1 <- glmnet(train_x, train_y)

I get the error:

Error in elnet(x, is.sparse, ix, jx, y, weights, offset, type.gaussian,
:(list) object cannot be coerced to type 'double'

Am I missing something?


回答1:


Coercing the first argument as a matrix solve for me :

p1 <- glmnet(as.matrix(train_x), train_y)

In fact , form glmnet? looks that the first argument should be a matrix/sparse matrix:

x: input matrix, of dimension nobs x nvars; each row is an observation vector. Can be in sparse matrix format (inherit from class "sparseMatrix" as in package Matrix; not yet available for family="cox")



来源:https://stackoverflow.com/questions/30717201/glmnet-training-throws-error-on-x-y-dataframe-arguments-am-i-using-it-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!