glmnet NA/NaN/Inf中的套索错误 [英] Lasso error in glmnet NA/NaN/Inf
本文介绍了glmnet NA/NaN/Inf中的套索错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在glmnet上遇到问题,因为我不断收到错误消息
I'm having an issue with glmnet in that I keep getting the error message
"Error in elnet(x, is.sparse, ix, jx, y, weights, offset, type.gaussian, : NA/NaN/Inf in foreign function call (arg 5)
In addition: Warning message:
In elnet(x, is.sparse, ix, jx, y, weights, offset, type.gaussian, : NAs introduced by coercion"
下面,我可以使用"iris"数据集复制错误,但这是我的特定数据的简化代码:
Below I can replicate the error with the 'iris' data set, but here is the simplified code for my particular data:
vars <- as.matrix(ind.vars)
lasso <- glmnet(vars, y=cup98$TARGET_D, alpha=1)
以下是您可以轻松复制的内容:
Here is something you can easily reproduce:
data(iris)
attach(iris)
x <- as.matrix(data.frame(Sepal.Width, Petal.Length, Petal.Width, Species))
y <- Sepal.Length
lasso <- glmnet(x,y=y, alpha=1)
非常感谢大家!
推荐答案
使用as.matrix
可以将数字值强制转换为字符,因为您要保留在种类"中:
With as.matrix
you are coercing the numeric values to character because you are leaving in "Species":
str(as.matrix(iris[, c('Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species')]))
chr [1:150, 1:4] "3.5" "3.0" "3.2" "3.1" "3.6" "3.9" ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:4] "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
使用attach/detach
通常也是非常个坏主意,如果您不知道为什么不使用它,那么您肯定会最多 /em>应该不使用它.
Also usually a very bad idea to use attach/detach
, and if you do not know why you should not use it, then you most definitely should not use it.
data(iris); require(glmnet)
x<-as.matrix(iris[, c('Sepal.Width', 'Petal.Length', 'Petal.Width')])
y<-iris$Sepal.Length
lasso<-glmnet(x,y=y, alpha=1); lasso # no error
这篇关于glmnet NA/NaN/Inf中的套索错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文