File Exchange

image thumbnail

Support Vector Regression

version (34.5 KB) by Bhartendu
On-line support vector regression (using Gaussian kernel)


Updated 22 May 2017

View License

On-line regression On-line learning algorithms are not restricted to classification problems. The update rule for the kernel adatron algorithm also suggests a general methodology for creating on-line versions of the optimisations.
making the first update of the kernel adatron algorithm equivalent to αi ← αi + ∂W(α) ∂αi making it a simple gradient ascent algorithm augmented with corrections to ensure that the additional constraints are satisfied. If, for example, we apply this same approach to the linear ε-insensitive loss version of the support vector regression algorithm.
One of the advantages of Support Vector Machine, and Support Vector Regression as the part of it, is that it can be used to avoid difficulties of using linear functions in the high dimensional feature space and optimization problem is transformed into dual convex quadratic programmes. In regression case the loss function is used to penalize errors that are grater than threshold - . Such loss functions usually lead to the sparse representation of the decision rule, giving significant algorithmic and representational advantages.

Kernel Methods for Pattern Analysis byJohn Shawe-Taylor & Nello Cristianini

Cite As

Bhartendu (2020). Support Vector Regression (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (45)

Aisha Sa'ad

I have a problem running this code using the linear kernel. it shows NaN for the b, w and mse while the graph only shows the actual value on zero horizontally. can you also please explain to me why your data has 3 dimensions x-y-z? Please what method did you use to obtain the third column of the data (z)? Thank you

To test the model on a new data set, the Alpha was generated for only the training set.
How would you get the alpha matrix for a new set without retraining?


To people asking for how to predict:
plz check the example tab Support_Vector_Regression.mlx

hey can you help me by sharing how to predict for the new data

To test the model on a new data set, the Alpha was generated for only the training set.
How would you get the alpha matrix for a new set without retraining?

Thanks for your interesting code!
How can we denormalize the predicted data?
Your code just shows the normalized prediction data.

Can I predict next value prediction by using this code?
for example, i have 100 train data, can i predict 101-105? Please Help me.

Aminah Hina

I don't want to normalize my data as I want predicted values in their true form.
My problem is that I have train data of matrix [41x11] (where first 10 columns are features and 11th columns is response) and test data of matrix [6x10] (only features).
now when I have to test my data, how should I compute this module of your code?

% Predicted values

for j=1:N
disp('[Actual Values Predicted Values]')
disp([y(1:10) ,fx(1:10)])
% Mean Square error (Gaussian Kernel)

Aminah Hina

The code is not working properly for k=l and k=p,i.e poly nominal and linear SVR. it fails to calculate weights giving NAN values . pasting values that are displayed in command window
N =39
itr =1
Total number of iteration 1
w =


b =
[Actual Values Predicted Values]
134 NaN
123 NaN
115 NaN
101 NaN
80 NaN
111 NaN
93 NaN
95 NaN
87 NaN
129 NaN
mse =


please suggest what parameters needs to be changes.


May i know how accurate is this code, I want to learn the svm by refering to the online lecture code


Mr Vadlan: It's a regression problem. We do not calculate accuracy for any regression problem. R2 score can be calculated but still that is not called "accuracy". Also you asked code for prediction, which I already have given in code and example.

oh and can you please add the code for predict new data ? thank you

Hi Bhartendu, can your code show an accuracy of this method ?


Mr Franck, check the dimension,may be previous build requires exact dimension while handling matrices. It works for me in newer build of Matlab (2017b or later).

nene franck

Error in SupportVectorRegression (line 23)

Thanks for sharing your code, but can it be used for multi output regression too ?


Tian Lan:
Make sure to normalize the data, the method will work. If size (no. of attributes) are not same then little modification will be required especially in choosing the kernel. In case the data seems to be linear, then use linear kernel and like wise.

Tian Lan

Hi, Bhartendu,
If the data is non-Gaussian distributed, can this method work?
Which kernel should I choose?
I am new to SVM, thank you for your time.


Step 1. zscore normalization mentioned below:


Step 2. To get Predicted_values on x_validation: (after applying normalization to x_validation)

for j=1:N


Can you please explain me how to test a validation data set with the trained SVR model.

Thank you.

how to make the gerenal function f(x) for predicting sir?


Lawrence Soon:
No, it's just fundamental SVR.

Hi Bhartendu, I am new to SVR, can this library support the epsilon-SVR?

vahid izadi


sorry, can not use the code since the alpha has a MM rows but the x_test has M rows (MM~=M).


Hi Bhartendu, Thank you for your code. I am just wondering how to get the predicted value for test set (M*N) based on training set (MM*N) (note: M~==MM). Because I can use the code since the alpha has a MM rows but the x_test has M rows:
for j=1:M


Hi D W
Please ask, I will be more then happy to answer you.


Hi Bhartendu, can you answer me? Thank you very much!


N is the number of training data samples. So after training, alpha has N rows. But if test data has M (M~=N) number of samples, the following code does not work because alpha has N rows, not M rows. Thank you, Bhartendu!
for j=1:M


To get Predicted_values on Test_set: (after applying normalization to Test_set)

for j=1:N



After training, how to get predicted value on test set?


Tord Bjørnevaagen
May be zscore normalization has not been executed properply, try something like mentioned below:


I may have misunderstood something, but when I train the SVM on a trainset, the result usually performs well on the trained set. But it often performs horribly on a blind set it did not have access to during training. Are there any tweaks to change this?


Tord Bjørnevaagen
I will update considering your suggestions shortly.

Brilliant work!

I have, however, a few suggestions for improvements.

In kernel.m, you write "length(x)". If x has more samples (vertical dimention) than parameters (horizontal direction), length will return the number of samples. If not, it will return the number of parameters. I suggest you replace this with size(x,1) if you want it to apply the number of samples, and size(x,2) for parameters, to avoid crashes. (I assume you want the first).

Also, I suggest adding the line "fx1 = nan(numSamples);" just after the line "% Predicted values", for the sake of speed and readability.

Chin Chou


Damo Nair, try the following:

clear w;

Damo Nair

The size of 'alpha' is 200 x 1 & the size of 'x' is 200 x 2.


The code is generalised, this kind of error is unfortunate, Please tell me the size of 'alpha' and 'x' (at the moment when you are getting this error).

Damo Nair

When I run your demo SupportVectorRegression on Matlab R2011b it gives me the following error ...
w=sum(alpha.*x) Error using .*
Matrix dimensions must agree.

After a 1000 iterations.

MATLAB Release Compatibility
Created with R2016a
Compatible with any release
Platform Compatibility
Windows macOS Linux