【问题标题】:Show the outcome of decision tree in a confusionmatrix在混淆矩阵中显示决策树的结果
【发布时间】:2016-01-07 03:28:10
【问题描述】:

我试图预测比赛的结果。因此,我在测试和训练集上使用 rpart 算法。

当我训练我的算法时,我会这样做:

 tree <- rpart(won ~ EXPG1 + EXPG2, data=training, method="class")

通过这个我可以预测一场比赛的最终结果是 0,1 还是 2。

 predict <- predict(tree, testing)

这导致以下结果:

 head(predict)
       0         1         2
2  0.7368421 0.1578947 0.1052632
7  0.2777778 0.5000000 0.2222222

这一切都很好。但现在我想在混淆矩阵中比较我的结果。

但这显然行不通

confusionMatrix(testing$won, predict)

因为我的 testing$won 列仅包含 0,1 或 2。关于如何在混淆矩阵中查看我的结果有什么建议吗?

【问题讨论】:

    标签: r decision-tree


    【解决方案1】:

    将类型传递给预测函数以返回类标签:

    predict <- predict(tree, testing, type = "class")
    

    完整的工作示例:

    library(rpart)
    library(caret)
    
    data(iris)
    
    mod <- rpart(Species~., iris)
    p <- predict(mod, type="class")
    

    然后输出:

    > confusionMatrix(p, iris$Species)
    Confusion Matrix and Statistics
    
                Reference
    Prediction   setosa versicolor virginica
      setosa         50          0         0
      versicolor      0         49         5
      virginica       0          1        45
    
    Overall Statistics
    
                   Accuracy : 0.96           
                     95% CI : (0.915, 0.9852)
        No Information Rate : 0.3333         
        P-Value [Acc > NIR] : < 2.2e-16      
    
                      Kappa : 0.94           
     Mcnemar's Test P-Value : NA             
    
    Statistics by Class:
    
                         Class: setosa Class: versicolor Class: virginica
    Sensitivity                 1.0000            0.9800           0.9000
    Specificity                 1.0000            0.9500           0.9900
    Pos Pred Value              1.0000            0.9074           0.9783
    Neg Pred Value              1.0000            0.9896           0.9519
    Prevalence                  0.3333            0.3333           0.3333
    Detection Rate              0.3333            0.3267           0.3000
    Detection Prevalence        0.3333            0.3600           0.3067
    Balanced Accuracy           1.0000            0.9650           0.9450
    

    【讨论】:

      猜你喜欢
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 2019-09-28
      • 2020-07-25
      • 1970-01-01
      • 2014-07-09
      相关资源
      最近更新 更多