LIBSVM高级进阶

来源:互联网 发布:程序员的职业修养 pdf 编辑:程序博客网 时间:2024/05/20 08:43

SVM怎样才能得到较好的结果

1)对数据归一化处理(simple scale)

2)应用RBF kernel

3)用交叉验证(cross-validation)和grid-search得到最优的C和g

采用交叉验证选择最佳参数C和g:不同的参数(最常用的是C和g)条件下训练出不同的SVM。

4)训练数据并预测

参见“LIBSVM入门解读”篇libsvm工具使用说明部分。


libsvm可以实现基于SVM的分类和回归

(分类与回归大致一样,重点只说明参数寻优过程不同的地方)

1、分类

在分类过程中主要是用tools文件夹下grid.py来选取SVM最好的参数。

grid.py是一种用于RBF核函数的C-SVM分类的参数选择程序。用户只需给定参数的一个范围,grid.py采用交叉验证的方法计算每种参数组合的准确度来找到最好的参数。

Usage: grid.py [-log2c begin,end,step] [-log2g begin,end,step] [-v fold]

       [-svmtrain pathname] [-gnuplot pathname] [-out pathname] [-png pathname]

       [additional parameters for svm-train] dataset

The program conducts v-fold cross validation using parameter C (and gamma)= 2^begin, 2^(begin+step), ..., 2^end.

示例:
python grid.py -log2c -10,10,1 -log2g 10,-10,-1 trainset.txt

2、回归

gridregression.py来搜索最优参数C和g

示例:

python.exe gridregression.py -svmtrain H:\SVM\libsvm-2.81\windows\svmtrain.exe -gnuplot C:\gp373w32\pgnuplot.exe -log2c -10,10,1 -log2g -10,10,1 -log2p -10,10,1 -v 10 -s 3 -t 2 H:\SVM\libsvm-2.81\windows\feature.scaled > gridregression_feature.parameter

注意:-svmtrain是给出svmtrain.exe所在路径,一定要是完整的全路径 
-gnuplot是给出pgnuplot.exe所在路径。这里要用pgnuplot.exe这种命令行形式的,不要用wgnupl32.exe,这个是图形界面的。 
-log2c是给出参数c的范围和步长 
-log2g是给出参数g的范围和步长 
-log2p是给出参数p的范围和步长 
上面三个参数可以用默认范围和步长 
-s选择SVM类型,也是只能选3或者4 
-t是选择核函数 
-v 10 将训练数据分成10份做交叉验证。默认为5 
最后给出归一化后训练数据的全路径 
搜索最优参数的过程写入文件gridregression_feature.parameter(注意别少了这个>符号啊) 
打开gridregression_feature.parameter,下面最后一行分别为c,g,p,mse。其中mse没有用,其实这个值越小越好。

备注:对于grid.py和gridregression.py这两个文件用到了pythongnuplot这两个软件来解决svm核心的问题——参数选择。

解压缩安装后,python打开(不能双击,而要右键选择“Edit with IDLE”)打开grid.py文件,修改svmtrain_exegnuplot_exe的路径。 (这里面有一个是对非win32的,可以不用改,只改# example for windows下的就可以了)如下图所示:



easy.py文件对样本文件从参数优选到文件预测都一次完成,因此,其对grid.pysvm-trainsvm-scalesvm-predict都进行了调用(当然还有必须的pythongnuplot)。因此,运行easy.py需要保证这些文件的路径都要正确。当然还需要样本文件和预测文件,这里样本文件还是用heart_scale,预测文件我们复制一份然后改名heart_test,下面说一下使用方法:(1)打开easy.py,修改# example for windows下的几个路径;(2)进入dos环境输入命令: python easy.py heart_scale heat_test 即可。

可参看博客:http://blog.csdn.net/flydreamgg/article/details/4470477


详细补充说明:

如果预测的准确率太低,如何提高一下?使用python目录下的grid.py进行模型选择以找到比较好的参数。

grid.py是一种用于RBF核函数的C-SVM分类的参数选择程序。用户只需给定参数的一个范围,grid.py采用交叉验证的方法计算每种参数组合的准确度来找到最好的参数。

Usage: grid.py [-log2c begin,end,step] [-log2g begin,end,step] [-v fold]

       [-svmtrain pathname] [-gnuplot pathname] [-out pathname] [-png pathname]

       [additional parameters for svm-train] dataset

The program conducts v-fold cross validation using parameter C (and gamma)= 2^begin, 2^(begin+step), ..., 2^end.

首先sudo apt-get install gnuplot

然后编译C++版本的LibSVM,生成svm-train二进制可执行文件。

cd  /path/to/libsvm-3.1

make

举个例子就都明白了:python grid.py -log2c -5,5,1 -log2g -4,0,1 -v 5 -svmtrain /home/orisun/develop/libsvm-3.1/svm-train -m 500 traincev_svmfmt_part1

-m 500是使用svm_train时可以使用的参数。

最后输出两个文件:dataset.png绘出了交叉验证精度的轮廓图dataset.out对于每一组log2(c)和log2(gamma)对应的CV精度值

如果训练时间过长,你可能需要:

1.指定更大有cache size。(-m)

2.使用更宽松的stopping tolerance。(-e)

当使用一个很大有-e时,你可能需要检查一下-h 0 (no shrinking) or -h 1 (shrinking)哪个更快。

3.如果上面的方法还不行就需要裁剪训练集。使用tools目录下的subset.py来随机获得训练集的一个子集。

Usage: subset.py [options] dataset number [output1] [output2]

This script selects a subset of the given data set.

options:

-s method : method of selection (default 0)

     0 -- stratified selection (classification only)

     1 -- random selection

output1 : the subset (optional)

output2 : the rest of data (optional)

If output1 is omitted, the subset will be printed on the screen.

当迭代次数很高时使用shrinking是有帮助的,而当使用一个很大的-e时,迭代次数会减少,最好把shrinking关掉。


1 0
原创粉丝点击