필터 지우기
필터 지우기

SVM classification with different kernels

조회 수: 3 (최근 30일)
caterina
caterina 2012년 12월 27일
댓글: Jeremy Huard 2023년 1월 31일
I am using an SVM (SVM_train, Bioinformatics toolbox) to classify data, and I would like to have my final trained SVM models with different kernel functions. I didn't understand how to specify my own kernel maps: if I would like to use a Cauchy kernel defined as
k=(1/(1+(|u-v|^2/sigma))
where u,v are the vectors of the X data, and sigma is the parameter defined by the user (sigma=2.5). How I have to edit this matlab statement?
svmStruct = svmtrain(X,group,'Kernel_Function', 'rbf','RBF_Sigma', 2.5, 'Method', 'QP');
Thank you!
  댓글 수: 1
Jeremy Huard
Jeremy Huard 2023년 1월 31일
Users using R2014b or newer should use the fitcsvm function for for one-class and binary classification or fitcecoc for multiclass classification instead.
There you can specify a custom kernel function by adding the 'KernelFunction','myfunction' name-value pair, where myfunction is the name of your function containing the kernel function definition.

댓글을 달려면 로그인하십시오.

답변 (4개)

Ilya
Ilya 2012년 12월 27일

caterina
caterina 2012년 12월 28일
Hi Ilya anche thank you for you answer. I read it that thread before writing and still I did not understand what it is wrong in my code. I created my kernel code that I saved in a folder 'pgm_qkda'. You can find the code below
clear all
clc;
addpath('/Users/Documents/Parallels/shared/D/KQDA/matlab2/pgm_qda');
x=[3 3 4 5 6 7 8 0.1 0.2
3 4 3 5 7 7 5 0.5 0.6
7 7 2 3 3 4 5 0.4 0.5
3 3 4 5 6 2 6 0.2 0.3];
group=[1,1, 2, 2]';
p1=2.5;
svmStruct = svmtrain(x,group,'Kernel_Function', @(u,v) kfun(u,v,p1), 'Method', 'QP');
The kfun function is defined as:
function kval = kfun(u,v,p1);
dot=((u-v)*(u-v)')/p1;
kval = 1/(1+dot);
Matlab gives me a warning message: Error using ==> svmtrain at 453 Error calculating the kernel function: Matrix dimensions must agree.
I do not understand where is my mistake. If you can help me I really appreciate.
  댓글 수: 2
Ilya
Ilya 2012년 12월 28일
Replace
kval = 1/(1+dot);
with
kval = 1./(1+dot);
./ is for elementwise division. / is for matrix division.
Also, please do not post follow-up questions as answers to your own posts. Use comments for that.
Fourth Sem Geethanjali Electrical and Electronics Engineering
hi
svmStruct = svmtrain(x,group,'Kernel_Function', @(u,v) kfun(u,v,p1), 'Method', 'QP');
how will the function call change if i use fitcsvm instead of svmtrain.
can you please help me.

댓글을 달려면 로그인하십시오.


Sandy
Sandy 2014년 10월 3일
Hi Caterina,
Just want to check whether you are able to rectify tha above issues. I am also getting the same error. Will you post me the code for above custom kernel.
Thanks in advance.

muqdad aljuboori
muqdad aljuboori 2017년 4월 2일
Hi I very interested in this discussion I tried to apply the new funcion in my project but it doesnt work that what i did
SVR1 = fitrsvm(TrainInputs,TrainTargets,...
'KernelFunction', @(u,v) kfun3(u,v) ,...
'KernelScale','auto','Standardize',true);__
and the error is
Error using classreg.learning.modelparams.SVMParams.make (line 225) You must pass 'KernelFunction' as a character vector.
Error in classreg.learning.FitTemplate/fillIfNeeded (line 598) this.MakeModelParams(this.Type,this.MakeModelInputArgs{:});
Error in classreg.learning.FitTemplate.make (line 124) temp = fillIfNeeded(temp,type);
any suggestion ??? thanks
  댓글 수: 1
Jeremy Huard
Jeremy Huard 2023년 1월 31일
You should specify the kernel function as char array:
SVR1 = fitrsvm(TrainInputs,TrainTargets,...
'KernelFunction', 'kfun3',...
'KernelScale','auto','Standardize',true);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by