📄 srsplit.m
字号:
function [RIndex EndIndex]=srsplit(mode, label, ratio, th)%==========================================================================% To extract reduced set for SVM or SVR, stratified over % (1) classes (SVM), or slices of sorted responses (SVR); and% (2) cross-validation folds, if applicable.%--------------------------------------------------------------------------% Inputs:% mode [1 x 1] : learning algorithm. {'SVM', 'SVR'}% label [m x 1] : training data label or response% ratio [1 x 1] : ratio of reduced set size to the full data size % th [1 x 1] : number of folds in cross-validation%--------------------------------------------------------------------------% Outputs:% For SVM% RIndex [th, ?] : the ith row records extracted indices of% reduced set for the ith cross-validation fold% of all classes%% EndIndex [? x z] : the end index of each class in a cross-validation fold%% For SVR% RIndex [th, ?] : the ith row records extracted indices of% reduced set for the ith cross-validation fold%==========================================================================rand('state',sum(100*clock)); % set random number generator seedif(~exist('th', 'var')) th =1;endRows=length(label);if(strcmpi(mode, 'svm')) % SVM label(find(label==-1)) = 2; Num_label = length(unique(label)); EndIndex = zeros(Num_label+1,1); RIndex = []; for i = 1 : Num_label Temp_Index = find(label==i); Temp_Rows = length(Temp_Index); Temp_Size = fix(Temp_Rows*ratio); Temp_Index_R = Temp_Index(randperm(Temp_Rows)); Temp=[]; for j = 1 : th Temp(j,:) = Temp_Index_R((j-1)*Temp_Size+1 : j*Temp_Size); end RIndex = [RIndex Temp]; EndIndex(i+1) = EndIndex(i)+Temp_Size; end EndIndex = EndIndex(2:end);elseif(strcmpi(mode, 'svr')) % SVR [SLabel, SIndex]=sort(label); clear SLabel; size=fix(Rows*ratio);% set the number of slices num=fix(Rows/size);% size of each slice, % there might be some left-out elements for a partial slice boxes=zeros(size, num); for i=1: size base=(i-1)*num; if(i<size) boxes(i, :)=base + randperm(num);%random permutation within each slice else % insure that elements from the last partial slice get a chance % to enter the reduced set tmp=base + randperm(Rows-(num*(size-1))); boxes(i, :)=tmp(1: num); end end RIndex=zeros(num, size); for i=1: num RIndex(i, :)=SIndex(boxes(:, i))'; % pick one index from each slice within each CV-fold end RIndex=RIndex(1:th, :);else error([mode, ', unknown!!']);end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -