How to use svmtrain() with a custom kernel in Matlab?

4 views (last 30 days)
svmtain() is a function in MATLAB for SVM learning. The help doc is here:
How can I use it with a custom kernel? In the help doc, it says:
------------------------------------------------------------------------------------
@kfun — Function handle to a kernel function. A kernel function must be of the form
function K = kfun(U, V)
The returned value, K, is a matrix of size M-by-N, where U and V have M and N rows respectively. ------------------------------------------------------------------------------------
It mentions nothing about what U and V are and what M and N mean. I just don't know how to use it in the right format. Can anyone tell me what U and V are and what M and N mean? For example, the training data are 5-dimensional vectors and the kernel function is the sum of the length of the vectors. How can I write the kernel function?
Thank you!

Answers (1)

Ilya
Ilya on 22 Dec 2012
By convention adopted for svmtrain, observations are in rows and predictors are in columns. The same convention would hold for kfun. This means U is of size M-by-P, and V is of size N-by-P, where P is the number of predictors (P=5 for you). Other functions such as pdist2 in the Statistics Tlbx follow the same convention. If you want your kernel function to be a simple dot product, you would do
kfun = @(U,V) U*V';
  5 Comments
Defne Ozan
Defne Ozan on 31 Mar 2021
For anyone else having similar problems, writing the kernel function in a separate file (instead of at the bottom of the same file) and then calling it with 'KernelFunction','kernel' worked for me.
jyoti lele
jyoti lele on 22 Jul 2021
can you please give the code of 'kernel' you wrote

Sign in to comment.

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!