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 Center および File ExchangeStatistics 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