Crossvalidation for linear support vector machine

4 次查看(过去 30 天)
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