libsvm在matlab中的使用

来源:互联网 发布:苹果系统办公软件 编辑:程序博客网 时间:2024/06/05 14:52

        之前花了很多时间才成功在matlab中使用libsvm库,今晚把过程整理记录一下。

        首先是下载libsvm库,官方下载地址:http://www.csie.ntu.edu.tw/~cjlin/libsvm/    在该网页Download LIBSVM部分点击红字加下划线的zip file或者tar.gz  即可下载

     (我下载的是libsvm-3.21版本,下载地址:http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm.cgi?+http://www.csie.ntu.edu.tw/~cjlin/libsvm+zip)

        下载并解压完成后,把文件夹添加到路径:我用的是matlab2015a版本,点击左上角home中的set path(低版本是File->set path),然后点击add with subfolder选择libsvm文件夹的路径点击save,close,完成(注意,不可以点Add Folder,因为要把libsvm和它的子文件都添加到路径才能正常使用,只添加libsvm文件夹的话将来使用会报错!)

        接下来,在matlab中编译。命令窗口输入mex -setup(注意横杠“-”之前有个空格)


点击 mex -setup C++ ,命令窗口会显示:

MEX configured to use 'Microsoft Visual C++ 2013 Professional' for C++ language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the
new API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.

把当前路径改成libsvm文件夹的路径(如第一张截图中路径),打开libsvm文件夹下的matlab文件夹中的make.m,运行。

现在就编译完成了,libsvm文件夹下的matlab文件夹中也出现了好几个.mexw64结尾的文件(如果你的电脑是32位,就是.mexw32)


最后就可以测试了。

命令窗口输入:

[label_vector, instance_matrix] = libsvmread('heart_scale');model = svmtrain(label_vector,instance_matrix);[predict_label,accuracy] = svmpredict(label_vector,instance_matrix,model);[predict_label, accuracy, dec_values] = svmpredict(label_vector,instance_matrix, model);  

【注意:】

1. 现在下载的新的libsvm自带的数据集没有matlab的。所以之前网上很多博客教程里写的用load heart_scale现在用是会报错的,解决方法有两种,一种是下载之前的heart_scale.mat数据集http://download.csdn.net/detail/abcjennifer/4215779,一种是改为

[label_vector, instance_matrix] = libsvmread('heart_scale');

用libsvmread的原因是现在自带的数据集不是matlab的,所以需要用libsvmread转换一下。

2. 我之前看过Rachel-Zhang的教程(http://blog.csdn.net/abcjennifer/article/details/7370177#),她是用
model = svmtrain(heart_scale_label,heart_scale_inst);  [predict_label,accuracy] = svmpredict(heart_scale_label,heart_scale_inst,model);
但是我用的时候报错:

>> model = svmtrain(heart_scale_label,heart_scale_inst);
Undefined function or variable 'heart_scale_label'.

应该是libsvm版本不同造成的存放数据的变量名不同,改为:

>> model = svmtrain(label_vector,instance_matrix); 

输出结果为
*
optimization finished, #iter = 162
nu = 0.431029
obj = -100.877288, rho = 0.424462
nSV = 132, nBSV = 107
Total nSV = 132


预测时,输入

>> [predict_label,accuracy] = svmpredict(label_vector,instance_matrix,model);

不能得出正确结果,accuracy变量为空,matlab命令窗口提示如下:
Usage: [predicted_label, accuracy, decision_values/prob_estimates] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
       [predicted_label] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
Parameters:
  model: SVM model structure from svmtrain.
  libsvm_options:
    -b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet
    -q : quiet mode (no outputs)
Returns:
  predicted_label: SVM prediction output vector.
  accuracy: a vector with accuracy, mean squared error, squared correlation coefficient.
  prob_estimates: If selected, probability estimate vector.

改为输入:

>> [predict_label, accuracy, dec_values] = svmpredict(label_vector,instance_matrix, model);  

即可正常输出结果:
Accuracy = 86.6667% (234/270) (classification)

或者输入:

[predicted_label] = svmpredict(label_vector,instance_matrix, model);  
也可以

安装过程也可参考网上的一个视频:http://v.youku.com/v_showMini/id_XMjc2NTY3MzYw_ft_131.html (该视频使用的matlab版本为matlab7.0),这个视频是我通过Rachel-Zhang大神的博文才看到的,mac上使用libsvm可以参考上面提到的Rachel-Zhang大神的博客,我没有mac所以没有试过。


最后,附上一些我觉得不错的学习libsvm的资料:

1.林智仁的SVM使用方法讲义 http://download.csdn.net/detail/xiahouzuoxin/5778941

(林智仁的讲义提到的一些方法在这篇博文中也有总结http://blog.csdn.net/xiahouzuoxin/article/details/9372805)

2.高人对libsvm的经典总结(全面至极)  http://blog.163.com/crazyzcs@126/blog/static/129742050201061192243911/

3.LibSVM学习详细说明http://blog.csdn.net/zy_zhengyang/article/details/45009431

0 0