【问题标题】:Classification output in random forest with randomForest package使用 randomForest 包在随机森林中的分类输出
【发布时间】:2019-04-15 16:02:01
【问题描述】:

我想在随机森林中使用 predict() 函数创建分类输出 下面是我的代码:

#Packages
library(randomForest)
library(dplyr)

#Data
data(iris)
Rep<-seq(1,length(iris[,1]))
all_iris<-cbind(Rep,iris)


#Tranning RF
dg_o_cal<-all_iris %>% sample_n(150*0.8)
iris.rf <- randomForest(Species ~ Sepal.Length + Sepal.Width + Petal.Length 
+ Petal.Width, data=dg_o_cal, importance=TRUE, proximity=TRUE)

# Predicting on Validation set 
dg_s_val<-anti_join(all_iris,dg_o_cal, by=c("Rep"))
predValid <- predict(iris.rf, dg_s_val, type = "class")
# Checking classification accuracy
mean(predValid == dg_s_val$Species)                    
table(predValid,dg_s_val$Species)
#

但我喜欢 dg_s_val 对象中数据帧格式的输出,其中包含一个带有 RF 分类 (CLASS_RF) 的新列,其中预测验证集结果如下:

  Rep Sepal.Length Sepal.Width Petal.Length Petal.Width Species CLASS_RF
1   6          5.4         3.9          1.7         0.4  setosa setosa
2   9          4.4         2.9          1.4         0.2  setosa virginica
3  10          4.9         3.1          1.5         0.1  setosa virginica
4  11          5.4         3.7          1.5         0.2  setosa setosa
5  15          5.8         4.0          1.2         0.2  setosa setosa
6  20          5.1         3.8          1.5         0.3  setosa setosa
...

有可能吗?

【问题讨论】:

    标签: r random-forest


    【解决方案1】:

    像这样:

    dg_s_val$CLASS_RF <- predValid
    

    您正在将列表 predValid 分配给 dg_s_val 中的列

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 2016-11-11
      • 2019-09-05
      • 2013-09-22
      • 2018-02-18
      • 2013-02-06
      • 2018-05-20
      • 2016-05-25
      • 2020-08-16
      相关资源
      最近更新 更多