R C5.0获得每片叶子的规则和概率 [英] R C5.0 get rule and probability for every leaf
问题描述
我认为在研究解决这个问题的过程中,我非常接近.我正在为C5.0软件包寻找 this 之类的东西.
I think during my research to solve this question I came pretty close. I am looking for something like this for the C5.0 package.
SO答案中提供的方法适用于party
对象.但是,C5.0软件包不支持as.party
.在进一步研究中,我发现
The method provided in the SO answer works with a party
object. However the C5.0 package does not support as.party
. On my further research I found this comment that the maintainer of the C5.0 package already programmed the function, but did not export it.
我认为这应该很好,但是不幸的是建议的功能C50:::as.party.C5.0(mod1)
会引发错误:
I thought great this should work, but unfortunately the suggested function C50:::as.party.C5.0(mod1)
throws the error:
error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ""function"" to a data.frame
任何解决此错误的建议都值得赞赏. 让我们使用以下示例:
Any suggestions to solve this error appreciated. Let's use the following example:
library(C50)
p = iris[1:4]
t = factor(iris$Species)
model = C50::C5.0(p,t)
#summary(model)
modParty = C50:::as.party.C5.0(model)
推荐答案
使用默认方法C5.0()
而不是公式方法时,似乎会出现问题.如果使用后者,则as.party()
转换将成功进行,并且您可以为此应用所有方法:
The problem seems to occur when using the default method of C5.0()
as opposed to the formula method. If you use the latter then the as.party()
conversion works successfully and you can apply all methods for that:
model <- C5.0(Species ~ ., data = iris)
modParty <- C50:::as.party.C5.0(model)
modParty
## Model formula:
## Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
##
## Fitted party:
## [1] root
## | [2] Petal.Length <= 1.9: setosa (n = 50, err = 0.0%)
## | [3] Petal.Length > 1.9
## | | [4] Petal.Width <= 1.7
## | | | [5] Petal.Length <= 4.9: versicolor (n = 48, err = 2.1%)
## | | | [6] Petal.Length > 4.9: virginica (n = 6, err = 33.3%)
## | | [7] Petal.Width > 1.7: virginica (n = 46, err = 2.2%)
##
## Number of inner nodes: 3
## Number of terminal nodes: 4
然后选择预测路径,就像在您链接的其他讨论中一样:
And then a selection of predicted paths as in the other discussion you linked:
pathpred(modParty)[c(1, 51, 101), ]
## response prob.setosa prob.versicolor prob.virginica
## 1 setosa 1.00000000 0.00000000 0.00000000
## 51 versicolor 0.00000000 0.97916667 0.02083333
## 101 virginica 0.00000000 0.02173913 0.97826087
## rule
## 1 Petal.Length <= 1.9
## 51 Petal.Length > 1.9 & Petal.Width <= 1.7 & Petal.Length <= 4.9
## 101 Petal.Length > 1.9 & Petal.Width > 1.7
我不确定为什么该方法不适用于默认界面.但是设置所需的模型框架可能会更加困难.不过,您可以考虑向C50
维护者咨询.
I'm not sure why the method does not work for the default interface. But probably it's more difficult to set up the required model frame. You might consider asking the C50
maintainer about this, though.
这篇关于R C5.0获得每片叶子的规则和概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!