R 简明教程
R - Decision Tree
决策树是一款图形,用于以树的形式表示选择及其结果。图形中的节点表示事件或选择,图形中的边表示决策规则或条件。它在使用 R 的机器学习和数据挖掘应用程序中应用最为广泛。
Decision tree is a graph to represent choices and their results in form of a tree. The nodes in the graph represent an event or choice and the edges of the graph represent the decision rules or conditions. It is mostly used in Machine Learning and Data Mining applications using R.
决策树的使用示例:预测电子邮件是否是垃圾邮件,预测肿瘤是否为癌症或根据每个因素预测贷款是良好的信誉风险还是坏的信誉风险。通常,模型使用观察数据(培训数据)创建。然后,使用一组验证数据来验证和改善模型。R 有多个用于创建和可视化决策树的包。对于新的一组预测变量,我们使用此模型对数据的类别(是/否、垃圾邮件/不是垃圾邮件)做出决策。
Examples of use of decision tress is − predicting an email as spam or not spam, predicting of a tumor is cancerous or predicting a loan as a good or bad credit risk based on the factors in each of these. Generally, a model is created with observed data also called training data. Then a set of validation data is used to verify and improve the model. R has packages which are used to create and visualize decision trees. For new set of predictor variable, we use this model to arrive at a decision on the category (yes/No, spam/not spam) of the data.
R 包 "party" 用于创建决策树。
The R package "party" is used to create decision trees.
Install R Package
在 R 控制台中使用以下命令安装包。还需要安装相关包(如果存在)。
Use the below command in R console to install the package. You also have to install the dependent packages if any.
install.packages("party")
“party”包具有用于创建和分析决策树的 ctree() 函数。
The package "party" has the function ctree() which is used to create and analyze decison tree.
Syntax
在 R 中创建决策树的基本语法为:
The basic syntax for creating a decision tree in R is −
ctree(formula, data)
以下是所用参数的描述 -
Following is the description of the parameters used −
-
formula is a formula describing the predictor and response variables.
-
data is the name of the data set used.
Input Data
我们将使用名为 readingSkills 的 R 内置数据集创建决策树。如果我们知道变量“age”、“shoesize”、“score”,以及该人是否为母语人士,则该数据集将说明某人的 readingSkills 得分。
We will use the R in-built data set named readingSkills to create a decision tree. It describes the score of someone’s readingSkills if we know the variables "age","shoesize","score" and whether the person is a native speaker or not.
以下是示例数据。
Here is the sample data.
# Load the party package. It will automatically load other
# dependent packages.
library(party)
# Print some records from data set readingSkills.
print(head(readingSkills))
当我们执行上述代码时,它会产生以下结果和图表:
When we execute the above code, it produces the following result and chart −
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
...............................
...............................
Example
我们将使用 ctree() 函数创建决策树并查看其图形。
We will use the ctree() function to create the decision tree and see its graph.
# 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()
当我们执行上述代码时,会产生以下结果 -
When we execute the above code, it produces the following result −
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
