[英]How to plot Rule based Decision tree in R
I am just starting with R and I am having a difficulty in plotting a rule based decision tree in R. 我只是从R开始,而在R中绘制基于规则的决策树却遇到了困难。
The problem is, I already have an output from hadoop MapReduce and it is in the form of a simple text file. 问题是,我已经有了hadoop MapReduce的输出,它的形式是简单的文本文件。 Now I want to use this output from Hadoop and represent it graphically on R. The output file looks something like this. 现在,我想使用Hadoop的输出,并在R上以图形方式表示。输出文件看起来像这样。
1 overcast yes
1 rain 3 strong no
1 rain 3 weak yes
1 sunny 2 high no
1 sunny 2 normal yes
Is there a way that I can represent this graphically in R in something like, 有没有一种方法可以像这样在R中以图形方式表示它,
http://web.cs.swarthmore.edu/~meeden/cs63/f05/figure3.1.jpg http://web.cs.swarthmore.edu/~meeden/cs63/f05/figure3.1.jpg
Any help will be much appreciated. 任何帮助都感激不尽。 Thanks 谢谢
Take a look at package 'rpart'
. 看一下包'rpart'
。 It's a package for recursive partitioning and decision trees. 它是用于递归分区和决策树的软件包。 The following is obtained directly from the example from the help file ?rpart
. 从帮助文件?rpart
的示例中直接获得以下内容。 The function expand.grid
may be of some use to you here as well. 函数expand.grid
在这里也可能对您有用。
> example(rpart)
fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
fit2 <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis,
parms = list(prior = c(.65,.35), split = "information"))
fit3 <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis,
control = rpart.control(cp = 0.05))
par(mfrow = c(1,2), xpd = NA) # otherwise on some devices the text is clipped
plot(fit)
text(fit, use.n = TRUE)
plot(fit2)
text(fit2, use.n = TRUE)
First you have to convert the text file into a data frame. 首先,您必须将文本文件转换为数据框。 This might be a good start: Converting (web site) text file into data frame in R 这可能是一个不错的开始: 在R中将(网站)文本文件转换为数据框
Then you could use 'rpart'
to build a tree. 然后,您可以使用'rpart'
来构建树。 In addition to ' rpart'
and it's prp()
-function, you could also use the 'fancyRpartPlot'
in the "rattle" -package to build fancier trees. 除了“ rpart'
及其prp()
函数外,您还可以在“ rattle” 'fancyRpartPlot'
使用'fancyRpartPlot'
来构建更高级的树。 Here is a good example. 这是一个很好的例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.