Python调用Libsvm

来源:互联网 发布:金天鹅软件下载 编辑:程序博客网 时间:2024/06/05 20:41

最近想用svm做遥感影像的分类,经过一番搜索,发现台湾大学林智仁教授开发的libsvm是个极其不错的选择。首先,libsvm提供了对应多个语言版本的接口(C++/C#/python/C#+python/matlab/java等);其次,libsvm可以跨平台;最后,也是最重要的一点,libsvm允许用户根据所面对的问题的不同来修改参数以及选择核函数类型。具体参考请详见http://www.csie.ntu.edu.tw/~cjlin/和http://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html。但是,svm中包含有多种核函数类型(Linear、RBF、polynomial、sigmoid),选择一种正确的核函数对提高影像分类精度是至关重要的[1]。作为一个初学者,如何配置和成功调用libsvm中的函数是学习libsvm的第一步。以下步骤是自己参阅各大博客上的文章总结整理出来的。

1、下载三个软件,依次为Python(x,y)2.6.6,libsvm-3.20,gnuplot4.6,下载地址可以上百度搜索,libsvm的下载地址为http://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html。

2、将以上三个软件安装或者解压缩到C:\program files\目录下。

3、打开libsvm目录下tools文件夹中的grid.py文件,修改这个文件中所有关于pnuplot的安装路径。我的修改情况见图1

                                                                                                        图1 gnuplot路径修改

 

4、所有这些软件配置工作准备就绪之后,就可以开始测试有没有配置成功。打开命令窗口,输入Python命令(前提是在系统变量中设置了Python命令路径,这里不再赘述)。详细命令见图2。

                                     图2 libsvm初步调用(图中默认的核函数类型为RBF,gama系数默认为0.5,惩罚系数C为函数svm_train中指定的4)

5、到了第4步,初步成功地调用了libsvm,但是没有看到gnuplot发挥了什么作用。那么这里再介绍一种调用方式。为了充分地阐述问题,这里不再使用libsvm自带的样本数据,这里我们自己制作一份符合libsvm格式的数据。由于目的是要做遥感影像分类,这里首先就以采集到的遥感影像ROI样本作为例子,来制作一份符合libsvm格式的数据。

     5.1 首先需要简单说明下libsvm样本数据的格式。<lable_value> <index1>:<value1> <index2>:<value2>.....。(lable_value,index,value变量的值全部为数值型,lable_value表示样本的类型,在二分类其中,其值一般取-1和1或者0和1,当然取其他值也是可以的,只要能区分就行。index可以理解为遥感影像中的波段序列或者特征序列。value可以理解为对应的像元值或者特征值)。

     5.2 下载自带宏命令的FormatDatalibsvm.xls,宏命令见图3   ,下载地址见本博客。

       

                                            图3 宏命令      

       5.3将保存成文本文件的ROI数据采用Excel打开,编辑index值,然后复制粘贴到FormatDatalibsvm中。这里需要注意的是在FormatDatalibsvm中数据的排列顺序不能按照libsvm格式来排序。在FormatDatalibsvm.xls中的顺序应该为<index1>:<value1> <index2>:<value2>.......<label_value>。然后选择宏命令中的FormatDatalibsvm,单击执行就可以。

6、 有了自己制作的样本数据,就可以开始试试另外一种调用方法了。为了方便起见,首先将下图4所示的文件全部拷贝到一个文件中,其中FormatDatalibsvm.txt就是自己制作的数据。

 

                                                                   图4 文件拷贝

7、用NotePad++打开grid.py文件,cmd运行grid.py文件,输入如下代码所示的命令,运行结果见下图5。采用这种方法的特点是算法会进行不断的迭代,自动搜索RBF核函数中gama系数和惩罚系数C的最优值。

 

 

C:\Users\Administrator\Desktop\svm_test>grid.py FormatDatalibsvm.txt[local] 5 -7 57.0213 (best c=32.0, g=0.0078125, rate=57.0213)[local] -1 -7 45.7447 (best c=32.0, g=0.0078125, rate=57.0213)[local] 5 -1 23.8298 (best c=32.0, g=0.0078125, rate=57.0213)[local] -1 -1 22.3404 (best c=32.0, g=0.0078125, rate=57.0213)[local] 11 -7 56.5957 (best c=32.0, g=0.0078125, rate=57.0213)[local] 11 -1 23.8298 (best c=32.0, g=0.0078125, rate=57.0213)[local] 5 -13 65.9574 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] -1 -13 63.4043 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 11 -13 62.9787 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] -3 -7 33.1915 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] -3 -1 22.3404 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] -3 -13 58.2979 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 5 1 22.9787 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] -1 1 22.1277 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 11 1 22.9787 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] -3 1 22.1277 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 9 -7 56.5957 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 9 -1 23.8298 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 9 -13 64.8936 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 9 1 22.9787 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 5 -11 63.4043 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] -1 -11 65.1064 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 11 -11 58.5106 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] -3 -11 63.617 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 9 -11 58.5106 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 3 -7 57.4468 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 3 -1 23.8298 (best c=32.0, g=0.0001220703125, rate=65.9574)[local] 3 -13 66.383 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 3 1 22.9787 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 3 -11 65.1064 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 5 -5 37.6596 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -1 -5 30.2128 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 11 -5 37.6596 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -3 -5 26.8085 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 9 -5 37.6596 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 3 -5 37.8723 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 15 -7 56.5957 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 15 -1 23.8298 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 15 -13 61.4894 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 15 1 22.9787 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 15 -11 57.8723 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 15 -5 37.6596 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 5 -15 66.383 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -1 -15 60.8511 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 11 -15 63.4043 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -3 -15 55.3191 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 9 -15 65.7447 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 3 -15 64.6809 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 15 -15 64.4681 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -5 -7 32.3404 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -5 -1 22.3404 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -5 -13 54.8936 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -5 1 22.1277 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -5 -11 53.617 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -5 -5 26.8085 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -5 -15 37.6596 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 5 3 22.5532 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -1 3 22.5532 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 11 3 22.5532 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -3 3 22.5532 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 9 3 22.5532 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 3 3 22.5532 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 15 3 22.5532 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -5 3 22.5532 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 7 -7 56.8085 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 7 -1 23.8298 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 7 -13 64.8936 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 7 1 22.9787 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 7 -11 61.7021 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 7 -5 37.6596 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 7 -15 66.383 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 7 3 22.5532 (best c=8.0, g=0.0001220703125, rate=66.383)[local] 5 -9 60.0 (best c=8.0, g=0.0001220703125, rate=66.383)[local] -1 -9 67.0213 (best c=0.5, g=0.001953125, rate=67.0213)[local] 11 -9 57.4468 (best c=0.5, g=0.001953125, rate=67.0213)[local] -3 -9 55.9574 (best c=0.5, g=0.001953125, rate=67.0213)[local] 9 -9 57.4468 (best c=0.5, g=0.001953125, rate=67.0213)[local] 3 -9 62.1277 (best c=0.5, g=0.001953125, rate=67.0213)[local] 15 -9 57.4468 (best c=0.5, g=0.001953125, rate=67.0213)[local] -5 -9 38.5106 (best c=0.5, g=0.001953125, rate=67.0213)[local] 7 -9 59.1489 (best c=0.5, g=0.001953125, rate=67.0213)[local] 1 -7 58.2979 (best c=0.5, g=0.001953125, rate=67.0213)[local] 1 -1 23.8298 (best c=0.5, g=0.001953125, rate=67.0213)[local] 1 -13 65.9574 (best c=0.5, g=0.001953125, rate=67.0213)[local] 1 1 22.9787 (best c=0.5, g=0.001953125, rate=67.0213)[local] 1 -11 66.1702 (best c=0.5, g=0.001953125, rate=67.0213)[local] 1 -5 38.0851 (best c=0.5, g=0.001953125, rate=67.0213)[local] 1 -15 64.8936 (best c=0.5, g=0.001953125, rate=67.0213)[local] 1 3 22.5532 (best c=0.5, g=0.001953125, rate=67.0213)[local] 1 -9 65.1064 (best c=0.5, g=0.001953125, rate=67.0213)[local] 5 -3 28.5106 (best c=0.5, g=0.001953125, rate=67.0213)[local] -1 -3 23.4043 (best c=0.5, g=0.001953125, rate=67.0213)[local] 11 -3 28.5106 (best c=0.5, g=0.001953125, rate=67.0213)[local] -3 -3 23.1915 (best c=0.5, g=0.001953125, rate=67.0213)[local] 9 -3 28.5106 (best c=0.5, g=0.001953125, rate=67.0213)[local] 3 -3 28.5106 (best c=0.5, g=0.001953125, rate=67.0213)[local] 15 -3 28.5106 (best c=0.5, g=0.001953125, rate=67.0213)[local] -5 -3 23.1915 (best c=0.5, g=0.001953125, rate=67.0213)[local] 7 -3 28.5106 (best c=0.5, g=0.001953125, rate=67.0213)[local] 1 -3 28.7234 (best c=0.5, g=0.001953125, rate=67.0213)[local] 13 -7 56.5957 (best c=0.5, g=0.001953125, rate=67.0213)[local] 13 -1 23.8298 (best c=0.5, g=0.001953125, rate=67.0213)[local] 13 -13 61.2766 (best c=0.5, g=0.001953125, rate=67.0213)[local] 13 1 22.9787 (best c=0.5, g=0.001953125, rate=67.0213)[local] 13 -11 57.0213 (best c=0.5, g=0.001953125, rate=67.0213)[local] 13 -5 37.6596 (best c=0.5, g=0.001953125, rate=67.0213)[local] 13 -15 62.766 (best c=0.5, g=0.001953125, rate=67.0213)[local] 13 3 22.5532 (best c=0.5, g=0.001953125, rate=67.0213)[local] 13 -9 57.4468 (best c=0.5, g=0.001953125, rate=67.0213)[local] 13 -3 28.5106 (best c=0.5, g=0.001953125, rate=67.0213)0.5 0.001953125 67.0213C:\Users\Administrator\Desktop\svm_test>svm-train.exe FormatDatalibsvm.txt default_predict.model.*optimization finished, #iter = 293nu = 0.823529obj = -78.211476, rho = 0.234660nSV = 169, nBSV = 70.*.*optimization finished, #iter = 400nu = 0.823529obj = -80.498903, rho = 0.300005nSV = 170, nBSV = 70.*.*optimization finished, #iter = 266nu = 0.923077obj = -64.284626, rho = -0.142821nSV = 130, nBSV = 60.*.*optimization finished, #iter = 266nu = 0.923077obj = -64.284627, rho = -0.142821nSV = 130, nBSV = 60.*optimization finished, #iter = 189nu = 0.933333obj = -74.359342, rho = 0.124407nSV = 150, nBSV = 70.*optimization finished, #iter = 347nu = 0.935081obj = -96.720022, rho = 0.066375nSV = 199, nBSV = 77.*optimization finished, #iter = 303nu = 0.750000obj = -70.319713, rho = -0.344001nSV = 159, nBSV = 60.*optimization finished, #iter = 303nu = 0.750000obj = -70.319715, rho = -0.344001nSV = 159, nBSV = 60.*optimization finished, #iter = 324nu = 0.888889obj = -84.976735, rho = -0.125326nSV = 179, nBSV = 80.*.*optimization finished, #iter = 375nu = 0.750000obj = -71.999965, rho = -0.399999nSV = 160, nBSV = 60.*.*optimization finished, #iter = 372nu = 0.750000obj = -71.999967, rho = -0.399999nSV = 160, nBSV = 60.*.*optimization finished, #iter = 396nu = 0.888889obj = -87.964154, rho = -0.200003nSV = 180, nBSV = 80*optimization finished, #iter = 60nu = 1.000000obj = -60.000754, rho = 0.000000nSV = 120, nBSV = 98*.*optimization finished, #iter = 188nu = 0.857143obj = -67.479948, rho = 0.249331nSV = 140, nBSV = 60*.*optimization finished, #iter = 188nu = 0.857143obj = -67.479950, rho = 0.249331nSV = 140, nBSV = 60Total nSV = 469C:\Users\Administrator\Desktop\svm_test>svm-predict.exe FormatDatalibsvm.txt default_predict.model after_FormatTrain.modelAccuracy = 100% (470/470) (classification)C:\Users\Administrator\Desktop\svm_test>


8、特别说明:由于学习时间有限,还没有对libsvm进行深入的学习。本文参考了大量其他博客文章。下一篇是要真正解决如何将libsvm运用于遥感影像的分类。

Reference:

[1] 谭琨, 杜培军. 基于再生核Hibert空间小波核函数支持向量机的高光谱遥感影像分类[J]. 测绘学报, 2011, 40(2): 142-147

[2] Chih-Wei Hsu,Chih-Chung Chang,Chih-Jen Lin.A Practical Guide to Support Vector Classication.2003

参考博客:http://blog.chinaunix.net/uid-22414998-id-4175203.html

                  http://www.cnblogs.com/petali/archive/2008/11/06/1328454.html

                  http://blog.csdn.net/timidsmile/article/details/9316743

                  http://www.matlabsky.com/thread-12649-1-1.html

                  http://blog.sina.com.cn/s/blog_4d7c97a00101a10w.html

                  http://blog.csdn.net/passball/article/details/7661887/

                  http://blog.csdn.net/v_july_v/article/details/7624837/

0 0
原创粉丝点击