-
-
Save elyase/7050373 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function svmStruct = best_svm_classifer_rbf(cdata,labels) | |
%Write a function called crossfun to calculate the predicted classification yfit from a test vector | |
%xtest, when the SVM is trained on a sample xtrain that has classification ytrain. | |
function yfit = crossfun(xtrain,ytrain,xtest, rbf_sigma, boxconstraint) | |
% Train the model on xtrain, ytrain, | |
% and get predictions of class of xtest and output it as yfit | |
svmStruct = svmtrain(xtrain,ytrain,'Kernel_Function','rbf',... | |
'rbf_sigma',rbf_sigma,'boxconstraint',boxconstraint); | |
yfit = svmclassify(svmStruct,xtest); | |
end | |
%Set up a partition for cross validation. This step causes the cross validation to be fixed. | |
%Without this step, the cross validation is random. | |
c = cvpartition(length(labels),'kfold',10); | |
%Set up a function that takes an input z=[rbf_sigma,boxconstraint], | |
%and returns the cross-validation value of exp(z). The reason to take | |
%exp(z) is twofold: rbf_sigma and boxconstraint must be positive. | |
% You should look at points spaced approximately exponentially apart. | |
% This function handle computes the cross validation at parameters ex | |
minfn = @(z)crossval('mcr',cdata,labels,'Predfun', ... | |
@(xtrain,ytrain,xtest)crossfun(xtrain,ytrain,... | |
xtest,exp(z(1)),exp(z(2))),'partition',c); | |
%Search for the best parameters [rbf_sigma,boxconstraint] with fminsearch, setting looser tolerances than the defaults. | |
opts = optimset('TolX',5e-4,'TolFun',5e-4); | |
[searchmin fval] = fminsearch(minfn,randn(2,1),opts); | |
%best parameters after the optimization | |
bp = exp(searchmin); | |
%Use the z parameters to train a new SVM classifier: | |
svmStruct = svmtrain(cdata,grp,'Kernel_Function','rbf',... | |
'rbf_sigma',bp(1),'boxconstraint',bp(2)); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment