14

我正在尝试在大型数据集(5000x300)上运行 randomForest。不幸的是,我收到如下错误消息:

> RF <- randomForest(prePrior1, postPrior1[,6]
+                    ,,do.trace=TRUE,importance=TRUE,ntree=100,,forest=TRUE)
Error in randomForest.default(prePrior1, postPrior1[, 6], , do.trace = TRUE,  : 
  NA/NaN/Inf in foreign function call (arg 1)

因此,我尝试使用以下方法查找任何 NA:

> df2 <- prePrior1[is.na(prePrior1)]
> df2 
character(0)
> df2 <- postPrior1[is.na(postPrior1[,6])]
> df2 
numeric(0)

这让我相信问题出在 Inf 上,因为似乎没有任何 NA。

关于如何根除 Inf 的任何建议?

4

5 回答 5

25

您可能正在寻找is.finite,尽管我不能 100% 确定问题出在输入数据中的 Infs 上。

请务必is.finite仔细阅读帮助,了解它选择了哪些缺失、无限等组合。具体来说,这个:

> is.finite(c(1,NA,-Inf,NaN))
[1]  TRUE FALSE FALSE FALSE
> is.infinite(c(1,NA,-Inf,NaN))
[1] FALSE FALSE  TRUE FALSE

其中一件事与其他事情不同。毫不奇怪,还有一个is.nan功能。

于 2011-12-31T18:20:45.503 回答
11

randomForest 的“外部函数调用中的 NA/NaN/Inf”通常是错误警告,而且真的很烦人:

  • 如果传递的任何变量是字符,你会得到这个
  • 实际的 NaNs 和 Infs几乎从不发生在干净的数据中

我的快速而肮脏的技巧来缩小范围,对变量列表进行二进制搜索,并使用令牌参数ntree=2来获取变量子集的即时通过/失败:

RF <- randomForest(prePrior1[m:n],ntree=2,...)
于 2012-10-29T08:13:33.250 回答
4

与 类似is.na,您可以使用is.infinite来查找无穷大的出现。

于 2011-12-31T18:21:29.903 回答
2

看一下with,例如:

> with(df, df == Inf)
        foo   bar   baz   abc ...
[1,]  FALSE FALSE  TRUE FALSE ...
[2,]  FALSE  TRUE FALSE FALSE ...
...
于 2011-12-31T18:24:47.220 回答
1

joran 的答案是您想要的并且内容丰富。有关 and 的更多详细信息is.na()is.infinite()您应该查看https://stat.ethz.ch/R-manual/R-devel/library/Matrix/html/is.na-methods.html 以及在获得逻辑向量之后它表示原始向量的每个元素是否为NA/Inf,您可以使用该which()函数获取索引,就像这样:

> v1 <- c(1, Inf, 2, NaN, Inf, 3, NaN, Inf)
> is.infinite(v1)
[1] FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE
> which(is.infinite(v1))
[1] 2 5 8
> is.na(v1)
[1] FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
> which(is.na(v1))
[1] 4 7

文档在which()这里https://stat.ethz.ch/R-manual/R-devel/library/base/html/any.html

于 2015-11-26T06:51:45.637 回答