【问题标题】:Why does decision tree give wrong classification in R?为什么决策树在 R 中给出错误的分类?
【发布时间】:2017-12-17 01:22:28
【问题描述】:

由于为 Fisher 的虹膜数据创建决策树,我得到了错误分类错误率:0.02667 = 4 / 150。但我在我的图中只看到 3 个错误: DS for the iris.

如果我们查看这一点的概率 - 没关系(弗吉尼亚州 - 与上图相同):

   setosa versicolor  virginica
   0      0.1666667   0.83333333

你能解释一下为什么会发生这种错误分类(4 个错误,而不是图中清楚描绘的 3 个)吗?

代码:

# install.packages("tree")
# install.packages("ggplot2")

library('tree')
library('ggplot2') 

data(iris)

iris <- iris[ , c('Petal.Length', 'Petal.Width', 'Species')]
myTree <- tree(Species ~ Petal.Length + Petal.Width, data = iris)
summary(myTree)

# Classification tree:
# tree(formula = Species ~ Petal.Length + Petal.Width, data = iris)
# Number of terminal nodes:  5 
# Residual mean deviance:  0.157 = 22.77 / 145 
# Misclassification error rate: 0.02667 = 4 / 150 

# The errors were found by comparing predict(myTree, iris, type="class")
# with native data set 
errors <- data.frame(
Species = c('versicolor', 'versicolor', 'versicolor', 'virginica'),
Petal.Length = c(4.8, 5.0, 5.1, 4.5), Petal.Width = c(1.8, 1.7, 1.6, 1.7))

ggplot(iris, aes(x = Petal.Length, y = Petal.Width, colour = Species)) + 
 geom_point(size = 2.1) +
 geom_vline(xintercept = 2.45) +
 geom_hline(yintercept = 1.75) +
 geom_vline(xintercept = 4.95) + 
 geom_point(data = errors, shape = 1, size = 5,colour = "black")

【问题讨论】:

  • 您将需要共享用于构建模型并获得预测的代码。否则我们能做的最好的就是说你要么编码错误,要么解释了一些输出错误。
  • 请指定您使用的任何包并包括加载它们的代码。旁注:你为什么称树DT?通常这意味着该对象是一个 data.table。还要说明你是如何确定你认为哪些是错误分类的。
  • 我建议添加用于制作图表的代码和用于确定哪些被错误分类的代码。请注意,花瓣长度 = 4.8 和花瓣宽度 = 1.8 的观察结果不止一个
  • ...我不希望您手动构建错误数据帧。这根本没有帮助 - 我试图让你展示你用来找出哪些错误的代码......无论哪种方式 - 看看我的答案 - 没有出错。

标签: r statistics decision-tree


【解决方案1】:

您正在查看的点没有分类错误。

但当时有多个观察结果,它们并不都具有相同的物种。为情节添加一些抖动...

ggplot(iris, aes(x = Petal.Length, y = Petal.Width, colour = Species)) +
   geom_point(position = "jitter") +
   geom_vline(xintercept = 4.95) + geom_vline(xintercept = 2.45) + geom_hline(yintercept = 1.75)

你会看到实际发生了什么。

从数据...

> iris[iris$Petal.Length == 4.8 & iris$Petal.Width == 1.8,]
    Petal.Length Petal.Width    Species
71           4.8         1.8 versicolor
127          4.8         1.8  virginica
139          4.8         1.8  virginica

【讨论】:

    猜你喜欢
    • 2016-09-17
    • 2017-05-03
    • 2013-03-16
    • 2016-03-03
    • 2014-12-17
    • 2018-06-30
    • 2018-05-03
    • 2019-02-26
    • 2021-06-13
    相关资源
    最近更新 更多