%说明:下面是我自己写的matlab代码,其实matlab有自带的交叉验证代码crossvalind,见Chunhou Zheng师兄的Metasample Based Sparse Representation for Tumor提供的代码
%说明:LibSVM没必要用这个,因为
'-v'就能实现,见
http://www.cppblog.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