找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3574|回复: 0

SVM学习

[复制链接]

33

主题

1

回帖

561

积分

用户组: 大·技术宅

UID
580
精华
26
威望
28 点
宅币
341 个
贡献
0 次
宅之契约
0 份
在线时间
8 小时
注册时间
2014-12-8
发表于 2014-12-30 15:58:36 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
本帖最后由 0x55AA 于 2014-12-30 16:04 编辑

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的值因为不知道,所以可以填写任何一个数字,这个不可以为空,记住一定要随便写个数字把位置给占了。
        向量缩放
  1. Usage: svm-scale [options] data_filename
  2. options:
  3. -l lower : x scaling lower limit (default -1)
  4. -u upper : x scaling upper limit (default +1)
  5. -y y_lower y_upper : y scaling limits (default: no y scaling)
  6. -s save_filename : save scaling parameters to save_filename
  7. -r restore_filename : restore scaling parameters from restore_filename

  8. > svm-scale -l -1 -u 1 -s range train > train.scale
  9. > svm-scale -r range test > test.scale
复制代码
其中 –s 参数是把训练向量的缩放参数放进一个文本里面,那么在对测试样本进行缩放时就可以直接调用这个参数文件,而不是再重复填写跟训练时一样的参数了。
        参数选定
        参数选择就是选出一个最合适的参数用于训练向量,以及后来的预测向量,当然参数选择只是针对RBF核函数.libSVM里面提供了用于参数选择的工具,也就是grid.py,因为是python语言写的,所以要安装python的开发环境,python的下载地址是:
http://www.python.org/getit/,用法如下:
  1. grid.py [grid_options] [svm_options] dataset
  2. -log2c {begin,end,step | "null"} : set the range of c (default -5,15,2)
  3.     begin,end,step -- c_range = 2^{begin,...,begin+k*step,...,end}
  4.     "null"         -- do not grid with c
  5. -log2g {begin,end,step | "null"} : set the range of g (default 3,-15,-2)
  6.     begin,end,step -- g_range = 2^{begin,...,begin+k*step,...,end}
  7.     "null"         -- do not grid with g
  8. -v n : n-fold cross validation (default 5)
  9. -svmtrain pathname : set svm executable path and name
  10. -gnuplot {pathname | "null"} :
  11.     pathname -- set gnuplot executable path and name
  12.     "null"   -- do not plot
  13. -out {pathname | "null"} : (default dataset.out)
  14.     pathname -- set output file path and name
  15.     "null"   -- do not output file
  16. -png pathname : set graphic output file path and name (default dataset.png)
  17. -resume [pathname] : resume the grid task using an existing output file (default pathname is dataset.out)
  18.     Use this option only if some parameters have been checked for the SAME data.

  19. svm_options : additional options for svm-train
复制代码
其实找出最佳参数就是一个一个的试,如果你不想用这个工具的话,就一个一个试了。
        训练样本
  1. options:
  2. -s svm_type : set type of SVM (default 0)
  3.         0 -- C-SVC                (multi-class classification)
  4.         1 -- nu-SVC                (multi-class classification)
  5.         2 -- one-class SVM       
  6.         3 -- epsilon-SVR        (regression)
  7.         4 -- nu-SVR                (regression)
  8. -t kernel_type : set type of kernel function (default 2)
  9.         0 -- linear: u'*v
  10.         1 -- polynomial: (gamma*u'*v + coef0)^degree
  11.         2 -- radial basis function: exp(-gamma*|u-v|^2)
  12.         3 -- sigmoid: tanh(gamma*u'*v + coef0)
  13.         4 -- precomputed kernel (kernel values in training_set_file)
  14. -d degree : set degree in kernel function (default 3)
  15. -g gamma : set gamma in kernel function (default 1/num_features)
  16. -r coef0 : set coef0 in kernel function (default 0)
  17. -c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
  18. -n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
  19. -p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
  20. -m cachesize : set cache memory size in MB (default 100)
  21. -e epsilon : set tolerance of termination criterion (default 0.001)
  22. -h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)
  23. -b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)
  24. -wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)
  25. -v n: n-fold cross validation mode
  26. -q : quiet mode (no outputs)
复制代码
可以看出参数真的很多,但是我们如果用的RBF核函数的话,通常只需要填写-c,-g参数就可以了。如下:
  1. ./svm-train -c 2 -g 2 svmguide1.scale
复制代码
预测样本
  1. Usage: svm-predict [options] test_file model_file output_file
  2. options:
  3. -b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 is supported
复制代码
示例如下:
  1. ./svm-predict svmguide3.t.scale svmguide3.scale.model svmguide3.t.predict
复制代码
附上我在程序中的例子:
        这个是用于训练样本自己的测试
  1. system(".\\windows\\svm-scale.exe -l 0 -u 1 zTrain.txt>zTrain.scale");    //对样本进行缩放,结果保存在train.scale
  2. system(".\\windows\\svm-train.exe -c 8.0 -g 0.0078125 -h 0 zTrain.scale");  //对样本进行训练,结果保存在train.scale.model
  3. system(".\\windows\\svm-predict.exe zTrain.scale zTrain.scale.model goal.txt"); //对训练样本自身样本预测,结果保存在goal.txt
复制代码
这个是用于预测测试样本的值
  1. system(".\\windows\\svm-scale.exe -l 0 -u 1 zTest.txt>zTest.scale");
  2. system(".\\windows\\svm-predict.exe zTest.scale zTrain.scale.model goal.txt");
复制代码
回复

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-26 19:45 , Processed in 0.042537 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表