What is the predict formula for fitrsvm (gaussian kernel) generated model?

Using fitrsvm, I have generated a model from X and Y data:
mdl = fitrsvm(X,Y,'Standardize',true,...
'KernelFunction','gaussian','KernelScale','auto');
Given an input x, the predicted output using Matlab is:
y_expected=predict(mdl,x);
My attempt below at replicating 'predict' function does not yield the correct answer as y and y_expected do not agree.
y=mdl.Bias;
for i=1:length(mdl.Alpha)
xmxd=x-mdl.SupportVectors(i,:);
y=y + mdl.Alpha(i)*exp(-xmxd*xmxd'/2/mdl.KernelParameters.Scale);
end
y
What is the underlying formula behind the predict function in this case? I need it to write equivalent java code from parameters in the mdl model.

1 Comment

Hello, Actually i tried with the predictive formula given above but am not able to get the result what am getting using predict function. if any one help me regarding this please. what formula i have to use because i want to use the model in microcontroller to validate. am using 4 predictors and am using gaussian kernel

Sign in to comment.

Answers (2)

It looks like all the relevant formulas (and references) are in this documentation page.

5 Comments

Thanks. I had already seen the documentation you refer to. There are questions that remain. It is not clear to me what the equivalences are between mdl.Bias, mdl.Alpha, mdl.SupportVector, etc and the symbols in the documentation and how they relate.
In principle, you should be able to look into code itself to trace what is happening with any given calculation. (In practice, you will sometimes be thwarted when you get to a built-in MATLAB function that you cannot see the code for.)
You can start with
edit fitrsvm
which will immediately lead you to
edit RegressionSVM
You can also use debug mode to go line-by-line through the code to trace what is happening in any given calculation.
Quite right about being thwarted by built-in matlab code. So, no luck with debug mode.
However, my replicating predict function is correct if the parameters 'Standardize' and KernelScale' are omitted. The remaing problem is that it's about 12 times slower!
Thanks for your comments. Much appreciated.
The best form of thanks is upvoting and/or accepting an answer that you find helpful. This rewards the contributor, and points future users to helpful answers.
Could take a look into my question? https://uk.mathworks.com/matlabcentral/answers/842800-why-matlab-svr-is-not-working-for-exponential-data-and-works-well-with-data-that-fluctuates?s_tid=prof_contriblnk

Sign in to comment.

Hello, Actually i tried with the predictive formula given above but am not able to get the result what am getting using predict function. if any one help me regarding this please. what formula i have to use because i want to use the model in microcontroller to validate.
Thanks,

Asked:

on 18 May 2016

Commented:

on 29 May 2021

Community Treasure Hunt

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

Start Hunting!