美文网首页
R语言 决策树

R语言 决策树

作者: yuanyb | 来源:发表于2017-11-14 08:22 被阅读0次

决策树是以树的形式表示选择及其结果的图。图中的节点表示事件或选择,并且图的边缘表示决策规则或条件。它主要用于使用R的机器学习和数据挖掘应用程序。

决策树的使用的例子是 - 预测电子邮件是垃圾邮件或非垃圾邮件,预测肿瘤癌变,或者基于这些因素预测贷款的信用风险。通常,使用观测数据(也称为训练数据)来创建模型。然后使用一组验证数据来验证和改进模型。 R具有用于创建和可视化决策树的包。对于新的预测变量集合,我们使用此模型来确定R包“party”用于创建决策树。

安装R语言包

在R语言控制台中使用以下命令安装软件包。您还必须安装相关软件包(如果有)。

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">install.packages("party")
</pre>

“party”包具有用于创建和分析决策树的函数ctree()

语法

在R中创建决策树的基本语法是 -

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ctree(formula, data)
</pre>

以下是所使用的参数的描述 -

  • formula是描述预测变量和响应变量的公式。

  • data是所使用的数据集的名称。

输入数据

我们将使用名为readingSkills的R内置数据集来创建决策树。 它描述了某人的readingSkills的分数,如果我们知道变量“年龄”,“shoesize”,“分数”,以及该人是否为母语者。

这里是示例数据。

<pre class="prettyprint notranslate tryit" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># Load the party package. It will automatically load other dependent packages.
library(party)

Print some records from data set readingSkills.

print(head(readingSkills))
</pre>

当我们执行上面的代码,它产生以下结果及图表 -

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> nativeSpeaker age shoeSize score
1 yes 5 24.83189 32.29385
2 yes 6 25.95238 36.63105
3 no 11 30.42170 49.60593
4 yes 7 28.66450 40.28456
5 yes 11 31.88207 55.46085
6 yes 10 30.07843 52.83124
Loading required package: methods
Loading required package: grid
...............................
...............................
</pre>

我们将使用ctree()函数创建决策树并查看其图形。

<pre class="prettyprint notranslate tryit" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># Load the party package. It will automatically load other dependent packages.
library(party)

Create the input data frame.

input.dat <- readingSkills[c(1:105),]

Give the chart file a name.

png(file = "decision_tree.png")

Create the tree.

output.tree <- ctree(
nativeSpeaker ~ age + shoeSize + score,
data = input.dat)

Plot the tree.

plot(output.tree)

Save the file.

dev.off()
</pre>

当我们执行上面的代码,它产生以下结果 -

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">null device
1
Loading required package: methods
Loading required package: grid
Loading required package: mvtnorm
Loading required package: modeltools
Loading required package: stats4
Loading required package: strucchange
Loading required package: zoo

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

as.Date, as.Date.numeric

Loading required package: sandwich
</pre>

决策树,使用R

结论

从上面显示的决策树,我们可以得出结论,其readingSkills分数低于38.3和年龄超过6的人不是一个母语者。

相关文章

  • 决策树(R语言)

    原文链接:决策树(R语言) 微信公众号:机器学习养成记 搜索添加微信公众号:chenchenwings 决策树是...

  • R语言 决策树

    决策树是以树的形式表示选择及其结果的图。图中的节点表示事件或选择,并且图的边缘表示决策规则或条件。它主要用于使用R...

  • R语言-17决策树

    应用及介绍 是一个预测模型,分为回归决策树和分类决策树,根据已知样本训练出一个树模型,从而根据该模型对新样本因变量...

  • R语言决策树实现

    @[toc] 决策树 适用于分类型独立变量 决策树函数 ID3:information entropy 信息熵 C...

  • 决策树及R语言实现

    决策树是什么: 决策树是基于树结构来进行决策,这恰是人类在面临决策问题时一种很自然的处理机制。例如,我们要对“这是...

  • 机器学习专题:代码实现(R)

    一、入门(转载) 1、线性回归 Python代码 R代码 2、逻辑回归 Python代码 R代码 3、决策树 Py...

  • 决策树-R

    1、特征缩放 特征缩放是为了图形可视化的方便,决策树没有用到欧式距离不需要特征缩放 2、代码 关键代码:libra...

  • 学习小组Day4笔记-皇晓燕

    R语言和R studio R语言是全面的统计分析平台,计算作图等等 R studio是R语言的操作平台 下载R语言...

  • Day4 学习小组--张小张

    今天是 R 语言基础的学习 了解R与Rstudio R 语言是一款统计软件; R 语言也是一门编程语言,语言也是一...

  • 学习小组Day4笔记--扬马延

    R语言学习 1. R以及R studio安装 直接搜索R语言网页可直接安装 2. R语言入门 参考书目《R for...

网友评论

      本文标题:R语言 决策树

      本文链接:https://www.haomeiwen.com/subject/wrhxvxtx.html