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
需要成为一个因素的响应,没有明确的帮助,但暗示:
Run Code Online (Sandbox Code Playgroud)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)
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)Error in y - ymean : non-numeric argument to binary operator
转换Species
回因子后,randomForest
运行没有错误.
iris$Species = factor(iris$Species)
rf <- randomForest(Species ~ ., data=iris)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9907 次 |
最近记录: |