RDataMining系列:Chapter 4 Decision Trees --决策树实现,未完待续

 

 

*****************

利用party来做决策树分类

*****************

数据:iris data

目标:

利用Sepal.Length, Sepal.Width,Petal.Length and Petal.Width 来预测 Species of flowers.

预处理:

分成训练,测试样本集:

> set.seed(1234)
> ind <- sample(2, nrow(iris), replace=TRUE, prob=c(0.7, 0.3))
> trainData <- iris[ind==1,]
> testData <- iris[ind==2,]

接下里就是

1.Load package party,

2.build a decision tree,

3.and check the prediction.

> library(party)
> myFormula <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
> iris_ctree <- ctree(myFormula, data=trainData)
> # check the prediction
> table(predict(iris_ctree), trainData$Species)

                        setosa         versicolor          virginica
setosa                     40                   0                     0
versicolor                   0                  37                    3
virginica                     0                    1                   31

 

下面主要是分析结果:即分析得到的决策树

>  print(iris_ctree)

 

>  plot(iris_ctree)

 

>  plot(iris_ctree,  type="simple")

 

在测试样本上使用决策树得到预测结果

> # predict on test data
> testPred <- predict(iris_ctree, newdata = testData)
> table(testPred, testData$Species)
 testPred            setosa        versicolor          virginica
setosa                     10                   0                    0
versicolor                   0                 12                    2
virginica                     0                   0                  14

 

注意的问题:

The current version of ctree (i.e. version 0.9-9995) does not handle missing values well. An
instance with a missing value may sometimes go to the left sub-tree and sometimes to the right.

Another issue is that, when a variable exists in training data and is fed into ctree but does not
appear in the built decision tree, the test data must also have that variable to make prediction.
Otherwise, a call to predict would fail. Moreover, if the value levels of a categorical variable
in test data are different from that in train data, it would also fail to make prediction on the
test data. One way to get around the above issue is, after building a decision tree, to call ctree
build a new decision tree with data containing only those variables existing in the first tree, and
to explicitly set the levels of categorical variables in test data to the levels of the corresponding
variables in train data.

 **********************************

4.2    Building Decision Trees with Package rpart

**********************************

未完待续

*****************

4.3    Random Forest

*****************

Package randomForest 中的 cforest可以来构建 Random Forest进行预测

第一步:The iris data is split below into two subsets: training (70%) and testing (30%).

> ind <- sample(2, nrow(iris), replace=TRUE, prob=c(0.7, 0.3))
> trainData <- iris[ind==1,]
> testData <- iris[ind==2,]

第二步:

Load randomForest and then train a random forest.
> library(randomForest)
> rf <- randomForest(Species ~ ., data=trainData, ntree=100, proximity=TRUE)
> table(predict(rf), trainData$Species)

                      setosa         versicolor           virginica
setosa                   38                    0                    0
versicolor                 0                  33                    2
virginica                   0                    2                  28

*****************

后面未完待续

*****************

 

 

 

 

 

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/548689.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hicp第五天

条件匹配 指定路由器----DR----和MA网络中其他所有设备建立邻接关系 备份指定路由器----BDR----和MA网络中其他所有设备建立邻接关系 在DR和BDR都存在的情况下&#xff0c;一个MA网络里最少需要4台设备才会出现邻居关系 DR和BDR虽然叫指定路由器&#xff08;备份路由器&#xf…

第六天hcip笔记

OSPF的不规则区域 区域划分的要求&#xff1a; 1&#xff0c;区域之间必须存在ABR设备 2&#xff0c;区域划分必须按照星型拓扑结构划分。 1&#xff0c;远离骨干的非骨干区域 2&#xff0c;不连续骨干 1&#xff0c;使用VPN隧道使非法的ABR合法化 在这个过程中&#xff0c;可…

在VMWare Workstation 8.0.1中安装苹果MAC OS X Lion Part 2

选择虚拟磁盘的类型为“SCSI”。如图12。 图12 12. 指定虚拟磁盘的容量&#xff0c;建议至少20G。为了提高磁盘性能&#xff0c;建议选择“单个文件存储虚拟磁盘”。如图13。 图13 13. 为虚拟磁盘指定一个名称。如图14。 图14 14. 如图15为汇总信息&#xff0c;如果…

第二天实验hcip

一、给R1-R7配置IP地址和环回地址&#xff0c;并检查配置结果 1&#xff0c;R1 [Huawei]int loopback0 [Huawei-LoopBack0]ip add 1.1.1.1 24 [Huawei-LoopBack0]int g0/0/0 [Huawei-GigabitEthernet0/0/0]ip add 12.1.1.1 24 [Huawei-GigabitEthernet0/0/0]int g0/0/1 [Huaw…

hcip第一次实验

子网划分&#xff1a; 由实验要求图可知R1、R2、R4均需两个网段代表使用百兆和千兆时的不同环回&#xff0c;R5需要一个网段代表环回&#xff0c;同时为了路由表尽量少&#xff0c;因此只需将192.168.1.0/24分为8个网段&#xff1a; 192.168.1.0/27&#xff08;主网段&#xff…

中小企业实施MES管理系统需要注意的问题

在当今高度自动化的制造业环境中&#xff0c;MES管理系统发挥着越来越重要的作用。作为车间制造运营的核心&#xff0c;MES系统提供了一个集中的平台&#xff0c;使制造商能够实时监控、管理和优化其生产线。本文将深入探讨MES管理系统的定义、实施的好处以及在实施过程中可能遇…

告别忙碌

大学上管理课时有位老师说&#xff1a;“有些老板总说自己忙&#xff0c;并以之为荣。实际上&#xff0c;是他们不善于时间管理。”电影《无极》中有句话&#xff1a;“真正的速度是看不见的”。这句话可以用量子力学来证明&#xff0c;你无法知道下一秒它在哪出现&#xff0c;…

hcip第四天实践

配置地址 【R1】 [r1-Serial4/0/0]ip address 15.0.0.2 24 [r1-GigabitEthernet0/0/0]ip address 192.168.1.1 24 【R2】 [r2-Serial4/0/0]ip address 25.0.0.2 24 [r2-GigabitEthernet0/0/0]ip address 192.168.2.1 24 【R3】 [r3-Serial4/0/0]ip address 35.0.0.2 24 [r3-Gig…

hcip第五天实践

配置缺省路由&#xff1a; [R1]ip route-static 0.0.0.0 0 16.0.0.2 [R1]ip route-static 0.0.0.0 0 116.0.0.2 [R2]ip route-static 0.0.0.0 0 26.0.0.2 [R3]ip route-static 0.0.0.0 0 36.0.0.2 [R4]ip route-static 0.0.0.0 0 46.0.0.2 [R5]ip route-static 0.0.0.0 0 56.0…

JTAG 案例

摘要&#xff1a; 以前看datasheet&#xff0c;到了JTAG一章&#xff0c;一直奇怪为啥需要这么多篇幅来进行介绍。这段时间碰到两个JTAG问题&#xff0c;才把章节浏览了一遍。才理解了 TAP其实由TMS,TCK控制&#xff0c;有时候能扫描器件&#xff0c;但是不能下载是因为TMS,TCK…

hcip第七天笔记

类型LS ID通告者作用范围携带信息 Type-1LSA Router 通告者的RID区域内所有运行单区域本地接口的直连拓扑 Typr-2LSA Network DR接口的地址单个MA网络中DR所在路由器的RID单区域单个MA网络拓扑信息的补充信息 Typr-3LSA Sum-Net(summary) 路由信息的目标网络号ABR&#xff0c;在…

CISICO 路由器和交换机的基本配置命令

Configure terminal 功能&#xff1a;进入全局配置模式 条件&#xff1a;特权模式 格式&#xff1a;configure terminal Hostname 功能&#xff1a;设置主机名 条件&#xff1a;全局配置模式 格式&#xff1a;hostname hostname Interface vlan vlan-id Ip address address …

第七次实验hcip

第一步:地址划分 172.16.0.0/16 172.16.0.0/19(area0) 172.16.0.0/24(P2P骨干) 172.16.0.0/30 172.16.0.4/30 172.16.0.8/30 172.16.1.0/24(MA骨干) 172.16.1.0/29 172.16.1.8/29 …

第8天hcip

OSPF的防环 1&#xff0c;域间防环 1&#xff0c;OSPF因为区域之间也传递的是路由信息&#xff0c;所有&#xff0c;存在区域水平分割机制。路由信息是从哪个区域学习到的则将不再回传到这个区域。 2&#xff0c;星型拓扑的区域划分要求本身也是一种防环机制。 2&#xff0c;域…

C和C++实务精选丛书

C和C实务精选丛书[0].C和指针 C和C实务精选丛书[1].C陷阱与缺陷 C和C实务精选丛书[2].C专家编程 C和C实务精选丛书[3].C和C代码精粹 C和C实务精选丛书[4].C 和面向对象的数值计算 C和C实务精选丛书[5].CTemplates C和C实务精选丛书[6].C沉思录 C和C实务精选丛书[7].C代码设计与…

hcip第9天

华为设备为了消除路由回馈现象&#xff0c;他将OSPF内部产生的路由的优先级定义为10&#xff0c;而将域外导入的路由的默认优先级为150.&#xff08;150的优先级大于所有华为体系下IGP协议的默认优先级&#xff09;。通过这种方式来消除路由回馈。 由于重发布技术的种子度量值问…

iphone图片等比缩放

iphone图片等比缩放 UIImage *img imgView.p_w_picpath; int h img.size.height; int w img.size.width; if(h < 320 && w < 480) { imgView.p_w_picpath img; } else { float b (float)320/w < (float)480/h ? (float)320/w : (float)480/h; CGSize i…

第10天笔记hcip

BGP --边界网关协议 AS -自治系统---由单- -机构或组织管理的一-些列IP网络及其设备的集合。 1&#xff0c;网络范围太大&#xff0c;协议跑不过来&#xff0c;需要进行划分; 2&#xff0c;自治管理. 为了方便区分和标定不同的AS,我们给每个自治系统设计了- -个编号--- AS号…

在装有raid卡的服务器上安装Ubuntu

1、开机时进入Raid Bios&#xff0c;选择创建Raid&#xff0c;这个大家随意&#xff0c;按需自选。 2、然后放入ubuntu系统盘&#xff0c;步骤与一般安装无异&#xff0c;直到识别硬盘时会发现无法识别硬盘 3、用跟你需要装的系统相同内核的系统&#xff0c;重新编译下rockraid…

hcip第11天笔记

6&#xff0c;BGP的基本配置 1&#xff0c;BGP建邻的基本关系 1&#xff0c;EBGP对等体关系直接建邻 [r1]bgp 1----启动BGP进程---后面的1不是进程号&#xff0c;而是配置路由器所在AS的AS号 [r1-bgp] [r1-bgp]router-id 1.1.1.1---配置RID [r1-bgp]peer 12.0.0.2 as-number-…