%說明:下面是我自己寫的matlab代碼,其實matlab有自帶的交叉驗證代碼crossvalind,見Chunhou Zheng師兄的Metasample Based Sparse Representation for Tumor提供的代碼
%說明:LibSVM沒必要用這個,因為'-v'就能實現,見http://www.shnenglu.com/guijie/archive/2013/09/05/169034.html
%說明:Main_gene10FOLD_1.m有,用法非常簡單,還是用crossvalind比較好,我的程序在各類樣本數不一樣時可能沒有考慮。具體使用方法:見我的電腦:cite NIPS 2010\DLSR\code\LSR\TestUministLSRCrossValidation.m,就這樣用沒錯,不要再在“crossvalind如何使用”這個上面浪費時間。

stratified five-fold cross-validation:

http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.StratifiedKFold.html

The folds are made by preserving the percentage of samples for each class.

-----------------------------------下面的例子不看也行-------------------------------

crossvalind程序中自帶的例子
%Create a 10-fold cross-validation to compute classification error.
load fisheriris %該數據有150個樣本
indices = crossvalind('Kfold',species,10); %species是樣本的標記,indices是一個與樣本數同樣大的向量,里面的元素是1到10
cp = classperf(species); %Evaluate performance of classifier

for i = 1:10     
test = (indices == i);
train = ~test;
class = classify(meas(test,:),meas(train,:),species(train,:));
classperf(cp,class,test)
end
size(test)
size(train)
size(meas(test,:),1)
size(meas(train,:),1)
cp.ErrorRate


% 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:
%
% If you used my matlab code, we appreciate it very much if you can cite our following papers:
% Jie Gui et al., "Group sparse multiview patch alignment framework with view consistency for image classification", IEEE Transactions on Image Processing (Accepted)
% Jie Gui et al., "How to estimate the regularization parameter for spectral regression
% discriminant analysis and its kernel version?", IEEE Transactions on Circuits and 
% Systems for Video Technology, vol. 24, no. 2, pp. 211-223, 2014
% Jie Gui, Zhenan Sun, Wei Jia, Rongxiang Hu, Yingke Lei and Shuiwang Ji, "Discriminant
% Sparse Neighborhood Preserving Embedding for Face Recognition", Pattern Recognition, 
% vol. 45, no.8, pp. 2884–2893, 2012
% Jie Gui, Wei Jia, Ling Zhu, Shuling Wang and Deshuang Huang, 
% "Locality Preserving Discriminant Projections for Face and Palmprint Recognition," 
% Neurocomputing, vol. 73, no.13-15, pp. 2696-2707, 2010
% Jie Gui et al., "Semi-supervised learning with local and global consistency", 
% International Journal of Computer Mathematics (Accepted)
% Jie Gui, Shu-Lin Wang, and Ying-ke Lei, "Multi-step Dimensionality Reduction and 
% Semi-Supervised Graph-Based Tumor Classification Using Gene Expression Data," 
% Artificial Intelligence in Medicine, vol. 50, no.3, pp. 181-191, 2010

% 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