gpt4 book ai didi

r - 在插入符号中绘制 ctree 方法决策树,删除下面不需要的条形图

转载 作者:行者123 更新时间:2023-12-04 10:50:24 25 4
gpt4 key购买 nike

我在插入符号中运行 ctree 方法模型并尝试绘制我得到的决策树。
这是我的代码的主要部分。

fitControl <- trainControl(method = "cv", number = 10)
dtree <- train(
Outcome ~ ., data = training_set,
method = "ctree", trControl = fitControl
)

我正在尝试绘制决策树并使用
plot(dtree$finalModel)

这给了我这个 -

decision tree

这里的图片不好,但我得到的图像类似于这个问题的答案中的第一个图 - Plot ctree using rpart.plot functionality

并且函数 as.simpleparty 不起作用,因为它不是 rpart 对象。

我想删除下面的条形图,并在这些节点上简单地得到一个 1 或一个 0,告诉我它是如何分类的。
由于 dtree$finalModel 是一个二叉树对象,
prp(dtree$finalModel)

不起作用。

最佳答案

有可能在底部没有图形但有结果标签而不使用插入符号的情况下获得 ctree 图。不过,为了完整起见,我已经包含了下面的插入符号代码。

首先为可重现的示例设置一些数据:

library(caret)    
library(partykit)
data("PimaIndiansDiabetes", package = "mlbench")
head(PimaIndiansDiabetes)
pregnant glucose pressure triceps insulin mass pedigree age diabetes
1 6 148 72 35 0 33.6 0.627 50 pos
2 1 85 66 29 0 26.6 0.351 31 neg
3 8 183 64 0 0 23.3 0.672 32 pos
4 1 89 66 23 94 28.1 0.167 21 neg
5 0 137 40 35 168 43.1 2.288 33 pos
6 5 116 74 0 0 25.6 0.201 30 neg

现在使用插入符号找到最佳 ctree 参数:
fitControl <- trainControl(method = "cv", number = 10)
dtree <- train(
diabetes ~ ., data = PimaIndiansDiabetes,
method = "ctree", trControl = fitControl
)

dtree
Conditional Inference Tree

768 samples
8 predictor
2 classes: 'neg', 'pos'

No pre-processing
Resampling: Cross-Validated (10 fold)
Summary of sample sizes: 691, 691, 691, 692, 691, 691, ...
Resampling results across tuning parameters:

mincriterion Accuracy Kappa
0.01 0.7239747 0.3783882
0.50 0.7447027 0.4230003
0.99 0.7525632 0.4198104

Accuracy was used to select the optimal model using the largest value.
The final value used for the model was mincriterion = 0.99.

这不是一个理想的模型,但是嘿嘿,我们继续。

现在使用带有来自 caret 的最佳参数的 ctree 包构建和绘制 ctree 模型:
ct <- ctree(diabetes ~ ., data = PimaIndiansDiabetes, mincriterion = 0.99)

png("diabetes.ctree.01.png", res=300, height=8, width=14, units="in")
plot(as.simpleparty(ct))
dev.off()

这给出了下图,底部没有图表,但在终端节点上有结果变量(“pos”和“neg”)。有必要使用非默认的高度和宽度值来避免重叠终端节点。

Diabetes ctree diagram

请注意,使用带有插入符号的 ctree 时,应注意 0, 1 结果变量。
带有 ctree 方法的 caret 包默认使用整数或数字 0, 1 数据构建回归模型。如果需要分类,则将结果变量转换为因子。

关于r - 在插入符号中绘制 ctree 方法决策树,删除下面不需要的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53330709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com