MATLAB 安装使用libsvm详细步骤

来源:互联网 发布:渲染软件 编辑:程序博客网 时间:2024/05/24 07:01

分二个步骤:
- 配置编译
- 使用

———————–配置编译——————————

  1. 下载libsvm
    http://www.csie.ntu.edu.tw/~cjlin/libsvm/
    我的matlab版本是R2012b,我的libsvm版本3.21

  2. 解压至指定目录
    将libsvm解压至D:\MATLAB\R2012b\toolbox下,你也可以解压至你喜欢的地方。

  3. 设置路径
    可以选择2种方式:

*addpath D:\MATLAB\R2012b\toolbox\libsvm-3.21

*在set-path里面设置

这里写图片描述

下载下来的libsvm包里有svm的一些源文件,没有可执行的exe文件,所以,必须先将svmtrain等源文件编译为matlab可以使用的dll等文件。接下来就是编译的过程。
4. 编译
首先进入当前目录为 D:\MATLAB\R2012b\toolbox\libsvm-3.21\matlab

mex –setup

Welcome to mex -setup. This utility will help you set up a default compiler. For a list of supported compilers, see
http://www.mathworks.com/support/compilers/R2012b/win32.html Please
choose your compiler for building MEX-files: Would you like mex to
locate installed compilers [y]/n? y
Select a compiler:
[1]Lcc-win32 C 2.4.1 in D:\matlab\sys\lcc
[2] Microsoft Visual C++ 2010 in d:\VS2010\install
[0] None
Please verify your choices:
Compiler: Microsoft Visual C++ 2010
Are these correct [y]/n? y
直到出现Done,那么恭喜你,编译成功了

之后就可以看到这4个文件了
这里写图片描述

————————-开始使用了———————-

在libsvm中有个heart_scale,是libsvm 的数据集
但需注意:libsvm 3.12中提供的是c++版本的数据集heart_scale,这里需要加载matlab版本的数据集。
这两个数据集有什么不同呢?

C++版本的数据集里面如果某一个样本的某一个特征为0,这个特征可以不写,但是在matlab版本中,必须要写出来。否则的话,会报这样一个错误:


这里写图片描述

此时你可以选择下载matlab型的数据集,给一个链接。
http://download.csdn.net/detail/boruoshui/4881338
然后读取数据集

clc;  clear;  [heart_scale_label,heart_scale_inst]=libsvmread('heart_scale');  model = svmtrain(heart_scale_label,heart_scale_inst)   [predict_label,accuracy,dec_values] = svmpredict(heart_scale_label,heart_scale_inst,model) 

完成该步骤后发现Workspace中出现了heart_scale_inst 和 heart_scale_label,说明正确。

0 0