File Exchange

image thumbnail

Kernel Ridge Regression

version (2.01 KB) by Ambarish Jash
This is ridge regression implemented using the Gaussian Kernel.


Updated 14 Apr 2010

View License

The Gaussian Kernel can be changed to any desired kernel. However such a change will not dramatically improve results. This is a variant of ridge regression using the kernel trick (Mercers Theorem).

Cite As

Ambarish Jash (2020). Kernel Ridge Regression (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)


Jiyeoup Jeong

Louis Francois//
If u normalized ahead of this, x_in slightly different from you've seen before.


Thanks, it helped a lot for a good starting point in KKR.

I used it and it worked well up to now. But, not very efficient for large dataset.

1) Why not define something like


instead of


so that the loop of lines 40-42 can be simply removed if the calculation of the Kernel matrix of lines 33-38 is modified slightly as to not touch the diagonal since we know it is going to be 1 anyway.

2) Line 50 to obtain the alpha's uses the inv() function that is really slow and according to Matlab should basically be avoided as much as possible

Using something like

Klam = x_in + lamda*eye(size(x_in));
alpha = Klam\out_data;

makes it much much faster.

3)It is clearly possible to modify the multiples loops to calculate the Kernel matrix and those when calculating final_ans so that to use only one loop. I came up with one possibility, really not sure it's the best (hence why I do not tell it here), but works for what I do. And, it enables me to consider for example in_data of size 2x10000 on my Laptop which is really really long in the original implementation (both because of the multiples loop use and the inv() use).

Thank again for the function

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

Community Treasure Hunt

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

Start Hunting!