R:从矩阵

时间:2018-03-18 20:41:42

标签: r cross-validation

我正在进行交叉验证,我想将数据分成3倍。 我创建了一个mat=matrix(sample.int(10, 9*100, TRUE), 6, 10)的矩阵,如下所示:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   10   10    9    3    3    3    4    4    3     9
[2,]    9    3    5    1    3    9    5    5    4     8
[3,]    7    6    6    3    8    2    3   10    7     4
[4,]    7    4   10    8    7    5    2    6    2     8
[5,]    9    7    7    5    3    9    5    8    7     8
[6,]    3    3    1    2    9    3    6    7    6     9

我想得到3个数据矩阵:

弃1

       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   10   10    9    3    3    3    4    4    3     9
 [2,]    9    3    5    1    3    9    5    5    4     8

弃2

       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    7    6    6    3    8    2    3   10    7     4
[2,]    7    4   10    8    7    5    2    6    2     8

弃3

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    9    7    7    5    3    9    5    8    7     8
[2,]    3    3    1    2    9    3    6    7    6     9

这是我的代码:

require(stats)
mat=matrix(sample.int(10, 9*100, TRUE), 6, 10)
folds=cut(seq(1, nrow(mat)), breaks = 3, labels = FALSE)
#Perform 10 fold cross validation 
for(i in 1:3){
  #segment your data by folds using the which() function 
  testIndexes=which(folds==i, arr.ind = TRUE)
  testData=mat[testIndexes,]
  trainData=mat[-testIndexes,]
}

我从弃1 弃2 获得的培训数据已连接,我想单独生成它们。 这是生成的训练集,应该分成两个部分。

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   10   10    9    3    3    3    4    4    3     9
[2,]    9    3    5    1    3    9    5    5    4     8
[3,]    7    6    6    3    8    2    3   10    7     4
[4,]    7    4   10    8    7    5    2    6    2     8

0 个答案:

没有答案