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

利用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

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

后面未完待续

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

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

  1. ARM系列CPU对比介绍(未完待续)

    ARM(Advanced RISC Machine,更早称作:Acorn RISC Machine)即进阶精简指令集机器. "ARM公司拥有众多CPU指令集, CPU架构, CPU系列.以下 ...

  2. 深度学习(四十)优化求解系列(2)简单理解神经网络求解过程-未完待续

    对于神经网络的求解过程BP,其实说白了就是复合函数的求导过程,所以我们需要先复习一下高数复合函数的求导过程. 一.复合函数的求导法则 1.复合函数: 此函数是一个包含了三层映射过程的复合函数,为了跟后 ...

  3. 最新版校园招聘进大厂系列----------(1)阿里篇 -----未完待续

  4. 33 ArcToolBox学习系列之数据管理工具箱——投影与变换(Projections and Transformations)未完待续……...

    工具箱位置 打开ArcToolBox,找到工具集Projections and Transformations,位置如下:ArcToolbox--Data Management Tools--Proj ...

  5. 补充游戏思考13:游戏服务器杂谈(主要讲mmorpg,年更系列,未完待续10/20)

    文章目录 一.冒险岛状态同步数据的举例说明 1)基于帧的人物状态同步 2)基于预言的人物状态同步(插值计算) 3)基于客户端的怪物状态同步 4)基于服务器的怪物状态同步 一.冒险岛状态同步数据的举例说 ...

  6. 【白话系列】未完待续

    图论当中: 1.Dijistra+Heap 求最短路 2.Floyd 求最短路 3.SPFA 求最短路 4.Prim 求最小生成树 5.Kruskal 求最小生成树 6.Tarjan 求强连通分量 7 ...

  7. R语言构建决策树(decision trees)模型并进行调优和解释

    R语言构建决策树(decision trees)模型并进行调优和解释 目录 R语言构建决策树(decision trees)

  8. OpenCV使用不同的决策树decision trees的实例(附完整代码)

    OpenCV使用不同的决策树decision trees的实例 OpenCV使用不同的决策树decision trees的实例 OpenCV使用不同的决策树decision trees的实例 #inc ...

  9. 决策树(Decision Trees)

    决策树(Decision Trees) 1. Training and Visualizing a Decision Tree can perform both classification and ...

最新文章

  1. AFNetworking 源代码分析
  2. linux top命令查看内存及多核CPU的使用讲述【转】
  3. 开源软件架构总结之——Bash(readline做输入交互式,词法语法分析,进程交互)...
  4. 启用第三方Chrome插件
  5. ThinkPHP的易忽视点小结
  6. java derby连接_JAVA-Derby连接
  7. 画活动图教程_二次元人物头发怎么画?画好头发有什么技巧?
  8. MySQL5.6主从复制搭建基于日志(binlog)
  9. MySQL系列:数据库基本操作(1)
  10. 设计模式(二 三)工厂模式:1-简单工厂模式
  11. 95-134-105-源码-维表-维表优化
  12. Java 多线程 4:wait() 和 notify()/notifyAll()
  13. 正则表达式 贪婪与懒惰
  14. win7 oracle 冷恢复
  15. 多线程编程之二——MFC中的多线程开发(收藏)
  16. 小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
  17. win7常见问题汇总
  18. C语言基础练习100--008(输出国际象棋棋盘)
  19. C64x+ 与 C64x Cache 区别
  20. C++三种方法求解两个数最大公因数和最小公倍数

热门文章

  1. php文本文件操作,文本文件操作的php类
  2. mysql warning 日志_Mysql5.7.19安装后错误日志中有警告
  3. python判断能否组成三角形_python三角形判定怎么做
  4. 日文转换为罗马音_手把手教你掌握韩语40音!入门必备哦
  5. arm64 linux 除零正常返回,arm64程序调用规则
  6. vscode设置templates_在VScode中创建你的代码模板的方法
  7. 提交文件至服务器的设置——表单属性中的 enctype
  8. 解决VS2013或2017中类似于:error C4996: 'scanf': This function or variable may be unsafe的问题
  9. IntelliJ IDEA关于logger的live template配置
  10. java pdf增删改查_如何利用Java代码操作索引库?