在 R 中使用 randomforest() 进行分类? [英] Use of randomforest() for classification in R?
问题描述
我最初有一个由 N 行 12 列组成的数据框.最后一列是我的班级(0 或 1).我不得不使用
I originally had a data frame composed of 12 columns in N rows. The last column is my class (0 or 1). I had to convert my entire data frame to numeric with
training <- sapply(training.temp,as.numeric)
但后来我认为我需要将类列作为因子列才能使用 randomforest() 工具作为分类器,所以我做到了
But then I thought I needed the class column to be a factor column to use the randomforest() tool as a classifier, so I did
training[,"Class"] <- factor(training[,ncol(training)])
我继续创建树
training_rf <- randomForest(Class ~., data = trainData, importance = TRUE, do.trace = 100)
但我收到两个错误:
1: In Ops.factor(training[, "Status"], factor(training[, ncol(training)])) :
<= this is not relevant for factors (roughly translated)
2: In randomForest.default(m, y, ...) :
The response has five or fewer unique values. Are you sure you want to do regression?
如果有人能指出我所犯的格式错误,我将不胜感激.
I would appreciate it if someone could point out the formatting mistake I'm making.
谢谢!
推荐答案
所以问题其实很简单.结果证明我的训练数据是一个原子向量.所以它首先必须被转换为数据帧.所以我需要添加以下行:
So the issue is actually quite simple. It turns out my training data was an atomic vector. So it first had to be converted as a data frame. So I needed to add the following line:
training <- as.data.frame(training)
问题解决了!
这篇关于在 R 中使用 randomforest() 进行分类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!