各种教学视频或文档资料 +QQ:635992897

 

1.SVM训练

 

使用前请按照python

改变核函数需要在svm-train.exe执行命令行加
"-s svm_type : set type of SVM (default 0)\n"
        "       0 -- C-SVC             (multi-class classification)\n"
        "       1 -- nu-SVC            (multi-class classification)\n"
        "       2 -- one-class SVM\n"
        "       3 -- epsilon-SVR       (regression)\n"
        "       4 -- nu-SVR            (regression)\n"
        "-t kernel_type : set type of kernel function (default 2)\n"
        "       0 -- linear: u'*v\n"
        "       1 -- polynomial: (gamma*u'*v + coef0)^degree\n"
        "       2 -- radial basis function: exp(-gamma*|u-v|^2)\n"
        "       3 -- sigmoid: tanh(gamma*u'*v + coef0)\n"
        "       4 -- precomputed kernel (kernel values in training_set_file)\n"
        "-d degree : set degree in kernel function (default 3)\n"
        "-g gamma : set gamma in kernel function (default 1/num_features)\n"
        "-r coef0 : set coef0 in kernel function (default 0)\n"
        "-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n"
        "-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n"
        "-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n"
        "-m cachesize : set cache memory size in MB (default 100)\n"
        "-e epsilon : set tolerance of termination criterion (default 0.001)\n"
        "-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n"
        "-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n"
        "-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n"
        "-v n: n-fold cross validation mode\n"
        "-q : quiet mode (no outputs)\n"
 

第一步:打开easy.py,修改# example forwindows下的几个路径,同时修改grid.py路径。

 

 

修改cmd = '{0} {1} -svmtrain "{2}" -gnuplot "{3}" "{4}"'.format("D:\Anaconda2\python.exe",grid_py, svmtrain_exe, gnuplot_exe, scaled_file)

添加Python路径

第二步:运行cmd,进入控制台环境,定位到放置easy.py的目录d:/libsvm/program/tools。

第三步:输入命令:

d:/libsvm/python26/python easy.py heart_scale heart_test

 

heart_scale 是训练样本文件

heart_test  是测试样本文件

 

训练完成后得到模型文件

 

  1. 训练文件制作

    提取舌面分析算法,删除SVM预测部分svm.predict。将算法生产的特征向量输出到文件

    如下所示

    1:0.002  2:0.033:0.448

    在特征向量前增加分类标示

    1 1:0 2:0

    1 1:3 2:4

    1 1:5 2:9

    1 1:12 2:1

    1 1:8 2:7

    0 1:9 2:8

    0 1:6 2:12

    0 1:10 2:8

    0 1:8 2:5

    0 1:14 2:8

    增加住够多的特征向量即可用于训练。注意各分类的样本数量应该一致。

     

  2. 模型文件使用

    SVM训练结束后会生成range文件和model文件

     

    Range文件如下:

    x

    0.000000000000000 1.000000000000000

    1 0.000000000000000 14.00000000000000

    2 0.000000000000000 12.00000000000000

     

model文件内容如下:

svm_type c_svc

kernel_type rbf

gamma 0.5

nr_class 2

total_sv 9

rho -0.5061570424019811

label 1 0

nr_sv 4 5

SV

2.7686973549711875 1:0.214285714285714272:0.3333333333333333

5.0 1:0.35714285714285715 2:0.75

5.0 1:0.8571428571428571 2:0.08333333333333333

5.0 1:0.5714285714285714 2:0.5833333333333334

-5.0 1:0.6428571428571429 2:0.6666666666666666

-2.4351637665059895 1:0.42857142857142855 2:1.0

-5.0 1:0.7142857142857143 2:0.6666666666666666

-5.0 1:0.5714285714285714 2:0.4166666666666667

-0.3335335884651968 1:1.0 2:0.6666666666666666

 

将两个文件合并成

x

0.000000000000000 1.000000000000000

1 0.000000000000000 14.00000000000000

2 0.000000000000000 12.00000000000000

svm_type c_svc

kernel_type rbf

gamma 0.5

nr_class 2

total_sv 9

rho -0.5061570424019811

label 1 0

nr_sv 4 5

SV

2.7686973549711875 1:0.214285714285714272:0.3333333333333333

5.0 1:0.35714285714285715 2:0.75

5.0 1:0.8571428571428571 2:0.08333333333333333

5.0 1:0.5714285714285714 2:0.5833333333333334

-5.0 1:0.6428571428571429 2:0.6666666666666666

-2.4351637665059895 1:0.42857142857142855 2:1.0

-5.0 1:0.7142857142857143 2:0.6666666666666666

-5.0 1:0.5714285714285714 2:0.4166666666666667

-0.3335335884651968 1:1.0 2:0.6666666666666666

即可成为用于分析的模型文件

 

文件内容说明

svm_type c_svc % 训练所采用的 svm 类型,此处为 C- SVC

kernel_type rbf % 训练采用的核函数类型,此处为 RBF 核

gamma 0.047619 % 与操作参数设置中的 g 含义相同

nr_class 2 % 分类时的类别数,此处为两分类问题

total_sv 604 % 总共的支持向量个数

rho -0.337784 % 决策函数中的常数项 b

label 0 1 % 类别标签

nr_sv 314 290 % 各类别标签对应的支持向量个数

SV % 以下为支持向量

1 1:-0.963808 2:0.906788 ... 19:-0.197706 20:-0.928853 21:-1

1 1:-0.885128 2:0.768219 ... 19:-0.452573 20:-0.980591 21:-1

... ... ...

1 1:-0.847359 2:0.485921 ... 19:-0.541457 20:-0.989077 21:-1

% 对于分类问题,上面的支持向量的各列含义与训练数据集相同;对于回归问题,略有不同,与训练数据中的标签 label (即 y 值)所对应的位置在模型文件的支持向量中现在存放的是 Lagrange 系数 a 值,即为下面决策函数公式中的 a 值:

* *

1

() ( )( ( ) ( )) ( ) ( , )

( , )

k

i i i i i i

i i sv

i i

i sv

fx a a x x b a a k x x b

ak x x b

=

= - F F + = - +

= +

g

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐