哈哈哈对不起,没办法。目前没有MATLAB,因此无法检查代码是否存在错误。但这是一般的想法:
生成k个(在您的情况下为10个)子样本从1开始两个计数器并预分配新矩阵: index = 1; subsample = 1; newmat = zeros("150","6") <150是样本数,6 = 4宽数据+ 1宽标签+ 1我们稍后将使用
当您仍然有数据时: while ( length(labels) > 0 )
在剩余的数据量范围内生成一个随机数: randNum = randi(length(labels)) ?我认为这是一个随机整数,范围从1到标签数组的大小(可能为0,请检查文档-如果是,请通过简单的数学运算将其设置为1
将该行添加到带有标签的新数据集中: newmat(index,:) = [data(randNum,:) labels(randNum) subsample]
从数据和标签中删除该行: data(randNum,:) = []; same for labels 0而不是for循环和简单索引的原因
增量计数器: index = index + 1; subsample = subsample + 1;
如果子样本= 11,请再次将其设为1。
最后,您应该拥有一个大型数据矩阵,该矩阵看起来几乎与原始数据矩阵完全相同,但是随机分配了“折叠标签”。
遍历所有这些和您的执行代码k(10)次。
编辑:以更易于访问的方式放置代码。注意,它仍然是伪y代码,并且不完整!另外,您应该注意,这根本不是最有效的方法,但是如果您不能使用matlab函数,也应该不会太糟。
for k = 1:10
index = 1; subsample = 1; newmat = zeros("150","6");
while ( length(labels) > 0 )
randNum = randi(length(labels));
newmat(index,:) = [data(randNum,:) labels(randNum) subsample];
data(randNum,:) = []; same for labels
index = index + 1; subsample = subsample + 1;
if ( subsample == 11 )
subsample = 1;
end
end
% newmat is complete, now run code here using the sampled data
%(ie pick a random number from 1:10 and use that as your validation fold. the rest for training
end
编辑答案2:
好的,另一种方法是创建一个与您的数据集一样长的向量
foldLabels = zeros("150",1);
然后,循环那么长的时间(150),为随机索引分配标签!
foldL = 1;
numAssigned = 0;
while ( numAssigned < 150 )
idx = randi(150);
% no need to reassign a given label, so check if is still 0
if ( foldLabels(idx) == 0 )
foldLabels(idx) = foldL;
numAssigned++; % not matlab code, just got lazy. you get it
foldL++;
if ( foldL > 10 )
foldL = 1;
end
end
end
编辑答案#2.5
foldLabels = zeros("150",1);
for i = 1:150
notChosenLabels = [notChosenLabels i];
end
foldL = 1;
numAssigned = 0;
while ( length(notChosenLabels) > 0 )
labIdx = randi(length(notChosenLabels));
idx = notChosenLabels(labIdx);
foldLabels(idx) = foldL;
numAssigned++; % not matlab code, just got lazy. you get it
foldL++;
if ( foldL > 10 )
foldL = 1;
end
notChosenLabels(labIdx) = [];
end
编辑RANDPERM
用randperm生成索引
idxs = randperm(150);
现在只分配
foldLabels = zeros(150,1);
for i = 1:150
foldLabels(idxs(i)) = sampleLabel;
sampleLabel = sampleLabel + 1;
if ( sampleLabel > 10 )
sampleLabel = 1;
end
end