如何在R中实施保留验证

如何在R中实施保留验证

本文介绍了如何在R中实施保留验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在使用Sonar数据,并且我想在R中进行保留验证.我使用来自caret包的createFolds作为folds <- createFolds(mydata$Class, k=5)对数据进行了分区.

Let's say I'm using the Sonar data and I'd like to make a hold-out validation in R. I partitioned the data using the createFolds from caret package as folds <- createFolds(mydata$Class, k=5).

然后我想准确地使用折叠mydata[i]作为测试数据,并使用mydata[-i]作为训练数据来训练一个分类器.

I would like then to use exactly the fold mydata[i] as test data and train a classifier using mydata[-i] as train data.

我的第一个想法是使用train函数,但是找不到对保持验证的任何支持.我在这里想念什么吗?

My first thought was to use the train function, but I couldn't find any support for hold-out validation. Am I missing something here?

此外,我希望能够完全使用预定义的折叠作为参数,而不是让函数对数据进行分区.有人有什么想法吗?

Also, I'd like to be able to use exactly the pre-defined folds as parameter, instead of letting the function partition the data. Does anyone have any thoughts?

预先感谢

推荐答案

我认为您可能想使用1/5的数据作为测试集,并使用其他4/5的数据进行训练?

I think that maybe you want to use 1/5th of the data as a test set and train using the other 4/5ths?

如果是这种情况,则应首先使用createDataPartition,然后让train进行其余操作.例如:

If that is the case, you should used createDataPartition first and let train do the rest. For example:

> library(caret)
> library(mlbench)
> data(Sonar)
>
> set.seed(1)
> in_train <- createDataPartition(Sonar$Class, p = 4/5, list = FALSE)
>
> training <- Sonar[ in_train,]
> testing  <- Sonar[-in_train,]
>
> nrow(Sonar)
[1] 208
> nrow(training)
[1] 167
> nrow(testing)
[1] 41
>
> lda_fit <- train(Class ~ ., data = training, method = "lda")
> lda_fit
Linear Discriminant Analysis

167 samples
 60 predictors
  2 classes: 'M', 'R'

No pre-processing
Resampling: Bootstrapped (25 reps)

Summary of sample sizes: 167, 167, 167, 167, 167, 167, ...

Resampling results

  Accuracy  Kappa  Accuracy SD  Kappa SD
  0.71      0.416  0.0532       0.108

最大

这篇关于如何在R中实施保留验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:58