glmnet培训在x,y数据帧参数上引发错误:我使用错了吗? [英] glmnet training throws error on x,y dataframe arguments: am I using it wrong?

查看:206
本文介绍了glmnet培训在x,y数据帧参数上引发错误:我使用错了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用glmnet学习一种惩罚性logistic回归方法。我正在尝试根据mtcars示例数据预测汽车是否将具有自动变速器或手动变速器。我认为我的代码非常简单,但是我似乎遇到了错误:

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:

这第一个步骤只是将mtcar分为80%的训练集和20%的测试集

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,]

我知道x数据应该是没有响应的矩阵形式,所以我将两个训练集分离为一个非响应矩阵(train_x)和一个响应向量(train_y)

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)

我收到错误消息:

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

我错过了什么吗?

推荐答案

将第一个参数强制为矩阵为我求解:

Coercing the first argument as a matrix solve for me :

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

实际上,表格 glmnet?看起来第一个参数应该是矩阵/稀疏矩阵:

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


x:输入矩阵,尺寸为nobs x nvars;每行是一个观察向量。可以为稀疏矩阵格式(继承自Matrix类,如Matrix包;尚不适用于family = cox)

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")

这篇关于glmnet培训在x,y数据帧参数上引发错误:我使用错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆