我找到了一种使用sunburstR-package从rpart绘制决策树解决方案的方法。要绘制朝阳图,必须具有代表序列的data.frame。我将决策树结果修改为如下所示的顺序
决策树的结果
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) *
朝阳的顺序:
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
绘制森伯斯特:
library(sunburstR)
sunburst(sequences_1,count=TRUE)
我手动完成了朝阳图的序列。有人知道如何从rpart决策树的结果像上面那样自动构建序列吗?
答案 0 :(得分:1)
d3r提供了功能d3_party
,用于将rpart/partykit
转换为d3层次结构。 sunburst
可以使用d3_party
的结果进行少量修改,将"rule"
更改为"name"
。这不是理想的方法,但是在大多数情况下可以正常工作。
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
)