【问题标题】:Error in y - ymean : non-numeric argument to binary operator randomForest Ry - ymean 中的错误:二元运算符 randomForest R 的非数字参数
【发布时间】:2017-01-12 05:21:29
【问题描述】:

我有一个大约 37k x 1024 的矩阵,由 1 和 0 作为分类变量组成,用于指示特征向量的存在或不存在。我通过 R 中的 randomForest 包运行了这个矩阵,如下所示:

rfr <- randomForest(X_train,Y_train)

其中 X_train 是包含分类变量的矩阵,Y__train 是由矩阵中每一行的标签组成的向量。当我运行它时,我收到以下错误:

Error in y - ymean : non-numeric argument to binary operator
In addition: Warning message:
In mean.default(y) : argument is not numeric or logical: returning NA

我检查了任何空值或丢失的数据,但没有找到。

我什至把整个东西做成了一个data.frame并尝试了以下

rfr <- randomForest(labels ~ ., data = featureDF)

仍然有同样的错误。

如果有任何帮助,我将不胜感激,谢谢!

【问题讨论】:

    标签: r matrix statistics random-forest


    【解决方案1】:

    我猜labels 是一个字符变量,但randomForest 期望分类结果变量是因素。将其更改为一个因子,看看错误是否消失:

    featureDF$labels = factor(featureDF$labels) 
    

    randomForest 的帮助没有明确说明响应需要成为一个因素,但它是暗示的:

    y  A response vector. If a factor, classification is assumed, otherwise   
       regression is assumed. If omitted, randomForest will run in unsupervised mode.
    

    您还没有提供示例数据,所以这里有一个内置 iris 数据的示例:

    Species 是原始数据框中的一个因素。让我们将Species 转换为字符:

    iris$Species = as.character(iris$Species)
    rf <- randomForest(Species ~ ., data=iris)
    
    Error in y - ymean : non-numeric argument to binary operator
    

    Species 转换回因子后,randomForest 运行没有错误。

    iris$Species = factor(iris$Species)
    rf <- randomForest(Species ~ ., data=iris)
    

    【讨论】:

      猜你喜欢
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 2018-05-04
      • 2019-05-03
      • 2016-11-14
      • 2021-08-18
      相关资源
      最近更新 更多