Libsvm with a pre-computed kernel: how to calculate classification scores?

I work with libsvm in MATLAB and train and test SVM 1-vs-all with a pre-computed non-linear kernel. I am a little new to SVM and I am trying to compute a solution function. I know that for linear SVM we can get w by (as per the libsvm documentation):

w = model.sv_coef'*model.SVs;

Then we can calculate the values โ€‹โ€‹of the solutions in accordance with:

w'*x

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

I am interested in getting a classification score from my non-linear kernel. How should I do it?

+3
source share
1 answer

, . , .

, . , โ€‹โ€‹RBF model.Label(1) = 1, ( model.Label(1) = -1, 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

v. [1,n] = size(v); i ( ):

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

( ):

s = t * w + b;

.


, โ€‹โ€‹RBF :

% 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);
+2

All Articles