# How to do grid Search to optimize sigma using Matlab?

161 views (last 30 days)
Diver on 28 Jun 2015
Answered: Don Mathis on 24 Apr 2021
Hi;
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.

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 http://uk.mathworks.com/matlabcentral/answers/222803-how-to-fit-6-curves-simultaneously-to-solve-for-2-unknowns
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.

Don Mathis on 24 Apr 2021
Nowadays you can just do this:
bestModel = fitcecoc(meas, species, 'OptimizeHyperparameters','auto')