Matlab安装使用libsvm

一.下载libsvm

http://www.csie.ntu.edu.tw/~cjlin/libsvm/

在libsvm的网站上下载 libsvm-3.12.zip文件,解压后放在任意目录下,最好放在MATLAB工具箱中,比如 C:\Program Files\MATLAB\R2011a\toolbox\libsvm-3.12下。

二.配置编译器

打开 matlab,切换到C:\Program Files\MATLAB\R2011a\toolbox\libsvm-3.12\matlab目录下,键入以下命令:

mex –setup

出现提示语句

Please choose your compiler for building MEX-files:

Would you like mex to locate installed compilers [y]/n?n %这次是选择编译器,输入n,选择自定义的编译器

出现以下选项(因电脑而异)

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