y - ymean中的错误:二元运算符randomForest R的非数字参数

use*_*224 5 statistics r matrix random-forest

我有一个大约37k x 1024的矩阵,由1和0组成,作为分类变量来表示特征向量的存在与否.我通过R中的randomForest包运行这个矩阵,如下所示:

rfr <- randomForest(X_train,Y_train)
Run Code Online (Sandbox Code Playgroud)

其中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
Run Code Online (Sandbox Code Playgroud)

我检查了任何空值或缺少数据,但没有找到任何.

我甚至把整个事情都变成了data.frame并尝试了以下内容

rfr <- randomForest(labels ~ ., data = featureDF)
Run Code Online (Sandbox Code Playgroud)

仍然有同样的错误.

我很感激任何帮助,谢谢!

eip*_*i10 13

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

featureDF$labels = factor(featureDF$labels) 
Run Code Online (Sandbox Code Playgroud)

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

y  A response vector. If a factor, classification is assumed, otherwise   
   regression is assumed. If omitted, randomForest will run in unsupervised mode.
Run Code Online (Sandbox Code Playgroud)

您还没有提供示例数据,因此这里是内置iris数据的示例:

Species是原始数据框中的一个因素.让我们转换Species为角色:

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

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

iris$Species = factor(iris$Species)
rf <- randomForest(Species ~ ., data=iris)
Run Code Online (Sandbox Code Playgroud)