【问题标题】:Decision Trees with R带有 R 的决策树
【发布时间】:2014-08-20 16:16:27
【问题描述】:

我从 rpart-manpage 运行该示例

tree <- rpart(Species~., data = iris)
plot(tree,margin=0.1)
text(tree)

现在我想为另一个数据集修改它

digitstrainURL <- "http://archive.ics.uci.edu/ml/machine-learning-databases/pendigits/pendigits.tra"
digitsTestURL <- "http://archive.ics.uci.edu/ml/machine-learning-databases/pendigits/pendigits.tes"
digitstrain <- read.table(digitstrainURL, sep=",",
                          col.names=c("i1","i2","i3","i4","i5","i6","i7","i8","i9","i10","i11","i12","i13","i14","i15","i16", "Class"))
digitstest <- read.table(digitsTestURL, sep=",",
col.names=c("i1","i2","i3","i4","i5","i6","i7","i8","i9","i10","i11","i12","i13","i14","i15","i16", "Class"))

tree <- rpart(Class~., data = digitstrain)
plot(tree,margin=0.1)
text(tree)

数据集包含手写数字的数据,“类”包含数字 0-9 但是当我绘制树时,我得到了奇怪的浮点数,知道这些数字是什么意思吗?我更喜欢 0-9 作为叶子的文本。

【问题讨论】:

    标签: r machine-learning decision-tree


    【解决方案1】:

    您正在尝试拟合分类树,但您的数据是整数,而不是因子。

    函数rpart 将尝试猜测要使用的方法,在你的情况下是错误的猜测。因此,您的代码适合基于method="anova" 的树,而您想使用method="class"

    试试这个:

    tree <- rpart(Class~., data = digitstrain, method="class")
    plot(tree,margin=0.1)
    text(tree, cex=0.7)
    

    要测试模型的准确性,您可以使用predict 获取预测值,然后创建混淆矩阵:

    confusion <- data.frame(
      class=factor(digitstest$Class), 
      predict=predict(tree, digitstest, type="class")
      )
    with(confusion, table(class, predict))
    
         predict
    class   0   1   2   3   4   5   6   7   8   9
        0 311   1   0   0   0   0   0   7  42   2
        1   0 139 186   4   0   0   0   1  10  24
        2   0   0 320  14   2   3   0   7  15   3
        3   0   6   0 309   1   3   0  17   0   0
        4   0   1   0   5 300   0   0   0   0  58
        5   0   0   0  74   0 177   0   1  14  69
        6   5   0   3   9  12   0 264  11   5  27
        7   2   9  11  13   0  10   0 290   0  29
        8  60   0   0   0   0  32   0  21 220   3
        9   1  44   0   9  20   0   0   8   0 254
    

    请注意,使用单个树的预测效果不佳。改进预测的一种非常简单的方法是使用随机森林,它由许多与训练数据的随机子集匹配的树组成:

    library(randomForest)
    
    fst <- randomForest(factor(Class)~., data = digitstrain, method="class")
    

    观察森林给出的预测结果要好得多:

    confusion <- data.frame(
      class=factor(digitstest$Class), 
      predict=predict(fst, digitstest, type="class")
      )
    with(confusion, table(class, predict))
    
         predict
    class   0   1   2   3   4   5   6   7   8   9
        0 347   0   0   0   0   0   0   0  16   0
        1   0 333  28   1   1   0   0   1   0   0
        2   0   5 359   0   0   0   0   0   0   0
        3   0   4   0 331   0   0   0   0   0   1
        4   0   0   0   0 362   1   0   0   0   1
        5   0   0   0   8   0 316   0   0   0  11
        6   1   0   0   0   0   0 335   0   0   0
        7   0  26   2   0   0   0   0 328   0   8
        8   0   0   0   0   0   0   0   0 336   0
        9   0   2   0   0   0   0   0   2   1 331
    

    【讨论】:

    • 非常感谢!结果不应该适合所有训练数据吗?我挑选了一些数据并自己检查了它,但它并没有给我正确的结果。是否有可能向树提供数据并获得结果?
    • @user2071938 一棵树并不能保证一个好的模型,但随机森林模型通常会表现得更好。我已经扩展了我的答案。
    【解决方案2】:

    发生这种情况是因为您的 Class 列是数字。将其转换为因子然后尝试...

    digitstrain$Class = as.factor(digitstrain$Class)
    tree <- rpart(Class~., data = digitstrain)
    plot(tree,margin=0.1)
    text(tree)
    

    结果是

    【讨论】:

      猜你喜欢
      • 2016-05-18
      • 2014-08-21
      • 2011-05-02
      • 2018-07-24
      • 2015-07-25
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 2017-05-01
      相关资源
      最近更新 更多