How to do grid Search to optimize sigma using Matlab?

198 views (last 30 days)
I new to SVM, hence, please consider this when you answer the question.
I'm trying to classify IRIS data using matlab. I choose fitcecoc becouse it can classify multiple features and classes.
Next, I need to search for the best value for sigma. My understanding I need to do something called "grid Search". However, I have no clue how to do grid Search using Matlab.
Please, notice since I'm new to Matlab, I might be asking the wrong questions in the first place.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Jun 2015
Edited: Walter Roberson on 17 May 2017
firstparam = [1, 2, 3.3, 3.7, 8, 21]; %list of places to search for first parameter
secondparam = linspace(0,1,20); %list of places to search for second parameter
[F,S] = ndgrid(firstparam, secondparam);
fitresult = arrayfun(@(p1,p2) fittingfunction(p1,p2), F, S); %run a fitting on every pair fittingfunction(F(J,K), S(J,K))
[minval, minidx] = min(fitresult);
bestFirst = F(minidx);
bestSecond = S(minidx);
now the fitting was best at values bestFirst and bestSecond
It is common that you have a range of values for each parameter; in that case you use linspace() to sample in-between the range. The number of points you ask for in linspace() determines how fine of a grid you search at.
When you are searching something that should be somewhat smooth, you can use a coarse grid to determine the general area to search and then you can use that to select an area to pay more attention to. I gave an example of code for that in
Walter Roberson
Walter Roberson on 17 May 2017
The original poster needed to "search for the best value for sigma". In order to do that, the poster needed to have some function that accepted sigma (and possibly some other parameter) and returned some indication of how good that combination of values was, with smaller output indicating more desirable. That function is the "fitting function" for the purpose of the above discussion. It might also be called the "objective function" in other contexts.
I am not sure how fitcecoc fit in to the question.

Sign in to comment.

More Answers (1)

Don Mathis
Don Mathis on 24 Apr 2021
Nowadays you can just do this:
load fisheriris
bestModel = fitcecoc(meas, species, 'OptimizeHyperparameters','auto')
Don Mathis
Don Mathis on 19 Sep 2022 at 13:19
The 'OptimizeHyperparameters' argument tells fitcecoc to use Bayesian Optimization to optimize some of the hyperparameters. The Doc page below explains which hyperparameters are optimized. Gridsearch is simpler than Bayesian Optimization but usually is slower to find the best parameters when multiple parameters are being optimized.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!