【问题标题】:How to compute error rate from a decision tree?如何从决策树计算错误率?
【发布时间】:2012-03-28 19:14:49
【问题描述】:

有谁知道如何用 R 计算决策树的错误率? 我正在使用rpart() 函数。

【问题讨论】:

    标签: r classification decision-tree rpart


    【解决方案1】:

    假设您的意思是计算用于拟合模型的样本的错误率,您可以使用printcp()。比如使用网上的例子,

    > library(rpart)
    > fit <- rpart(Kyphosis ~ Age + Number + Start, data=kyphosis)
    > printcp(fit)
    
    Classification tree:
    rpart(formula = Kyphosis ~ Age + Number + Start, data = kyphosis)
    
    Variables actually used in tree construction:
    [1] Age   Start
    
    Root node error: 17/81 = 0.20988
    
    n= 81 
    
            CP nsplit rel error  xerror    xstd
    1 0.176471      0   1.00000 1.00000 0.21559
    2 0.019608      1   0.82353 0.82353 0.20018
    3 0.010000      4   0.76471 0.82353 0.20018
    

    Root node error 用于计算预测性能的两种度量,当考虑在rel errorxerror 列中显示的值时,并取决于复杂性参数(第一列):

    • 0.76471 x 0.20988 = 0.1604973 (16.0%) 是重新替换错误率(即,在训练样本上计算的错误率)——这大约是

      class.pred <- table(predict(fit, type="class"), kyphosis$Kyphosis)
      1-sum(diag(class.pred))/sum(class.pred)
      
    • 0.82353 x 0.20988 = 0.1728425 (17.2%) 是交叉验证的错误率(使用 10 倍 CV,请参阅 rpart.control() 中的 xval;但另请参阅 @987654329 @ 和 plotcp() 依赖于这种措施)。该度量是预测准确性的更客观指标。

    请注意,它或多或少与tree的分类准确度一致:

    > library(tree)
    > summary(tree(Kyphosis ~ Age + Number + Start, data=kyphosis))
    
    Classification tree:
    tree(formula = Kyphosis ~ Age + Number + Start, data = kyphosis)
    Number of terminal nodes:  10 
    Residual mean deviance:  0.5809 = 41.24 / 71 
    Misclassification error rate: 0.1235 = 10 / 81 
    

    其中Misclassification error rate 是根据训练样本计算得出的。

    【讨论】:

      猜你喜欢
      • 2016-09-27
      • 2018-07-03
      • 2014-04-15
      • 2016-12-08
      • 2011-09-11
      • 2016-05-11
      • 2021-11-14
      • 2014-07-20
      相关资源
      最近更新 更多