libSVM使用总结

SVM的原理以后再说,现在先说说台湾大学林智仁老师的libsvm的用法,打心眼里敬佩这些大牛,能够无私的把自己的研究成果做成简单易用的工具让很多人都能够使用。下面就进入正题,讲讲SVM的使用过程。

输入向量转换成libSVM的向量格式:

在libsvm中向量的格式是:

类别号 1:xx 2:xx 3:xx ………

需要注意的是,向量的索引必须是按照升序排列,索引对应的值为0的可以不写,例如svm的向量一共有10种属性(向量的维度是10维),那么我就可以这样写相关的向量,如下所示:

1 1:1 2:0.3 3:0.7 7:0.9 10:0.5

其中1表示分类的类别是1 ,1:1表示索引是1 的对应的值是1 ,中间索引为4,5,6,8的没有写因为其对应的值是0,当然你也可以写,不过写了也是多余的,浪费空间和时间。

对数据进行缩放处理

当然你可以不去缩放数据,但是不去缩放数据很可能会对最终的结果产生非常恶劣的影响。只所以缩放数据是因为有一下几点:

1 避免属性在大范围内的数据会把属性在小范围的数据给干掉

2 减少在数值计算时的困难

通过建议的数据缩放范围是[-1,1]或者[0,1]

核函数选择

核函数建议采用RBF类型的核函数,因为它包含着线性的特性,同时相比多项式核函数,它的超参数少,计算方便。

交叉验证和网格搜索

RBF核函数有两个重要的参数C,γ。交叉这一步就是找到一个最合适的参数,是SVM的预测性能最佳。原理就是把训练样本分成V份,每一次都是V-1份样本用作训练,剩下的1份用作测试。这样就可以防止数据的数据的过度拟合的问题。Grid.py是libsvm中采用交叉验证的方法来获取最佳的C,γ。

获取C,γ后就要用这两个参数去训练样本,获得最终的支持向量

对测试样本进行预测,前提是测试样本要进行和训练样本一样规则的缩放。

讲完注意事项就要讲讲使用的语法了:

向量格式化

<label><index1>:<value1> <index2>:<value2> ...

Label 表示类别,index表示索引,索引的取值范围是1-n。当然在预测文件中,label的值因为不知道,所以可以填写任何一个数字,这个不可以为空,记住一定要随便写个数字把位置给占了。

向量缩放

Usage: svm-scale [options]data_filename

options:

-l lower : x scaling lower limit(default -1)

-u upper : x scaling upper limit(default +1)

-y y_lower y_upper : y scaling limits(default: no y scaling)

-s save_filename : save scalingparameters to save_filename

-r restore_filename : restore scalingparameters from restore_filename

 

> svm-scale -l -1 -u 1 -s rangetrain > train.scale

> svm-scale -r range test >test.scale

其中 –s参数是把训练向量的缩放参数放进一个文本里面,那么在对测试样本进行缩放时就可以直接调用这个参数文件,而不是再重复填写跟训练时一样的参数了。

参数选定

参数选择就是选出一个最合适的参数用于训练向量,以及后来的预测向量,当然参数选择只是针对RBF核函数.libSVM里面提供了用于参数选择的工具,也就是grid.py,因为是python语言写的,所以要安装python的开发环境,python的下载地址是:

http://www.python.org/getit/,用法如下:

grid.py [grid_options] [svm_options]dataset

-log2c {begin,end,step |"null"} : set the range of c (default -5,15,2)

   begin,end,step -- c_range = 2^{begin,...,begin+k*step,...,end}

   "null"         -- do notgrid with c

-log2g {begin,end,step |"null"} : set the range of g (default 3,-15,-2)

   begin,end,step -- g_range =2^{begin,...,begin+k*step,...,end}

   "null"         -- do notgrid with g

-v n : n-fold cross validation (default5)

-svmtrain pathname : set svm executablepath and name

-gnuplot {pathname | "null"}:

   pathname -- set gnuplot executable path and name

   "null"   -- do not plot

-out {pathname | "null"} :(default dataset.out)

   pathname -- set output file path and name

   "null"   -- do notoutput file

-png pathname : set graphic output filepath and name (default dataset.png)

-resume [pathname] : resume the gridtask using an existing output file (default pathname is dataset.out)

   Use this option only if some parameters have been checked for the SAMEdata.

 

svm_options : additional options forsvm-train

其实找出最佳参数就是一个一个的试,如果你不想用这个工具的话,就一个一个试了。

训练样本

options:

-s svm_type : set type of SVM (default0)

       0-- C-SVC            (multi-classclassification)

       1-- nu-SVC           (multi-classclassification)

       2-- one-class SVM      

       3-- epsilon-SVR    (regression)

       4-- nu-SVR           (regression)

-t kernel_type : set type of kernelfunction (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)

       4-- precomputed kernel (kernel values in training_set_file)

-d degree : set degree in kernelfunction (default 3)

-g gamma : set gamma in kernel function(default 1/num_features)

-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 lossfunction of epsilon-SVR (default 0.1)

-m cachesize : set cache memory size inMB (default 100)

-e epsilon : set tolerance oftermination criterion (default 0.001)

-h shrinking : whether to use theshrinking heuristics, 0 or 1 (default 1)

-b probability_estimates : whether totrain a SVC or SVR model for probability estimates, 0 or 1 (default 0)

-wi weight : set the parameter C ofclass i to weight*C, for C-SVC (default 1)

-v n: n-fold cross validation mode

-q : quiet mode (no outputs)

可以看出参数真的很多,但是我们如果用的RBF核函数的话,通常只需要填写-c,-g参数就可以了。如下:

./svm-train -c 2 -g 2 svmguide1.scale

预测样本

Usage: svm-predict [options] test_filemodel_file output_file

options:

-b probability_estimates: whether topredict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 issupported

示例如下:

./svm-predict svmguide3.t.scalesvmguide3.scale.model svmguide3.t.predict

附上我在程序中的例子:

这个是用于训练样本自己的测试

system(".\\windows\\svm-scale.exe -l 0 -u 1zTrain.txt>zTrain.scale");    //对?样¨´本À?进?行D缩?放¤?,ê?结¨¢果?保À¡ê存ä?在¨²train.scale

system(".\\windows\\svm-train.exe -c 8.0 -g0.0078125 -h 0 zTrain.scale");  //对?样¨´本À?进?行D训¦Ì练¢¡¤,ê?结¨¢果?保À¡ê存ä?在¨²train.scale.model

system(".\\windows\\svm-predict.exe zTrain.scalezTrain.scale.model goal.txt");//对?训¦Ì练¢¡¤样¨´本À?自Á?身¦¨ª样¨´本À?预¡è测a,ê?结¨¢果?保À¡ê存ä?在¨²goal.txt

这个是用于预测测试样本的值

system(".\\windows\\svm-scale.exe -l 0 -u 1zTest.txt>zTest.scale");

system(".\\windows\\svm-predict.exe zTest.scalezTrain.scale.model goal.txt");

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值