Matlab:将数据块随机划分为大小相等的集合

发布于 2024-10-29 00:51:58 字数 458 浏览 1 评论 0原文

我有一个大型数据集,需要将其随机分为 5 个几乎相等大小的集合以进行交叉验证。我之前很高兴使用 _crossvalind_ 来划分集合,但是这次我需要一次将数据块划分为这些组。

假设我的数据如下所示:

data = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18];

然后我想将它们随机分成 5 组,每组 2 块,例如像这样

g1 = [3 4], [11 12]  
g2 = [9 10]  
g3 = [1 2], [15 16]  
g4 = [7 8], [17 18]  
g5 = [5 6], [13 14]

我认为我可以使用一些 for 循环来做到这一点,但我猜肯定会有更多的成本- 在 matlab 中执行此操作的有效方法:-)

有什么建议吗?

I have a large dataset that I need to divide randomly into 5 almost equal sized sets for cross validation. I have happily used _crossvalind_ to divide into sets before, however this time I need to divide chunks of data into these groups at a time.

Let's say my data looks like this:

data = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18];

Then I want to divide them randomly into 5 groups in chunks of 2, e.g. like this

g1 = [3 4], [11 12]  
g2 = [9 10]  
g3 = [1 2], [15 16]  
g4 = [7 8], [17 18]  
g5 = [5 6], [13 14]

I think I can do this with some for-loops, but I'm guessing there must be a much more cost-efficient way to do it in matlab :-)

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-11-05 00:51:58

我将您的需求解释为集合的随机排序,但在每个集合中,元素的顺序与父集合相同。您可以使用 randperm 随机排序集合的数量并对元素使用线性索引。

dataElements=numel(data);%# get number of elements
totalGroups=5;
groupSize=dataElements/totalGroups;%# I'm assuming here that it's neatly divisible as in your example
randOrder=randperm(totalGroups);%# randomly order of numbers from 1 till totalGroups
g=reshape(data,groupSize,totalGroups)';             %'# SO formatting
g=g(randOrder,:);

g 的不同行为您提供不同的分组。

I'm interpreting your needs to be random ordering of sets, but within each set, the ordering of elements is unchanged from the parent set. You can use randperm to randomly order the number of sets and use linear indexing for the elements.

dataElements=numel(data);%# get number of elements
totalGroups=5;
groupSize=dataElements/totalGroups;%# I'm assuming here that it's neatly divisible as in your example
randOrder=randperm(totalGroups);%# randomly order of numbers from 1 till totalGroups
g=reshape(data,groupSize,totalGroups)';             %'# SO formatting
g=g(randOrder,:);

The different rows of g give you the different groupings.

哆啦不做梦 2024-11-05 00:51:58

您可以对数组 (randperm) 进行打乱,然后将其分成连续的相等部分。

data = [10 20 30 40 50 60 70 80 90 100 110 120 130 140 150];
permuted = data(randperm(length(data)));
% padding may be required if the length of data is not divisible by the size of chunks
k = 5;
g = reshape(permuted, k, length(data)/k);

You can shuffle the array (randperm) and then divide it into consequentive equal parts.

data = [10 20 30 40 50 60 70 80 90 100 110 120 130 140 150];
permuted = data(randperm(length(data)));
% padding may be required if the length of data is not divisible by the size of chunks
k = 5;
g = reshape(permuted, k, length(data)/k);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文