libsvm in matlab

 一.下载libsvm

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

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

 

二.配置编译器

打开 matlab,切换到C:\Program Files\MATLAB\R2011a\toolbox\libsvm-3.14\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 %(because the make.m file can auto-configure every thing sg)

>> 

%编译完成

 

系统就会生成svmtrain.mexw32svmpredict.mexw32libsvmread.mexw32libsvmwrite.mexw32等文件(对于 Matlab 7.1以下版本,生成的对应文件为svmtrain.dllsvmpredict.dll read_sparse.dll,没做测试),


% Set path (This is very important! sg)

然后可以在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这个结构体(其中保存了所有的支持向量及其系数),那么说明libsvmmatlab 之间的接口已经完全配置成功。

 

注意:

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\matlabREADME文件才是帮助文件。

但是输入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版本的(它们的区别在类标签)。

法1、下载matlab数据集(http://download.csdn.net/detail/abcjennifer/4215779)

法2、用libsvmread而非load,就是这里



%% attention you should first add the libsvm/matlab into the matlab path, otherwise you should remove the heart_scale file to libsvm/matlab.

% but I suggest you 'd better add the path, it is easy for your next matlab code. sgliu


[label_vector, instance_matrix] = libsvmread(‘C++版本数据集’); %得到类标签和属性矩阵,然后可以使用它们训练了model = svmtrain(label_vector, instance_matrix);


 %Details (sg)

 [label_vector, instance_matrix] =  libsvmread('heart_scale');
 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 = 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


7  some others

You should make the Makefile in libsvm fold, becuase there are some other files and function eg. svm-scale

Your data should take the scale change at first, for improving your performance. Normalize is very imporatant on image processing sg

你可能感兴趣的:(libsvm in matlab)