与插入符号包“rpart”方法有关的问题。
method='rpart' 会自动修剪树吗?如果是这样,它遵循什么规则?如果没有,如何指导插入符号执行此操作?
与插入符号包“rpart”方法有关的问题。
method='rpart' 会自动修剪树吗?如果是这样,它遵循什么规则?如果没有,如何指导插入符号执行此操作?
为rpartpackage 和 package rpart方法提供适当的背景caret:
1 、如果rpart直接使用包,默认会构建完整的树。如果要修剪树,则需要提供rpart.control控制树适合度的可选参数。下面的 R 文档,例如:
rpart(formula, data, method, control = prune.control)
prune.control = rpart.control(minsplit = 20,
minbucket = round(minsplit/3), cp = 0.01,
maxcompete = 4, maxsurrogate = 5, usesurrogate = 2,
xval = 10, surrogatestyle = 0, maxdepth = 30 )
这些是您可以调整以获得修剪树的超参数。
一种遵循的方法是不提供cp即复杂性参数并执行交叉验证(xval),例如:
rpart.control(minsplit = 20, minbucket = round(minsplit/3), xval = 10)
复杂性参数(cp)可以被认为是衡量模型复杂性/拆分次数的量度,并且您希望增加复杂性,直到您的模型推广到新的观察结果。即正则化
因此评估交叉验证的误差与 cp并选择给出良好值的 cp (cp_good) 。
最后,将其添加为您的控制参数,即rpart.control(cp = cp_good)或使用修剪功能,即prune(fit, cp = cp_good)来获得所需的树。
2. caret另一方面,包已经实现了作为调整参数的rpart方法。默认情况下,将根据它在默认参数网格上进行的默认运行修剪您的树(即使您在训练模型时不提供任何内容:cpcarettuneGridtrControl
model <- train(data,
labels,
method = "rpart")
当我运行以下 R 代码时
ctrl <- trainControl(method = "repeatedcv",
number = 10,
repeats = 3)
model <- train(trainingData,
labels,
method = "rpart",
trControl = ctrl)
model
我得到低于输出
CART
760 samples
6 predictor
2 classes: 'Moving', 'Stationary'
No pre-processing
Resampling: Cross-Validated (10 fold, repeated 3 times)
Summary of sample sizes: 684, 684, 685, 684, 685, 684, ...
Resampling results across tuning parameters:
cp Accuracy Kappa
0.02310231 0.8640390 0.7224351
0.10231023 0.8460257 0.6925546
0.59075908 0.7273725 0.3787905
Accuracy was used to select the optimal model using the largest value.
The final value used for the model was cp = 0.02310231.
cp是用于rpart确定何时修剪的参数。因此,在使用caret. 因此,问题的答案是肯定的。