首页 文章

随机森林:用于识别特定正确预测记录的R代码

提问于
浏览
-1

使用简单的示例数据集“iris”,使用“trainData”训练随机森林模型,使用“testData”预测/分类物种 .

# 1 - Create a Random Forest Model. 
iris.rf <- randomForest(Species ~ ., data=trainData)  
# 2 - Predict using Model and Test data set 
iris.pred <- predict(iris.rf, testData)  
# 3 - Show Crosstab results:
table(observed = testData$Species, predicted = iris.pred)

Question:
上面的第3步,
为您提供一个包含总数的表格
测试记录分类正确和错误 .

获取"testData"中特定记录列表的R代码是什么,
这是:
(a) 正确预测 - 或 -
(b) 未正确预测,
(即:预测的$物种是错误的) .

Reason for my Question:
我想知道
如果有一些宝贵的见解
通过查看具体的个人记录
在随机森林会话中被错误分类的 .

也许这是一种额外的(可能的)方式
提高准确性
随机森林预测...

1 回答

  • 0

    根据我的评论,我认为你需要一个二元结果变量来研究特异性和敏感性,但这是你的答案的开始:

    library(randomForest)
    data(iris)
    set.seed(44)
    indexes = sample(2, length(iris), replace = TRUE, prob = c(0.3,0.7))
    trainData<-iris[indexes==2,]
    testData<-iris[indexes==1,]
    iris.rf <- randomForest(Species ~ ., data=trainData)
    iris.pred <- predict(iris.rf, testData)
    
    truePositives<-which(iris.pred==testData$Species)
    falsePositives<-which(iris.pred!=testData$Species)
    truePositives
     [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 16 17 18 19 20 21 22 23 24
    [24] 25 26 27 28 29 30
    falsePositives
    [1] 15
    
    head(testData[truePositives,])
       Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    1           5.1         3.5          1.4         0.2  setosa
    6           5.4         3.9          1.7         0.4  setosa
    11          5.4         3.7          1.5         0.2  setosa
    16          5.7         4.4          1.5         0.4  setosa
    21          5.4         3.4          1.7         0.2  setosa
    26          5.0         3.0          1.6         0.2  setosa
    

相关问题