How to write simple predict() function for ClassificationSVM

I have trained ClassificationSVM. What is the simplest way to write function working like predict( SVMModel , X ) ? I would be gratefull for equation containing properties names from ClassificationSVM class.
Thanks !

Answers (1)

First you'd have to select features, check out:
https://www.mathworks.com/discovery/feature-selection.html
for info and/or ideas on how to do this.
You'll also need to decide what kind of model you're using, for the standard multi-linear regression check out the documentation on regress()
https://www.mathworks.com/help/stats/regress.html?s_tid=srchtitle
Now - if you really want the simplest model, I'd say you could skip feature selection and just run:
regress(labels, features)
and this will give you the regressor for each feature. Now your model is simply
prediction = A1*feature1 + ... + AN*featurenN
But I would generally advise against this, mainly becasue of the danger of overfitting. I suggest building a train and test based algorithm

3 Comments

Thanks for your answer, but I'm afraid I did not make myself clear. My goal is to understand how predict() calculates output class from trained ClassificationSVM class. In https://www.mathworks.com/help/stats/classificationsvm-class.html we have :
If KernelParameters.Function is 'linear', then the software estimates the classification score for the observation x using
f(x)=(x/s)′β+b.
Mdl stores β, b, and s in the properties Beta, Bias, and KernelParameters.Scale, respectively.
If KernelParameters.Function is not 'linear', then Beta is empty ([]).
Well, I wrote something like that:
f = (X/svm.KernelParameters.Scale)*svm.Beta;
f = f + svm.Bias;
if f>=0
out = 1;
else
out = 0;
end
But the output not always is equal to predict( svm, X ). Am I wrong thinking that in svm.Beta are stored coefficients of separating hyperplane?
Did you get the answer? I also have the same doubt. In my case, I am using polynomial kernel (hence beta is an empty matrix). How can I write simple predict function to test new data sample?
dear all, I have similar doubt . I have trained model generated by classification learner model. now when i use it for predicting the test data, the error appears as Function 'subsindex' is not defined for values of class 'cell'.
Error in trainClassifier (line 48) predictors = inputTable(:, predictorNames);
Error in Test_svm1 (line 7) [trainedClassifier, validationAccuracy] = trainClassifier(trainingData); pl help

Sign in to comment.

Asked:

on 2 Dec 2016

Commented:

on 10 Sep 2018

Community Treasure Hunt

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

Start Hunting!