gpt4 book ai didi

r - 交叉验证 CART 模型

转载 作者:行者123 更新时间:2023-12-01 02:10:08 27 4
gpt4 key购买 nike

在作业中,我们被要求对 CART 模型执行交叉验证。我试过使用 cvFit函数来自 cvTools但收到一条奇怪的错误消息。这是一个最小的例子:

library(rpart)
library(cvTools)
data(iris)
cvFit(rpart(formula=Species~., data=iris))

我看到的错误是:
Error in nobs(y) : argument "y" is missing, with no default

traceback() :
5: nobs(y)
4: cvFit.call(call, data = data, x = x, y = y, cost = cost, K = K,
R = R, foldType = foldType, folds = folds, names = names,
predictArgs = predictArgs, costArgs = costArgs, envir = envir,
seed = seed)
3: cvFit(call, data = data, x = x, y = y, cost = cost, K = K, R = R,
foldType = foldType, folds = folds, names = names, predictArgs = predictArgs,
costArgs = costArgs, envir = envir, seed = seed)
2: cvFit.default(rpart(formula = Species ~ ., data = iris))
1: cvFit(rpart(formula = Species ~ ., data = iris))

看起来 y对于 cvFit.default 来说是强制性的.但:
> cvFit(rpart(formula=Species~., data=iris), y=iris$Species)
Error in cvFit.call(call, data = data, x = x, y = y, cost = cost, K = K, :
'x' must have 0 observations

我究竟做错了什么?哪个包可以让我对 CART 树进行交叉验证而不必自己编码? (我太懒了……)

最佳答案

caret 包使交叉验证变得轻而易举:

> library(caret)
> data(iris)
> tc <- trainControl("cv",10)
> rpart.grid <- expand.grid(.cp=0.2)
>
> (train.rpart <- train(Species ~., data=iris, method="rpart",trControl=tc,tuneGrid=rpart.grid))
150 samples
4 predictors
3 classes: 'setosa', 'versicolor', 'virginica'

No pre-processing
Resampling: Cross-Validation (10 fold)

Summary of sample sizes: 135, 135, 135, 135, 135, 135, ...

Resampling results

Accuracy Kappa Accuracy SD Kappa SD
0.94 0.91 0.0798 0.12

Tuning parameter 'cp' was held constant at a value of 0.2

关于r - 交叉验证 CART 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16717970/

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