⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libsvm-guide.txt

📁 libsvm-demo,支持向量机的演示程序,对初学者很有用!
💻 TXT
字号:
A demonstration in using LIBSVM for SVM classification
------------------------------------------------------

This text serves as a step-by-step guide for using LIBSVM for SVM
classification by an example. The website for LIBSVM is

http://www.csie.ntu.edu.tw/~cjlin/libsvm/

1) download the LIBSVM package, version 2.81, as in Nov 2005.

For the c binary users, please download:
http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm.cgi?+http://www.csie.ntu.edu.tw/~cjlin/libsvm+zip

For the Matlab users, LIBSVM provides a matlab interface to the c
binary, please download:
http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/matlab.cgi?+http://www.csie.ntu.edu.tw/~cjlin/libsvm/matlab+zip

For your convenience, all the required files, i.e., the libsvm
matlab interface package, the e6887 package and the demo file
(spr_libsvm_demo.m), are provided in libsvm_demo.zip. The demo
file contains the lines of code described in this text.

2) This guide shows an example which uses the matlab interface.
The c binary users are encouraged to read the README file
accompanying the downloaded package. The matlab interface users
may eventually want to take the README file in the downloaded
package as the ultimate guide, while this text may serve as an
initial guide for using the LIBSVM matlab interface.

The demo example is extracted from the README.

3) Unpack the svmlib package to c:\libsvm-mat-2.8-1 and the e6887
package to c:\e6887. You can change the directory according to
your needs.

4) In matlab, add the libsvm and the e6887 directory to the matlab
search path.

matlab> addpath c:\libsvm-mat-2.8-1 -end

matlab> addpath c:\e6887 -end

5) Load data

matlab> load heart_scale.mat;

matlab> whos

  Name                    Size                    Bytes  Class

  heart_scale_inst      270x13                    28080  double array
  heart_scale_label     270x1                      2160  double array

where heart_scale_inst is a matrix containing 270 rows of
13-dimension feature vectors, and heart_scale_label is the binary
label (1 and -1) of the data. The data set contain 120 feature
vectors with label 1 and 150 feature vectors with label -1.

6) Scale the data to the range of [-1 1]. This step is important
such that no dimension of the feature vector dominates the
decision boundary just because of its larger scale.

matlab> [heart_scale_inst , scale_parameter] =
svm_scale_firsttime_newlibsvm(heart_scale_inst);

7) Split the data set into a training set and a test set. The
training set is used to select the penalty coefficient and the
Radial Basis Function (RBF) kernel parameter. The test set is used
to evaluate the test performance of the classifier. This demo is
for an RBF kernel SVM classification.

matlab> trainSet = heart_scale_inst(1:210,:);

matlab> testSet = heart_scale_inst(211:end,:);

matlab> trainLabel = heart_scale_label(1:210);

matlab> testLabel = heart_scale_label(211:end);

8) Select the penalty coefficient and the gamma of the RBF kernel
by 5-fold cross-validation. Once the parameters are selected,
train a SVM model. The parameters are selected by a grid search as
explained in:

http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf

matlab> [bestGamma, bestC, bestCVAccuracy] =
svm_learn_gridsearch_RBF_parameter_newlibsvm(trainSet,trainLabel,5);

matlab> model = svmtrain(trainLabel,trainSet,sprintf('-s 0 -t 2 -g
%f -c %f -b 1',bestGamma,bestC));

To understand the option string, please refer to the description
below (extracted from the README):

============================================================
-s svm_type : set type of SVM (default 0)
    0 -- C-SVC
    1 -- nu-SVC
    2 -- one-class SVM
    3 -- epsilon-SVR
    4 -- nu-SVR
-t kernel_type : set type of kernel function (default 2)
    0 -- linear: u'*v
    1 -- polynomial: (gamma*u'*v + coef0)^degree
    2 -- radial basis function: exp(-gamma*|u-v|^2)
    3 -- sigmoid: tanh(gamma*u'*v + coef0)
-d degree : set degree in kernel function (default 3)
-g gamma : set gamma in kernel function (default 1/k)
-r coef0 : set coef0 in kernel function (default 0)
-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
-m cachesize : set cache memory size in MB (default 40)
-e epsilon : set tolerance of termination criterion (default 0.001)
-h shrinking: whether to use the shrinking heuristics, 0 or 1 (default 1)
-b probability_estimates: whether to train an SVC or SVR model for probability estimates, 0 or 1 (default 0)
-wi weight: set the parameter C of class i to weight*C in C-SVC (default 1)
-v n: n-fold cross validation mode

The k in the -g option means the number of attributes in the input data.

option -v randomly splits the data into n parts and calculates cross
validation accuracy/mean squared error on them.
============================================================

9) evaluate the test performance of the trained classifier.

matlab> [predict_label, accuracy] = svmpredict(testLabel, testSet, model);

The accuracy is the correct classification rate.
please refer to the README for the description of the svmpredict function.

10) For the training of a multi-class SVM classifier, the users
just need to identify the classes with different integers. For
example, the labels for a 3-class data can be of [1 2 3].

11) Users are encouraged to read "A practical guide to SVM
classification" written by the creators of LIBSVM.

http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -