在插入符号中绘制 ctree 方法决策树,删除下面不需要的条形图
Plotting a ctree method decision tree in caret, remove unwanted bargraph underneath
我是 运行 插入符号中的 ctree 方法模型,并试图绘制我得到的决策树。
这是我的代码的主要部分。
fitControl <- trainControl(method = "cv", number = 10)
dtree <- train(
Outcome ~ ., data = training_set,
method = "ctree", trControl = fitControl
)
我正在尝试绘制决策树并使用
plot(dtree$finalModel)
这给了我这个 -
这里的图片不太好,但我在这个问题的答案中得到了类似于第一个情节的图片 -
并且函数 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")。有必要使用非默认的高度和宽度值来避免终端节点重叠。
请注意,在使用带有插入符的 ctree 时,应注意 0、1 个结果变量。
带有 ctree 方法的 caret 包默认使用整数或数字 0、1 数据构建回归模型。如果需要分类,将结果变量转换为因子。
我是 运行 插入符号中的 ctree 方法模型,并试图绘制我得到的决策树。 这是我的代码的主要部分。
fitControl <- trainControl(method = "cv", number = 10)
dtree <- train(
Outcome ~ ., data = training_set,
method = "ctree", trControl = fitControl
)
我正在尝试绘制决策树并使用
plot(dtree$finalModel)
这给了我这个 -
这里的图片不太好,但我在这个问题的答案中得到了类似于第一个情节的图片 -
并且函数 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")。有必要使用非默认的高度和宽度值来避免终端节点重叠。
请注意,在使用带有插入符的 ctree 时,应注意 0、1 个结果变量。 带有 ctree 方法的 caret 包默认使用整数或数字 0、1 数据构建回归模型。如果需要分类,将结果变量转换为因子。