support vector machine in matlab...libsvm -- a library for support vector. 7 . libsvm [1] is an open...

14
SUPPORT VECTOR MACHINE IN MATLAB Weicheng Guo

Upload: others

Post on 16-Oct-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

SUPPORT VECTOR MACHINE IN MATLAB

Weicheng Guo

Page 2: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

PROBLEM DEFINITION

2

We classify red points (in a circle with radius of 1) as 1 and green points (in a ring with radius from 1 to 2) as -1 in this figure. We want to find the hyperplane separating these two classes. Our decision boundary will have equation:

𝑀𝑀𝑇𝑇x+b=0

Page 3: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

FINDING THE DECISION BOUNDARY

3

Let {x1,…, xn} be the training data set and let yi∈{1,-1} be the class label of xi

The decision boundary should classify all points correctly with

The decision boundary can be found by solving the following constrained optimization problem

𝑦𝑦𝑖𝑖 𝑀𝑀𝑇𝑇π‘₯π‘₯𝑖𝑖 + 𝑏𝑏 β‰₯ 1 βˆ’ πœ‰πœ‰π‘–π‘–

minβ€Šβ€Š12‖𝑀𝑀‖2 + πΆπΆοΏ½πœ‰πœ‰π‘–π‘–

𝑛𝑛

𝑖𝑖=1π‘ π‘ π‘ π‘ π‘π‘π‘ π‘ π‘ π‘ π‘ π‘ π‘ π‘ β€Šβ€Šβ€Šβ€Šπ‘ π‘ π‘‘π‘‘β€Šβ€Šβ€Šβ€Šπ‘¦π‘¦π‘–π‘– 𝑀𝑀𝑇𝑇π‘₯π‘₯𝑖𝑖 + 𝑏𝑏 β‰₯ 1 βˆ’ πœ‰πœ‰π‘–π‘–

Page 4: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

REFORMULATING AS A LAGRANGIAN

4

We can introduce Lagrange multipliers to represent the condition and thus have the following formulation:

j1 1 1

1

1max x x2

subject to 0 , 0

n n nT

i i j i j ii i j

n

i i ii

y y

C y

Ξ± Ξ±Ξ±

Ξ± Ξ±

= = =

=

βˆ’

≀ ≀ =

βˆ‘ βˆ‘βˆ‘

βˆ‘

Page 5: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

KERNEL TRICK

5

Since it is a nonlinear classification in this case, we can use kernel functions that operates on the lower dimension vectors xi and xj to produce a value equivalent to the dot product of the higher-dimensional vectors. Therefore, the data can be linearly separated in that space and the formulation is transformed to:

1 1 1

1

1max ( , )2

subject to 0 , 0

n n n

i i j i j i ji i j

n

i i ii

y y K x x

C y

Ξ± Ξ±Ξ±

Ξ± Ξ±

= = =

=

βˆ’

≀ ≀ =

βˆ‘ βˆ‘βˆ‘

βˆ‘

Page 6: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

KERNEL FUNCTIONS

6

Linear kernel

Polynomial kernel with Ξ³, d and r

Radial basis function kernel with Ξ³

Sigmoid kernel with Ξ³ and r

𝐾𝐾(π‘₯π‘₯𝑖𝑖 , π‘₯π‘₯𝑗𝑗) = π‘₯π‘₯𝑖𝑖𝑇𝑇π‘₯π‘₯𝑗𝑗

𝐾𝐾(π‘₯π‘₯𝑖𝑖 , π‘₯π‘₯𝑗𝑗) = 𝛾𝛾π‘₯π‘₯𝑖𝑖𝑇𝑇π‘₯π‘₯𝑗𝑗 + r 𝑑𝑑

𝐾𝐾 π‘₯π‘₯𝑖𝑖 , π‘₯π‘₯𝑗𝑗 = exp(βˆ’π›Ύπ›Ύ||π‘₯π‘₯𝑖𝑖 βˆ’ π‘₯π‘₯𝑗𝑗||2)

𝐾𝐾 π‘₯π‘₯𝑖𝑖 , π‘₯π‘₯𝑗𝑗 = tanh(𝛾𝛾π‘₯π‘₯𝑖𝑖𝑇𝑇π‘₯π‘₯𝑗𝑗 + r)

Page 7: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

LIBSVM -- A LIBRARY FOR SUPPORT VECTOR

7

LIBSVM[1] is an open source machine learning library developed at the National Taiwan University and written in C++ though with a C API. LIBSVM implements the SMO algorithm for kernelized support vector machines (SVMs), supporting classification and regression. [1] https://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html

Differences between LIBSVM and SVM function in Matlab

LIBSVM SVM function in Matlab

Classification or regression Both Classification

Classification type Two-class and multi-class Two-class

SVM formulations C-SVC, nu-SVC, one-class SVM, epsilon-SVR, nu-SVR C-SVC

Page 8: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

LIBSVM -- A LIBRARY FOR SUPPORT VECTOR

8

PROCEDURE 1. Transform data to the format of an SVM package

2. Conduct simple scaling on the data

3. Consider the RBF kernel K(π‘₯π‘₯𝑖𝑖,π‘₯π‘₯𝑗𝑗) = exp(βˆ’Ξ³| π‘₯π‘₯𝑖𝑖 βˆ’ π‘₯π‘₯𝑗𝑗 |2)

4. Find the best parameter C and Ξ³

5. Use the best parameter C and Ξ³ to train the whole training data

6. Test

Page 9: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

TRAINING RESULT

9

GAMMA=0.1

Page 10: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

TRAINING RESULT

10

GAMMA=3

Page 11: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

TRAINING RESULT

11

GAMMA=50

Page 12: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

TESTING RESULT

12

GAMMA=3

Page 13: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

TESTING RESULT

13

GAMMA=3

Page 14: SUPPORT VECTOR MACHINE IN MATLAB...LIBSVM -- A LIBRARY FOR SUPPORT VECTOR. 7 . LIBSVM [1] is an open source machine learning library developed at the National Taiwan University and

CONCLUSION

14

SVM is a new method of machine learning based on statistics theory. In contrast to β€˜black box’ learning approaches (artificial neural network), SVM is supported by certain mathematical models.

The training of SVM is relatively easy. Unlike in neural network, SVM can get global optimum and the training time does not depend on dimensionality of feature space any more by using the kernel trick.

The key to training SVM is to select a kernel function and its parameters, which cannot be conducted by a principled manner.