function [ knnacc, lsacc, svmacc, parameters ] = crossvalidation( X, y)
disp 'crossvalidation';
[n,d] = size(X);
m = floor(0.1*n);
fold5 = floor(m/5);
mod5 = 0;
if(fold5*5 ~= m)
mod5 = m-fold5*5;
fold5 = fold5+1;
end
knn_acc = zeros(10,1);
ls_acc = zeros(10,1);
svm_acc = zeros(10,1);
knn_k = [1 3 5 10 15];
knn_accu = zeros(size(knn_k,2),1);
ls_lambda = [0.01 0.1 0.5 1 5 10 100];
ls_accu = zeros(size(ls_lambda,2),1);
svm_C = [1 2 5 10 20 50];
svm_C_accu = zeros(size(svm_C,2),1);
svm_sigma = [0.1 0.2 0.4 0.6 0.8 1];
svm_C_sigma_accu = zeros(size(svm_C,2),size(svm_sigma,2));
parameters = zeros(4,10);
for i = 1:10
i
randomArr = randperm(n);
traindata = X(randomArr(1:m),:);
ytrain = y(randomArr(1:m),:);
testdata = X(randomArr(m+1:end),:);
ytest = y(randomArr(m+1:end),:);
for j = 1:5
if(j == 5)
testj = traindata(m+2-fold5:end,:);
ytestj = ytrain(m+2-fold5:end,:);
trainj = traindata(1:m+1-fold5,:);
ytrainj = ytrain(1:m+1-fold5,:);
else if(j <= mod5 | mod5 == 0)
testj = traindata(fold5*(j-1)+1:fold5*j,:);
ytestj = ytrain(fold5*(j-1)+1:fold5*j,:);
trainj = traindata([1:fold5*(j-1) fold5*j+1:end],:);
ytrainj = ytrain([1:fold5*(j-1) fold5*j+1:end],:);
else
testj = traindata(fold5*mod5+fold5*(j-1-mod5)+1:fold5*mod5+fold5*(j-mod5)-1,:);
ytestj = ytrain(fold5*mod5+fold5*(j-1-mod5)+1:fold5*mod5+fold5*(j-mod5)-1,:);
trainj = traindata([1:fold5*mod5+fold5*(j-1-mod5) fold5*mod5+fold5*(j-mod5):end],:);
ytrainj = ytrain([1:fold5*mod5+fold5*(j-1-mod5) fold5*mod5+fold5*(j-mod5):end],:);
end
end
%knn
for k = 1:size(knn_k,2)
[ ypred, accuracy ] = knnclassifier( trainj, testj, ytrainj, ytestj, knn_k(k) );
knn_accu(k) = accuracy;
end
%ls
for k = 1:size(ls_lambda, 2)
[ ypred, traccuracy, tetaccuracy ] = lsclassifier( trainj, ytrainj, testj, ytestj, ls_lambda(k) );
ls_accu(k) = tetaccuracy;
end
%svm
for k = 1:size(svm_C,2)
[ ypred, accuracy ] = softsvm( trainj, ytrainj, testj, ytestj, 0, svm_C(k) );
svm_C_accu(k) = accuracy;
end
for k = 1:size(svm_C,2)
for ll = 1:size(svm_sigma,2)
[ ypred, accuracy ] = softsvm( trainj, ytrainj, testj, ytestj, svm_sigma(ll), svm_C(k) );
svm_C_sigma_accu(k,ll) = accuracy;
end
end
end %for j = 1:5
%knn
knn_maxacc = 0;
knn_maxacc_k = 1;
for jj = 1:size(knn_accu,2)
if(knn_maxacc < knn_accu(jj))
knn_maxacc = knn_accu(jj);
knn_maxacc_k = knn_k(jj);
end
end
[ ypred, accuracy ] = knnclassifier( traindata, testdata, ytrain, ytest, knn_maxacc_k );
knn_acc(i) = accuracy;
%ls
ls_maxacc = 0;
ls_maxacc_lambda = 0;
for jj = 1:size(ls_lambda,2)
if(ls_maxacc < ls_accu(jj))
ls_maxacc = ls_accu(jj);
ls_maxacc_lambda = ls_lambda(jj);
end
end
[ ypred, traccuracy, tetaccuracy ] = lsclassifier( traindata, ytrain, testdata, ytest, ls_maxacc_lambda );
ls_acc(i) = tetaccuracy;
%svm
svm_maxacc = 0;
svm_maxacc_C = 0;
svm_maxacc_sigma = 0;
for jj = 1:size(svm_C,2)
if(svm_maxacc < svm_C_accu(jj))
svm_maxacc = svm_C_accu(jj);
svm_maxacc_C = svm_C(jj);
end
end
for jj = 1:size(svm_C,2)
for kk = 1:size(svm_sigma,2)
if(svm_maxacc < svm_C_sigma_accu(jj,kk))
svm_maxacc = svm_C_sigma_accu(jj,kk);
svm_maxacc_C = svm_C(jj);
svm_maxacc_sigma = svm_sigma(kk);
end
end
end
[ ypred, accuracy ] = softsvm( traindata, ytrain, testdata, ytest, svm_maxacc_sigma, svm_maxacc_C );
svm_acc(i) = accuracy;
parameters(1,i) = knn_maxacc_k;
parameters(2,i) = ls_maxacc_lambda;
parameters(3,i) = svm_maxacc_C;
parameters(4,i) = svm_maxacc_sigma;
end %for i = 1:10
knnacc = mean(knn_acc);
lsacc = mean(ls_acc);
svmacc = mean(svm_acc);
end