【问题标题】:How to divide the dataset into all the possible combination of test and train in R?如何将数据集划分为 R 中所有可能的测试和训练组合?
【发布时间】:2019-05-01 11:46:16
【问题描述】:
    I have a dataset with 90 rows and 5 columns ,of which 4 independent variables and one is dependent variable .I need to split the dataset into test and train Leaving one out cross validation .For example 90th train ,rest all test ....89th train ..rest all train and so on


  Below is the code which I tried ,its not working

K=90 折叠

# actual cross validation
for(k in 1:nrFolds) {
  # actual split of the data
  print(k)
  fold <- which(folds == k)
  data.train <- data[-fold,]
  dim(data.train)
  data.test <- data[fold,]
  dim(data.test)

}

任何帮助将不胜感激。在此之后,我需要将此测试和训练数据集发送到分类器进行训练和测试。 谢谢

【问题讨论】:

  • 2^90 = 1.24x10^27 是相当多的组合。您当然不想将数据拆分为所有可能的训练/测试组合。
  • 将有 90 种组合,正如我在问题 ...89 train ,1 test ..88 train ,2 test .....87 train ,3 test 等中讨论的那样
  • 90 种随机组合(按大小分层)和所有可能的组合之间存在差异。也许你可以澄清一下。
  • 是的,我只需要 90 个测试和训练的组合
  • 方法合理吗?接近尾声时,您将获得 1 次火车和 89 次测试

标签: r


【解决方案1】:

如果我对您的理解正确:(我使用了 mtcars 数据集,因为您没有在问题中提供数据)

res <- lapply(1: (nrow(mtcars)-1), function(n){
  train_idx <- sample(1:nrow(mtcars), n)
  list(train = mtcars[train_idx,], test = mtcars[-train_idx,])
})

这会生成以下列表:

str(res, max.level = 2)
List of 31
 $ :List of 2
  ..$ train:'data.frame':   1 obs. of  11 variables:
  ..$ test :'data.frame':   31 obs. of  11 variables:
 $ :List of 2
  ..$ train:'data.frame':   2 obs. of  11 variables:
  ..$ test :'data.frame':   30 obs. of  11 variables:
...
 $ :List of 2
  ..$ train:'data.frame':   30 obs. of  11 variables:
  ..$ test :'data.frame':   2 obs. of  11 variables:
 $ :List of 2
  ..$ train:'data.frame':   31 obs. of  11 variables:
  ..$ test :'data.frame':   1 obs. of  11 variables:

每个项目都包含请求的训练和测试df。正如其他人指出的那样,每次运行它都会产生不同的观察组合。 (也许事先set.seed(1)?)。我以前也没有见过这种分裂。

【讨论】:

  • 是的,这就是我想要的。谢谢..但是如何将分类器模型应用于此输出....我需要将这个朴素贝叶斯分类器应用于所有组合 NBclassfier=naiveBayes(Y~X1+ X2+X3+X4,数据=训练数据)
  • 如果您只想为 naiveBayers 使用每个拆分的训练数据,您可以使用 lapply(res, function(split){NBclassfier = naiveBayes(Y ~ X1 + X2 + X3 + X4, data = split$train)})
  • 或更短的公式符号map(res, ~NBclassfier = naiveBayes(Y ~ X1 + X2 + X3 + X4, data = .x$train)})
【解决方案2】:

以下代码将随机选择的 70% 的数据拆分为训练集,其余 30% 的样本拆分为测试数据集。

data<-read.csv("c:/datafile.csv")

dt = sort(sample(nrow(data), nrow(data)*.7))
train<-data[dt,]
test<-data[-dt,]

这是另一个很好的、非常好的、非常通用的例子。

library(ISLR)
attach(Smarket)
smp_siz = floor(0.75*nrow(Smarket))  # creates a value for dividing the data into train and test. In this case the value is defined as 75% of the number of rows in the dataset
smp_siz  # shows the value of the sample size

set.seed(123)   # set seed to ensure you always have same random numbers generated
train_ind = sample(seq_len(nrow(Smarket)),size = smp_siz)  # Randomly identifies therows equal to sample size ( defined in previous instruction) from  all the rows of Smarket dataset and stores the row number in train_ind
train =Smarket[train_ind,] #creates the training dataset with row numbers stored in train_ind
test=Smarket[-train_ind,]  # creates the test dataset excluding the row numbers mentioned in train_ind

require(caTools)  # loading caTools library

## Loading required package: caTools

set.seed(123)   #  set seed to ensure you always have same random numbers generated
sample = sample.split(Smarket,SplitRatio = 0.75) # splits the data in the ratio mentioned in SplitRatio. After splitting marks these rows as logical TRUE and the the remaining are marked as logical FALSE
train1 =subset(Smarket,sample ==TRUE) # creates a training dataset named train1 with rows which are marked as TRUE
test1=subset(Smarket, sample==FALSE)

https://rpubs.com/ID_Tech/S1

另外,看看这个。

https://edumine.wordpress.com/2015/04/06/splitting-a-data-frame-into-training-and-testing-sets-in-r/

【讨论】:

    猜你喜欢
    • 2017-09-30
    • 1970-01-01
    • 2015-03-23
    • 2019-10-15
    • 2021-08-24
    • 2021-11-08
    • 2016-07-28
    • 2015-12-21
    • 2019-05-01
    相关资源
    最近更新 更多