有没有办法使用rpart.plot
用于绘制不属于的对象的库rpart
(用于制作决策树)?
例如,这是经典的rpart
and rpart.plot
正在运行的库:
#load libraries
library(rpart)
library(rpart.plot)
#load data
data(iris)
#fit rpart model (i.e. decision tree)
r = rpart(Species ~., data=iris)
#plot model
rpart.plot(r)
![enter image description here](https://i.stack.imgur.com/S8Y6J.png)
Problem:我正在研究一个多类分类问题(如上面的示例 - 我只是用著名的“iris 数据集”说明了这一点),其中“rpart”运行时间太长(我等了 10 个小时,“rpart”代码仍然存在没有跑)。
然而,我在 R 中发现了另一个名为“c50”的库,它能够立即创建类似的模型:
#load library
library(C50)
#run same model
tree_mod <- C5.0(x = iris[, -5], y = iris$Species, rules = TRUE)
#view model
summary(tree_mod)
plot(tree_mod)
![enter image description here](https://i.stack.imgur.com/WTJx4.png)
Question:是否可以将“rpart.plot”库与“C50”库中的对象一起使用?
例如:
#my attempt
rpart.plot(tree_mod)
Error in rpart.plot(tree_mod) : Not an rpart object
My idea:可以使用“C50”库提取不同的规则:
summary(tree_mod)
Rule 1: (50, lift 2.9)
Petal.Length <= 1.9
-> class setosa [0.981]
Rule 2: (48/1, lift 2.9)
Petal.Length > 1.9
Petal.Length <= 4.9
Petal.Width <= 1.7
-> class versicolor [0.960]
Rule 3: (46/1, lift 2.9)
Petal.Width > 1.7
-> class virginica [0.958]
Rule 4: (46/2, lift 2.8)
Petal.Length > 4.9
-> class virginica [0.938]
类似的规则也可以从“rpart”库中提取:
rpart.rules(r)
Species seto vers virg
setosa [1.00 .00 .00] when Petal.Length < 2.5
versicolor [ .00 .91 .09] when Petal.Length >= 2.5 & Petal.Width < 1.8
virginica [ .00 .02 .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8
是否有可能以某种方式“重新格式化规则”C50
库以使其兼容的方式rpart.plot
?