天天看點

matlab cross valid,交叉驗證代碼(matlab code of cross validation)

%說明:下面是我自己寫的matlab代碼,其實matlab有自帶的交叉驗證代碼crossvalind,見Chunhou Zheng師兄的Metasample Based Sparse Representation for Tumor提供的代碼

%說明:Main_gene10FOLD_1.m有,用法非常簡單,和自己編寫的實作的是同樣的功能

% 10-fold cross validation

%This code is written by Gui Jie in the afternoon 2009/06/08.

%If you have find some bugs in the codes, feel free to contract me

% Reference:

%1. Algorithm 2 of "Shuiwang Ji and Jieping Ye. Generalized Linear Discriminant Analysis: A

%  Unified Framework and Efficient Model Selection. IEEE Transactions on Neural Networks.

%  Vol. 19, No. 10, pp. 1768-1782, 2008."

%2.Foot note 4 of "F.Wang,et al.,marginFace:A novel face recognition method by average neighborhood

%  margin maximization,Pattern Recognition (2009)"

v=10;% If v=4,it means 4-fold cross validation.

step=floor(size(fea_train,1)/v);

for j =1:v

if j~= v

startpoint=(j-1)*step+1;

endpoint=(j)*step;

else

startpoint=(j-1)*step+1;

endpoint=size(fea_train,1);

end

cv_p=startpoint:endpoint; %%%% test set position

%%%%%%%%%%%%%% test set

Test_data=fea_train(cv_p,:);

Test_lab=gnd_train(cv_p,:);  %%%%label

%%%%%%%%%%%%%% training data

Train_data=fea_train;

Train_data(cv_p,:)='';

Train_lab=gnd_train;

Train_lab(cv_p,:)='';

end