为什么与拟合模型评估和混淆矩阵的准确性存在很大差异。使用R

问题描述 投票:0回答:1

我正在尝试通过打印Model_RF_RF来解释我运行的随机森林模型的结果。但是,这些结果看起来与我通过手动比较混淆矩阵和准确性所获得的结果非常不同。

Model_RF_RF<-randomForest(Label ~ .,data = train.tokens.tfidf.df,ntree=500,mtry=82,importance=TRUE,proximity=TRUE,trControl = cv.cntrl,nodesize=10)
>Model_RF_RF

Call:
 randomForest(formula = Label ~ ., data = train.tokens.tfidf.df,      ntree = 500, mtry = 82, importance = TRUE, proximity = TRUE,      trControl = cv.cntrl, nodesize = 10) 
               Type of random forest: classification
                     Number of trees: 500
No. of variables tried at each split: 82

        OOB estimate of  error rate: 44.56%
Confusion matrix:
       HIGH LOW MEDIUM class.error
HIGH     46   3     72   0.6198347
LOW       3  25     93   0.7933884
MEDIUM   22  20    194   0.1779661
> confusionMatrix(PD5,train$Label )
Confusion Matrix and Statistics

          Reference
Prediction HIGH LOW MEDIUM
    HIGH    119   0      0
    LOW       1 120      3
    MEDIUM    1   1    233

Overall Statistics

               Accuracy : 0.9874          
                 95% CI : (0.9729, 0.9954)
    No Information Rate : 0.4937          
    P-Value [Acc > NIR] : <2e-16          

                  Kappa : 0.98            

 Mcnemar's Test P-Value : 0.3916          

Statistics by Class:

                     Class: HIGH Class: LOW Class: MEDIUM
Sensitivity               0.9835     0.9917        0.9873
Specificity               1.0000     0.9888        0.9917
Pos Pred Value            1.0000     0.9677        0.9915
Neg Pred Value            0.9944     0.9972        0.9877
Prevalence                0.2531     0.2531        0.4937
Detection Rate            0.2490     0.2510        0.4874
Detection Prevalence      0.2490     0.2594        0.4916
Balanced Accuracy         0.9917     0.9903        0.9895

对此行为有任何解释吗?

r confusion-matrix
1个回答
0
投票

欢迎使用堆栈溢出,Manu。区别在于,在Model_RF_RF的调用中显示的结果是OOB(袋装)结果,而最后打印的结果是训练集上的结果。

如您所知,Random Forests使用装袋法,这意味着它们使用数据的自举样本来种植树木。这意味着数据集中的每条记录仅会在您生长的所有树中的一小部分中使用,即在引导过程中绘制记录的树。因此,仅使用不包含引导程序中指定条目的树来预测条目,即可获得OOB分数,因此,每棵树都只能预测从未见过的数据-这可对测试进行良好的评估(通常有些悲观)错误。

因此,看起来您的训练准确性非常好,而您的测试却很低(如OOB估计所示)。您可以尝试在一些验证数据上测试模型或使用交叉验证,并且您应获得与OOB相似的分数。

尝试更改mtry的值,增加树的数量或进行更多的要素工程。祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.