新决策树例子

更新时间:2023-11-22 07:07:02 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

rpart包的rpart函数

Iris数据集

library(rpart) #加载rpart包

head(iris) #看看iris数据集里有哪些变量

iris以鸢尾花的特征作为数据来源,数据集包含150个数据,分为3类,每类50个数据,每个数据包含4个属性分别是花萼长度、花萼宽带、花瓣长度、花瓣宽度

用gini度量纯度

iris.rp1=rpart(Species~.,data=iris,method=\split=\ini\

# rpart(formula, data, method, parms, ...)得到决策树对象,其中 (1)formula是回归方程的形式,y~x1+x2+?,iris一共有5个变量,因变

量是Species,自变量是其余四个变量,所以formula可以省略为Species~.

(2) data是所要学习的数据集 (3)method根据因变量的数据类型有如下几种选择:anova(连续型),poisson(计数型),class(离散型),exp(生存型),因为我们的因变量是花的种类,属于离散型,所以method选择class

(4)parms可以设置纯度的度量方法,有gini(默认)和information(信

息增益)两种。

plot(iris.rp1, uniform=T, branch=0, margin=0.1,main=\Tree\\nIris Species by Petal and Sepal Length\

#plot的对象是由rpart得到的决策树对象,可以把这课决策树画出来,其中

(1) uniform可取T,F两个值,T表示图形在空间上均匀分配

(2) branch刻画分支的形状,取值在0和1之间,branch=0是倒v型,

branch=1是直角型,而当branch属于(0,1)时是梯形

Classification TreeIris Species by Petal and Sepal Lengthbranch=0

Classification TreeIris Species by Petal and Sepal Lengthbranch=1|

Classification TreeIris Species by Petal and Sepal Lengthbranch=0.5|(3)margin刻画图的大小,margin越大,决策树越小,上述三幅图的margin=0.1,而当margin=1时,决策树变小了

Classification TreeIris Species by Petal and Sepal Length

(4)main是图的标题,其中“\\n”是换行的意思

text(iris.rp1, use.n=T, fancy=T, col=\

Classification TreeIris Species by Petal and Sepal LengthPetal.Length< 2.45Petal.Length>=2.45setosa 50/0/0Petal.Width< 1.75Petal.Width>=1.75versicolor0/49/5virginica 0/1/45

text(iris.rp1, use.n=T, fancy=F, col=\

Classification TreeIris Species by Petal and Sepal LengthPetal.Length< 2.45setosa 50/0/0Petal.Width< 1.75versicolor0/49/5virginica 0/1/45

(1) use.n=T,在每个节点处都会显示落在该节点的观测个数和对应的分类,

use.n=F时就不显示观测个数了。

(2) fancy=T、F的区别见上图

(3) col=“blue”就是写在树上的信息的颜色。

还有另一种画图函数 library(rpart.plot)

rpart.plot(iris.rp1,branch=0,branch.type=2,type=1,extra=1,shadow.col=\,main=\决策树\

决策树yesPetal.Le < 2.4setosa50 50 50nosetosa50 0 0Petal.Wi < 1.8versicol0 50 50versicol0 49 5virginic0 1 45

rpart.plot(iris.rp1,branch=0,branch.type=0,type=1,extra=1,shadow.col=\main=\决策树\

决策树yesPetal.Le < 2.4setosa50 50 50nosetosa50 0 0Petal.Wi < 1.8versicol0 50 50versicol0 49 5virginic0 1 45

(1) branch.type是分支的形状参数,branch.type=0时画出来的分支跟plot

里的一样,就是一条线,而当branch.type=2时,分支的宽度是数据的标准差,branch.type=1时,是方差 (2) type:type=0只对叶子节点画圆圈。type=1对根节点和叶子节点画圆圈,

分裂属性的值会写在节点框的上面。type=2时跟type=1近似,但分裂属性的值会写在节点框的下面。type=3时每个根节点左右分支对应的属性的取值范围都标出来了(之前那些都只标了左分支的属性取值),并且只对叶子节点画圆圈。type=4时,跟type=3近似,但对叶子节点和根节点都画圆圈。

结果解释:

以根节点为例,观测总数是532,其中177例糖尿病,355例正常,该节点的分类属性是正常,所以错判例数(loss)是177,(错判率,正确判断率)= (0.33270677 0.66729323)。如果根节点的观测满足glucose>=127.5(即128),则观测值落入左边的分支,否则落入右边的分支。

rpart.plot(pima.rp,branch=0,branch.type=0,type=1,extra=1,shadow.col=\gray\决策树\

决策树yesglucose >= 128normal177 355noglucose >= 158diabetic118 71diabetic64 12bmi >= 30normal54 59normal7 27bmi >= 26diabetic23 19glucose >= 96diabetic23 12normal3 12diabetic20 5normal3 7normal0 7age >= 28normal59 284pedigree >= 0.62normal43 86normal16 198age >= 42diabetic47 32diabetic18 2glucose >= 110normal20 67npregnan < 1.5normal13 22diabetic6 2normal7 20normal7 45pedigree >= 0.28normal29 30glucose < 136diabetic26 18diabetic11 2bmi >= 42normal15 16diabetic7 3bmi < 35normal8 13diabetic5 3normal3 10

图的解释:

1. 每个节点框中的数字是:(落在该节点有糖尿病的个数/正常的个数) 2. 每个节点框内的diabetic、normal表示该节点对应的分类属性

printcp(pima.rp) #列出cp值

cp: complexity parameter复杂性参数,用来修剪树的 当决策树复杂度超过一定程度后,随着复杂度的提高,测试集的分类精确度反而会降低。因此,建立的决策树不宜太复杂,需进行剪枝。该剪枝算法依赖于复杂性参数cp,cp随树复杂度的增加而减小,当增加一个节点引起的分类精确度变化量小于树复杂度变化的cp倍时,则须剪去该节点。故建立一棵既能精确分类,又不过度适合的决策树的关键是求解一个合适的cp值。一般选择错判率最小值对应的cp值来修树

plotcp(pima.rp)

size of tree1246101215X-val Relative Error0.6Inf0.70.80.91.01.10.120.0380.023cp0.0170.0130.0067

pima.prune=prune(pima.rp,cp=pima.rp$cptable[which.min(pima.rp$cptable[,\

修剪决策树yesglucose >= 128normal177 355noglucose >= 158diabetic118 71normal59 284diabetic64 12bmi >= 30normal54 59diabetic47 32normal7 27

xerror:交叉验证(cross validation)错判率的估计 xstd:交叉验证错判率的标准差

建立树模型要权衡两方面问题,一个是要拟合得使分组后的变异较小,另一个是要防止过度拟合,而使模型的误差过大,前者的参数是CP,后者的参数是Xerror。所以要在Xerror最小的情况下,也使CP尽量小。

Party包的ctree函数

party包的处理方式:它的背景理论是“条件推断决策树”(conditional inference trees):它根

据统计检验来确定自变量和分割点的选择。即先假设所有自变量与因变量均独立。再对它们进行卡方独立检验,检验P值小于阀值的自变量加入模型,相关性最强的自变量作为第一次分割的自变量。自变量选择好后,用置换检验来选择分割点。用party包建立的决策树不需要剪枝,因为阀值就决定了模型的复杂程度。所以如何决定阀值参数是非常重要的(参见ctree_control)。较为流行的做法是取不同的参数值进行交叉检验,选择误差最小的模型参数。

Ctree在Iris数据上的应用

##先载入包party ##构造分类树

iris_ctree<-ctree(Species~.,data=iris) ##画出分类树的图 plot(iris_ctree)

plot(iris_ctree,type=\

##比较用分类树预测的结果和实际结果之间的区别 table(predict(iris_ctree),iris$Species)

##由结果可以看出,由该决策树得到的结果的错误率为6/150=4%

Ctree在diabetes数据上的应用

##载入diabetes的数据

data2=read.table(\课件\\\\讨论班\\\\研一秋季学期\\\\我的讲稿\\\\pima.indians.diabetes3.txt\##观察diabetes的数据 head(data2)

##构造分类树

pima_ctree=ctree(class~npregnant+glucose+diastolic.bp+skinfold.thickness+bmi+pedigree+age,data=data2)

##画出分类树的图 plot(pima_ctree)

plot(pima_ctree,type=\

##计算错判率

##由结果可以看出,由该决策树得到的结果的错误率为109/532=20.5% ##能否修改参数改变模型?

ctree_control(teststat = c(\testtype = c(\

\mincriterion = 0.95, minsplit = 20, minbucket = 7, stump = FALSE, nresample = 9999, maxsurrogate = 0, mtry = 0, savesplitstats = TRUE, maxdepth = 0)

a character specifying the type of the test statistic to be applied. teststat testtype mincriterion

a character specifying how to compute the distribution of the test statistic. the value of the test statistic (for testtype == \), or 1 - p-value (for other values of testtype) that must be exceeded in order to implement a split.

minsplit minbucket stump nresample maxsurrogate mtry

the minimum sum of weights in a node in order to be considered for splitting.

the minimum sum of weights in a terminal node.

a logical determining whether a stump (a tree with three nodes only) is to be computed.

number of Monte-Carlo replications to use when the distribution of the test statistic is simulated.

number of surrogate splits to evaluate. Note the currently only surrogate splits in ordered covariables are implemented.

number of input variables randomly sampled as candidates at each node for random forest like algorithms. The default mtry = 0 means that no random selection takes place.

savesplitstats a logical determining if the process of standardized two-sample statistics

for split point estimate is saved for each primary split.

maxdepth

maximum depth of the tree. The default maxdepth = 0 means that no restrictions are applied to tree sizes.

##

##尝试修改mincriterion=0.6(mincriterion=0.95的结果与原来一样,故0.95应该为默认值) pima_ctree=ctree(class~npregnant+glucose+diastolic.bp+skinfold.thickness+bmi+pedigree+age,data=data2,control=ctree_control(mincriterion=0.6)) table(predict(pima_ctree),data2$class) ##计算错判率

##由结果可以看出,由该决策树得到的结果的错误率为97/532=18.2% plot(pima_ctree)

##此时树变得更复杂了

本文来源:https://www.bwwdw.com/article/dypv.html

Top