Matlab:将数据按比例随机划分

将10行数据随机划分为5份,将其中1份作为测试集,另4份作为训练集

1. 随机生成10维1:5等数量随机数

m = 10;
k=5;
data = [1;2;3;4;5;6;7;8;9;10];
indices = crossvalind('Kfold',m,k)

output

indices = 10×1
     1
     2
     1
     3
     2
     4
     5
     5
     4
     3

2.按标签indices划分数据,生成五次测试集及训练集(1比4)

for i = 1:5
    testIndic = (indices == i);
    trainIndic = ~testIndic;
    testData = data(testIndic,:)
    trainData = data(trainIndic,:)
end

output

testData = 2×1
     1
     3
trainData = 8×1
     2
     4
     5
     6
     7
     8
     9
    10
testData = 2×1
     2
     5
trainData = 8×1
     1
     3
     4
     6
     7
     8
     9
    10
testData = 2×1
     4
    10
trainData = 8×1
     1
     2
     3
     5
     6
     7
     8
     9
testData = 2×1
     6
     9
trainData = 8×1
     1
     2
     3
     4
     5
     7
     8
    10
testData = 2×1
     7
     8
trainData = 8×1
     1
     2
     3
     4
     5
     6
     9
    10
上一篇:组件中监视Vuex的数值


下一篇:XCTF 攻防世界 WEB练习题1-5