Giter Club home page Giter Club logo

Comments (1)

cjlin1 avatar cjlin1 commented on July 16, 2024

Please see the following faq by using the matlab interface.
Alternatively you can consider liblinear, which directly gives you w.

Q: How could I generate the primal variable w of linear SVM?

Let's start from the binary class and assume you have two labels -1 and
+1. After obtaining the model from calling svmtrain, do the following to
have w and b:

w = model.SVs' * model.sv_coef;
b = -model.rho;

if model.Label(1) == -1
w = -w;
b = -b;
end

If you do regression or one-class SVM, then the if statement is not
needed.

For multi-class SVM, we illustrate the setting in the following example
of running the iris data, which have 3 classes

[y, x] = libsvmread('../../htdocs/libsvmtools/datasets/multiclass/iris.scale');
m = svmtrain(y, x, '-t 0')

m =

Parameters: [5x1 double]
  nr_class: 3
   totalSV: 42
       rho: [3x1 double]
     Label: [3x1 double]
     ProbA: []
     ProbB: []
       nSV: [3x1 double]
   sv_coef: [42x2 double]
       SVs: [42x4 double]

sv_coef is like:

+-+-+--------------------+
|1|1| |
|v|v| SVs from class 1 |
|2|3| |
+-+-+--------------------+
|1|2| |
|v|v| SVs from class 2 |
|2|3| |
+-+-+--------------------+
|1|2| |
|v|v| SVs from class 3 |
|3|3| |
+-+-+--------------------+

so we need to see nSV of each classes.

m.nSV

ans =

 3
21
18

Suppose the goal is to find the vector w of classes 1 vs 3. Then y_i
alpha_i of training 1 vs 3 are

coef = [m.sv_coef(1:3,2); m.sv_coef(25:42,1)];

and SVs are:

SVs = [m.SVs(1:3,:); m.SVs(25:42,:)];

Hence, w is

w = SVs'*coef;

For rho,

m.rho

ans =

1.1465
0.3682

-1.9969

b = -m.rho(2);

because rho is arranged by 1vs2 1vs3 2vs3.

from libsvm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.