在我的研究中试图解决这个问题时,我觉得我已经非常接近答案了。我正在寻找类似于这个的内容,适用于C5.0包。
Stack Overflow上的答案中提供的方法适用于party
对象。然而,C5.0包不支持as.party
。在进一步的研究中,我发现了这个评论,指出C5.0包的维护者已经编写了该函数,但没有导出它。
我以为这应该可以工作,但遗憾的是,建议的函数C50:::as.party.C5.0(mod1)
抛出了错误:
error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ""function"" to a data.frame
任何解决此错误的建议都将不胜感激。让我们使用以下示例:
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()
转换就能成功,你可以应用所有相关方法:
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
然后是您链接的其他讨论中的预测路径选择:
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
的维护者。