环球网校是美国纳斯达克上市企业欢聚时代(NASDAQ:YY)旗下品牌 | 住房和城乡建设部 建筑人才培训合作单位
您现在的位置在: > 财会考试 > 会计从业资格 > 考试动态 >

数据分类这一章节的重点内容总结(二)步骤

2023-01-03 来源:网络 作者:佚名

决策树,属于数据分类这一章节的重点内容,这里我单独拿出一个篇幅去整理总结决策树的相关知识。

#

首先理解什么是决策树,是对预测变量进行二元分离,从而构造一棵可用于预测新样本单元所属类别的树。 #

期次是决策树的分类:经典树和条件推断树。 #

下面依次介绍这两种决策树。

#

经典决策树,以一个二元输出为变量,一组预测变量为基础,按如下的方法构造决策树,这里为了方便描述,我们还是借用威斯康星州乳腺癌数据集中的良性/恶性数据进行分析 #

选定一个最佳预测变量将全部样本单元分为两类,实现两类中的纯度最大化(即一类中良性样本单元尽可能多,另一类中恶性样本单元尽可能多)。如果预测变量连续,则选定一个分割点进行分类,使得两类纯度最大化对每一个子类别继续执行步骤1重复步骤(1)~(2),直到子类别中所含的样本单元数过少,或者没有分类法能将不纯度下降到一个给定阈值以下。最终集中的子类别即终端节点( node)。根据每一个终端节点中样本单元的类别数众数来判别这一终端节点的所属类别

#

存在的问题:上述算法通常会得到一棵过大的树,从而出现过拟合现象。结果就是决策树的应用范围,对于训练集外单元的分类性能较差。为解决这一问题,可采用10折交叉验证法选择预测误差最小的树。这一剪枝后的树即可用于预测。

#

R语言中的实现 #

library(rpart)
> set.seed(1234)
> dtree <- rpart(class ~ ., data=df.train, method="class",      
+                parms=list(split="information")) 
#
#

使用rpart函数构造决策树,它被包含与rpart,所以需要提前安装这个安装包 #

另外在构造决策树时的参数

#

前面2个分别是与data, class~. ,响应变量为class,预测变量为其他的变量,数据集是df.train,=分类

#

在官方文档中是这样描述的:方法可以是方差分析,泊松,分类以及指数
one of "anova", "poisson", "class" or "exp". 
If method is missing then the routine tries to make an intelligent guess. 
If y is a survival object, then method = "exp" is assumed,
 if y has 2 columns then method = "poisson" is assumed, 
if y is a factor then method = "class" is assumed,
 otherwise method = "anova" is assumed. 
It is wisest to specify the method directly, 
especially as more criteria may added to the function in future.
Alternatively, method can be a list of functions named init, split and eval.
 Examples are given in the file ‘tests/usersplits.R’ in the sources, 
and in the vignettes ‘User Written Split Functions’. 
#
#

parms	
optional parameters for the splitting function.
Anova splitting has no parameters.
Poisson splitting has a single parameter, the coefficient of variation of the 
prior distribution on the rates. The default value is 1.
Exponential splitting has the same parameter as Poisson.
For classification splitting, the list can contain any of: 
the vector of prior probabilities (component prior),
the loss matrix (component loss) or 
the splitting index (component split). 
The priors must be positive and sum to 1. 
The loss matrix must have zeros on the diagonal and positive off-diagonal elements. The splitting index can be gini or information. The default priors are proportional to the data counts, the losses default to 1, and the split defaults to gini # 
#

列举决策树的构成 #

> dtree$cptable
        CP nsplit rel error  xerror       xstd
1 0.800000      0   1.00000 1.00000 0.06484605
2 0.046875      1   0.20000 0.30625 0.04150018
3 0.012500      3   0.10625 0.20625 0.03467089
4 0.010000      4   0.09375 0.18125 0.03264401 # 

#

rpart()返回的值中包括不同大小的树对应的预测误差,因此可用于辅助设定最终的树的大小,

#

通过()函数可画出交叉验证误差与复杂度参数的关系图。对于所有交叉验证误差在最小交叉验证误差一个标准差范围内的树,最小的树即最优的树 #

> plotcp(dtree) # 

#

本例中,最小的交叉验证误差为0.18,标准误差为0.0326,则最优的树为交叉验证误差在 #

0.18±0.0326(0.15和0.21)之间的树。四个终端节点(即三次分割)的树满足要求(交叉验证误差为0.206 25);根据图也可以选得最优树,即三次分割(n=四个节点)对应的树。

#

之后进行剪枝的操作,目的是简化树的结构 #

在完整树的基础上,prune()函数根据复杂度参数剪掉最不重要的枝,从而将树的大小控制在理想范围内。从代码清单17-3的中可以看到,最优树对应的复杂度参数为0.0125,从而prune(dtree, cp=0.0125)可得到一个理想大小的树。

#

dtree.pruned <- prune(dtree, cp=.0125) # 
#

rpart.plot包中的prp()函数可用于画出最终的决策树 #

library(rpart.plot)
prp(dtree.pruned, type = 2, extra = 104,  
    fallen.leaves = TRUE, main="Decision Tree") # 

#

type=2可画出每个节点下分割的标签 #

2 Like 1 but draw the split below the node #

extra=104可画出每一类的概率以及每个节点处的样本占比 #

extra=104 class model with a more than two #

.=TRUE可在图的底端显示终端节点

#

FALSE. If TRUE, the leaf nodes at the of the graph #

对观测点分类时决策树的应用范围,从树的顶端开始,若满足条件则从左枝往下,否则从右枝往下,重复这个过程直到碰到一个终端节点为止。该终端节点即为这一观测点的所属类别。 #

接下来就是要预测数据,以及验证模型的准确性

#

> dtree.pred <- predict(dtree.pruned, df.validate, type="class")
> dtree.perf <- table(df.validate$class, dtree.pred, 
+                     dnn=c("Actual", "Predicted"))
> dtree.perf
           Predicted
Actual      benign malignant
  benign       122         7
  malignant      2        79 # 
#

准确性=(122+79)/(122+7+79+2)=201/210=95.7%

#

下面讲述另外一种重要的树:条件推断树 #

条件推断树与传统决策树类似,但变量和分割的选取是基于显著性检验的,而不是纯净度或同质性一类的度量。显著性检验是置换检验

#

(1) 对输出变量与每个预测变量间的关系计算p值。

#

(2) 选取p值最小的变量。 #

(3) 在因变量与被选中的变量间尝试所有可能的二元分割(通过排列检验),并选取最显著的

#

分割。

#

(4) 将数据集分成两群,并对每个子群重复上述步骤。

#

(5) 重复直至所有分割都不显著或已到达最小节点为止。 #

code实现如下

#

library(party)
fit.ctree <- ctree(class~., data=df.train) # 

#

条件推断树可由party包中的ctree()函数获得

#

plot(fit.ctree, main="Conditional Inference Tree") # 
#

每个节点中的阴影区域代表这个节点对应的恶性肿瘤比例。

#

对数据进行预测,并验证算法的准确性 #

> ctree.pred <- predict(fit.ctree, df.validate, type="response")
> ctree.perf <- table(df.validate$class, ctree.pred, 
+                     dnn=c("Actual", "Predicted"))
> ctree.perf
           Predicted
Actual      benign malignant
  benign       122         7
  malignant      3        78 # 

#

算法准确性:(122+78)/(122+78+7+3)=200/210=95.2% #

总结: #

本章主要学历了决策树,典型树和条件推断树 #

学习了树的构造方法

#

学习典型树中如何选择最优树,以及对树进行裁剪优化

#

预测数据以及验证了算法的准确性 #

以及图形化树的结构

#

责编:admin 返回顶部  打印

关于我们联系我们友情链接网站声明网站地图广告服务帮助中心