permutegroups.m.svn-base

来自「a function inside machine learning」· SVN-BASE 代码 · 共 29 行

SVN-BASE
29
字号
function [X, y] = permuteGroups(X, y, groupSize)

% inputs
% X is an (m x n) matrix whose rows are the training inputs
% y is (m x 1) vector containing the corresponding output labels
% groupSize is the size of the group you wish to permute 

% outputs
% X permuted X
% y permuted y

num_groups = floor(size(X, 1)/groupSize); 

%Randomly permute the data matrix using Knuth's shuffling algorithm
for i=1:num_groups
   random_index = ceil(rand*(num_groups-i))+i; 
   
   %swap the current example with a random one later in the sequence
   current_example_indices = (i-1)*groupSize+1:i*groupSize; 
   random_example_indices = (random_index-1)*groupSize+1:random_index*groupSize; 
   
   temp_example  = X(current_example_indices, :); 
   X(current_example_indices, :) = X(random_example_indices,:);  
   X(random_example_indices, :) = temp_example;
   
   temp_label = y(current_example_indices); 
   y(current_example_indices) = y(random_example_indices); 
   y(random_example_indices) = temp_label; 
end 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?