一.下载libsvmhtml
http://www.csie.ntu.edu.tw/~cjlin/libsvm/web
在libsvm的网站上下载 libsvm-3.12.zip文件,解压后放在任意目录下,最好放在MATLAB工具箱中,好比 C:\Program Files\MATLAB\R2011a\toolbox\libsvm-3.12下。windows
二.配置编译器函数
打开 matlab,切换到C:\Program Files\MATLAB\R2011a\toolbox\libsvm-3.12\matlab目录下,键入如下命令:工具
mex –setup测试
出现提示语句网站
Please choose your compiler for building MEX-files:ui
Would you like mex to locate installed compilers [y]/n?n %此次是选择编译器,输入n,选择自定义的编译器this
出现如下选项(因电脑而异)加密
Select a compiler:
[1] Intel C++ 11.1 (with Microsoft Visual C++ 2008 SP1 linker)
[2] Intel Visual Fortran 11.1 (with Microsoft Visual C++ 2008 SP1 linker)
[3] Intel Visual Fortran 11.1 (with Microsoft Visual C++ 2008 Shell linker)
[4] Lcc-win32 C 2.4.1
[5] Microsoft Visual C++ 6.0
[6] Microsoft Visual C++ 2005 SP1
[7] Microsoft Visual C++ 2008 SP1
[8] Microsoft Visual C++ 2010
[9] Microsoft Visual C++ 2010 Express
[10] Open WATCOM C++
[0] None
Compiler: 8%能够用其余的,出现如下提示语句
Your machine has a Microsoft Visual C++ 2010 compiler located at
C:\Program Files\Microsoft Visual Studio 10.0. Do you want to use this compiler [y]/n?
编译器默认路径,确认正确输入y,更改路径,输入n
输入y出现再次确认
Please verify your choices:
Compiler: Microsoft Visual C++ 2010
Location: C:\Program Files\Microsoft Visual Studio 10.0
Are these correct [y]/n? y
编译器配置完成
Trying to update options file: C:\Documents and Settings\zhangduokun\Application Data\MathWorks\MATLAB\R2011a\mexopts.bat
From template: C:\PROGRA~1\MATLAB\R2011a\bin\win32\mexopts\msvc100opts.bat
Done . . .
三.编译
输入命令
>> make
>>
%编译完成
系统就会生成svmtrain.mexw32,svmpredict.mexw32,libsvmread.mexw32和libsvmwrite.mexw32等文件(对于 Matlab 7.1如下上版本,生成的对应文件为svmtrain.dll,svmpredict.dll和 read_sparse.dll,没作测试),而后能够在matlab的菜单 File->Set Path->add with subfolders(可直接用Add Folder)里,把C:\Program Files\MATLAB\R2011a\toolbox\libsvm-3.12\matlab目录添加进去,这样之后在任何目录下均可以调用 libsvm的函数了。
四.测试
为了检验 libsvm和 matlab之间的接口是否已经配置完成,能够在 matlab下执行如下命令:
>>load heart_scale
完成该步骤后发现Workspace中出现了heart_scale_inst和 heart_scale_label,说明正确
>>model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07');
>> [predict_label, accuracy, dec_values] = svmpredict(heart_scale_label, heart_scale_inst, model); %
Accuracy = 86.6667% (234/270) (classification)% done
若是运行正常并生成了model这个结构体(其中保存了全部的支持向量及其系数),那么说明 libsvm和matlab 之间的接口已经彻底配置成功。
注意:
1. matlab自带了C编译器Lcc-win32C,可是libsvm原始版本是C++实现的,所以须要C++的编译器来编译,这就是不适用matlab默认编译器而选择其余C++编译器的缘由。
matlab支持的编译器也是有限的,能够查看不一样版本matlab支持的编译器列表
2. 若是matlab版本过低,如matlab 7.0是不能用VS做为编译器的,只能用VC++ 6.0
3. .mexw32 文件是通过加密的,打开是乱码,函数自己没有帮助。
例如输入 help svmpredict会出现报错: svmpredict not found
工具箱libsvm-3.12\matlab中README文件才是帮助文件。
可是输入help svmtrain会出现帮助信息,其实出现的是系统自带的svmtrain函数,没有libsvm工具箱中的好用。
4.在新版本libsvm3.12中,文件夹libsvm-3.12\windows中已经有编译好的程序,能够直接使用,只须要把libsvm-3.12\windows添加到matlab路径中便可,不须要编译的过程。固然最好仍是本身编译一遍,由于编译环境不一样会致使一些不可预估的小问题,本身编译的过程是可控的。
5. 测试用数据集,libsvm官网上提供了不少数据集
测试使用的heart_scale数据集是C++版本的(类标签 1:第一个属性 2:第二个属性…),能够用libsvmread来转换为matlab版本的(它们的区别在类标签)。
[label_vector, instance_matrix] = libsvmread(‘C++版本数据集’); %获得类标签和属性矩阵,而后可使用它们训练了model = svmtrain(label_vector, instance_matrix);
>> load heart_scale
>> model = svmtrain(heart_scale_label,heart_scale_inst);
*
optimization finished, #iter = 162
nu = 0.431029
obj = -100.877288, rho = 0.424462
nSV = 132, nBSV = 107
Total nSV = 132
>> [predict_label,accuracy] = svmpredict(heart_scale_label,heart_scale_inst,model);
Accuracy = 86.6667% (234/270) (classification)
6.参考资料
libsvm库下载:http://www.csie.ntu.edu.tw/~cjlin/libsvm/
视频:http://v.youku.com/v_showMini/id_XMjc2NTY3MzYw_ft_131.html(有小问题,等下会提到)
详解:http://www.matlabsky.com/thread-11925-1-1.html