现在写一下在matlab中从安装libsvm到使用。
1,安装
系统版本:redhat 6.0
libsvm的版本:3.1. 下载地址:http://www.csie.ntu.edu.tw/~cjlin/libsvm/
matlab版本:2010b.
1) 首先将matlab当前目录设为libsvm\matlab所在目录,并且保证Makefile文件中MATLABDIR路径为matlab安装所在路径,例如:
MATLABDIR ?= /RLNK/PACKAGE/MATLAB.2010b
2)在matlab命令窗口中输入:-mex -setup
出现
Options files control which compiler to use, the compiler and link command
options, and the runtime libraries to link against.
Using the 'mex -setup' command selects an options file that is
placed in ~/.matlab/R2010b and used by default for 'mex'. An options
file in the current working directory or specified on the command line
overrides the default options file in ~/.matlab/R2010b.
To override the default options file, use the 'mex -f' command
(see 'mex -help' for more information).
The options files available for mex are:
1: /rlnk/package/matlab.2010b.64/bin/gccopts.sh :
Template Options file for building gcc MEX-files
2: /rlnk/package/matlab.2010b.64/bin/mexopts.sh :
Template Options file for building MEX-files via the system ANSI compiler
0: Exit with no changes
选择了 1
Enter the number of the compiler (0-2):
1
确定
Overwrite /dcs/pg10/xingjie/.matlab/R2010b/mexopts.sh ([y]/n)?
y
/rlnk/package/matlab.2010b.64/bin/gccopts.sh is being copied to
/dcs/pg10/xingjie/.matlab/R2010b/mexopts.sh
**************************************************************************
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/support/solutions/en/data/1-5C27B9/?solution=1-5C27B9
Building with the -largeArrayDims option enables the new API.
**************************************************************************
3)输入make,编译。
这里我发现输入make总是编译报错,输入 !make 就能正常通过编译。估计是compiler选择的问题。只能从工程角度表示可以解决问题,但不知到是什么原理,对Linux不熟,求懂行者解答。
另外在网上看到有人也遇到类似问题,用 !make 无法解决,但是将.c文件中所有的注释符号由 // 换成 /* */ ,并且吧Makefile中所有 .obj 后缀 换成 .o 后缀就可以了。同样不懂,求解答。
编译成功后就会发现目录下多了:
libsvmread.mexa64
libsvmwrite.mexa64
svmpredict.mexa64
svmtrain.mexa64
这就说明编译成功了。
2,使用
这里使用libsvm自带的例子测试一下。
load heart_scale.mat
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); % test the training data
可以看到结果:
Accuracy = 86.6667% (234/270) (classification)