Crossvalidation for linear support vector machine

조회 수: 1 (최근 30일)
MiauMiau
MiauMiau 2015년 9월 17일
Hi
I have the following code which performs k-fold crossvalidation. And as you can see the kernel chosen is a linear kernel. However, what I want to do is to perform gridsearch for the parameter C - how is that done in Matlab? Would you use standard gridsearch method as an outer loop on the k-fold crossvalidation or is there a gridsearch function specific for SVMs (I did not find any). Does anyone have me example code?
This is my code so far:
load feature
load class
k = 4; %number of folds
cvFolds = crossvalind('Kfold', class, k);
cp = classperf(class);
for i = 1:k %# for each fold
testIdx = (cvFolds == i); %# get indices of test instances
trainIdx = ~testIdx; %# get indices training instances
%# train an SVM model for autistic vs TD, k-fold crossvalidatio
svmModel = svmtrain(feature(trainIdx,:), class(trainIdx), ...
'Autoscale',true, 'Showplot',false, 'Method','QP', ...
'BoxConstraint',0.2, 'Kernel_Function','linear');
%# test using test instances
pred = svmclassify(svmModel, feature(testIdx,:), 'Showplot',false);
%# evaluate and update performance object
cp = classperf(cp, pred, testIdx);
end
%# get accuracy
cp.CorrectRate
%# get confusion matrix
%# columns:actual, rows:predicted, last-row: unclassified instances
cp.CountingMatrix

답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by