Matlab安装使用libsvm

防止别人重复犯一些错误,把网络上看到得东西总结一下,希望对某些人有些帮助。

20120703

一.下载libsvm

~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工具箱中的好用。

人生难免有挫折,但你是逃避不了的,一定要去面对它

Matlab安装使用libsvm

相关文章:

你感兴趣的文章:

标签云: