[英]Plotting a rpart decision tree modell with the sunburst view
I found a way to plot a decision tree solution from rpart with the sunburstR-package.我找到了一种使用 sunburstR 包从 rpart 绘制决策树解决方案的方法。 To plotting a sunburst it is necessary to have data.frame which represents a sequence.要绘制森伯斯特,必须有表示序列的 data.frame。 I modified the decision tree result to a sequence like below我将决策树结果修改为如下序列
Result of the decision tree决策树的结果
rpart(Species~.,data=iris)
n= 150
node), split, n, loss, yval, (yprob)
* denotes terminal node
1) root 150 100 setosa (0.33333333 0.33333333 0.33333333)
2) Petal.Length< 2.45 50 0 setosa (1.00000000 0.00000000 0.00000000) *
3) Petal.Length>=2.45 100 50 versicolor (0.00000000 0.50000000 0.50000000)
6) Petal.Width< 1.75 54 5 versicolor (0.00000000 0.90740741 0.09259259) *
7) Petal.Width>=1.75 46 1 virginica (0.00000000 0.02173913 0.97826087) *
Sequence for the sunburst:日出序列:
sequences_1<-1
sequences_1<-data.frame(sequences_1)
sequences_1[1:3,]<-1
sequences_1$V1[1]<-"Petal.Length<_2.45-setosa"
sequences_1$V1[2]<-"Petal.Length>=2.45-Petal.Width<_1.75_54_5-versicolor"
sequences_1$V1[3]<-"Petal.Length>=2.45-Petal.Width>=1.75_46_1-virginica"
sequences_1$V2[1]<-50
sequences_1$V2[2]<-54
sequences_1$V2[3]<-46
sequences_1$sequences_1<-NULL
Plotting Sunburst:绘制森伯斯特:
library(sunburstR)
sunburst(sequences_1,count=TRUE)
The sequence for the sunburst plot, I did manually.旭日图的序列,我手动完成的。 Do someone know how to build the sequence automatically like above from the result of the rpart decision tree?有人知道如何根据 rpart 决策树的结果像上面一样自动构建序列吗?
d3r provides a function d3_party
to convert rpart/partykit
into a d3 hierarchy. D3R提供了一个功能d3_party
转换rpart/partykit
到D3层次。 sunburst
can use the result of d3_party
with one minor modification to change "rule"
to "name"
. sunburst
可以使用的结果d3_party
有一个小的修改,以改变"rule"
到"name"
。 This is not ideal but in most cases will work flawlessly.这并不理想,但在大多数情况下可以完美运行。
library(rpart)
library(d3r)
# d3_party requires partykit
# install.packages("partykit")
library(sunburstR)
rp <- rpart(Species~.,data=iris)
rp_d3 <- d3_party(rp)
# one trick/hack required since sunburst expects
# name but d3_party gives rule
# this is ugly but let's replace all "rule" with "name"
# with gsub
rp_d3 <- gsub(
x = rp_d3,
pattern = '"rule":',
replacement = '"name":'
)
sunburst(
rp_d3,
valueField = "n",
sumNodes = FALSE,
count = TRUE,
legend = FALSE
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.