Question

I am working with libsvm in MATLAB and am training and testing a 1-vs-all SVM with a precomputed non-linear kernel. I'm a bit new to SVMs and I am trying to calculate the decision function. I know that for a linear SVM, we can obtain w by (according to libsvm documentation):

w = model.sv_coef'*model.SVs;

We can then calculate the decision values according to:

w'*x

and subsequently, predict the label accoding to sign(w'*x+b)where b is some threshold.

I am interested specifically in obtaining the classification score from my non-linear kernel. How do I go about this?

Was it helpful?

Solution

I'm trying to understand what do you mean by classification score. Actually you can compare the probability of each model, and select the largest one, as what I did in your previous post.

It is also fine if you are using decision functions. Suppose you are using RBF kernel, and model.Label(1) = 1, then you have (if model.Label(1) = -1, then w = -w; b = -b;)

[m,n] = size(model.SVs); % m is the number of support vectors,...
                           and n is the number of features
w = model.sv_coef; % m*1 weight vector
b = -model.rho; % scalar

Now you are given v under test. And you also have [1,n] = size(v); Then for each row i in support vectors, compute the Euclidean distance (you can vectorize the code below):

for i = 1:m
    d(i) = norm(model.SVs(i,:) - v);
    t(i) = exp(-gamma* d(i) .^2); % RBF model, t is 1*m vector
end

And the decision function (or score from the decision function) is:

s = t * w + b;

You can obtain the decision function similarly with other non-linear kernels.


EDIT

With a self-written precomputed kernel, let's take the RBF kernel as example:

% RBF kernel: exp(-gamma*|u-v|^2)

rbf = @(X,Y) exp(-gamma .* pdist2(X,Y,'euclidean').^2);

% Kernel matrices with sample serial number as first column as required 

K_train =  [(1:numTrain)' , rbf(trainData,trainData)];
K_test =   [(1:numTest)'  , rbf(testData,trainData)];

%# train and test
model             = svmtrain(trainLabel, K_train, '-t 4');
[predLabel, ~, ~] = svmpredict(testLabel, K_test, model);

%# confusion matrix
C = confusionmat(testLabel,predLabel);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top