[转]LIBSVM详细使用方法

Libsvm 是一个简单、方便使用和普遍适用高效率的软件,它是针对SVM分类的。这个文档将解释如何使用libsvm

Libsvm 在http://www.csie.ntu.edu.tw/~cjlin/libsvm 可以得到。在你使用libsvm前请阅读COPYRIGHT文件。

安装

============

在 Unix系统中,`make'类是用来建立`svm-train' `svm-classify' 程序的。不用看它们的使用说明,去运行它们。

在其它的系统中,使用pre-built binariesWindows binaries 在子目录`windows'下)或者求助`Makefile'创建它们,这很容易。

训练和测试数据文件的格式是:

<label> <index1>:<value1> <index2>:<value2> ...

.

.

.

<label>(标签)的值是+1或-1,<index>是一个从1开始的整型值,<value>是一个实数。

在这个打包文件里有一个示例训练数据文件(heart_scale) type`./svm-train heart_scale'和它的程序来读训练数据文件,然后输出它的模型文件`heart_scale.model',这时你就可以type`./svm-classify heart_scale heart_scale.model output'看到训练数据的分类速度。`output'文件包含了它的决策函数的输出属性。

 

在这个打包文件里还有其它的一些有用的程序。

scale:

这是个工具,用来归一化输入的数据文件,Type `make scale'来创建它。

svm-toy:

这是一个简单的图形接口,用来显示SVM在平面上如何分割数据。

      

你可以在窗口的底端输入选项,选项的语法规则与`svm-train'相同。

      

在各自的目录下创建类`make'

      

你需要Qt图书馆来创建Qt版本。

       (你可以从http://www.trolltech.com下载)

你需要GTK+图书馆来创建GTK版本。

       (你可以从http://www.gtk.org下载)

       我们用Visual C++来创建窗口视图。

pre-built Windows binaries windows 子目录下。

 

`svm-train' 的使用方法

=================

 

用法: svm-train [选项] training_set_file [model_file]

选项:

-c cost : 设置约束违反的代价CC是个惩罚函数,是个常数)

-t kernel_type : 设置核函数的类型(缺省值 2

        0 --线性核函数

1 --多项式核函数

2 -- 径向基核函数

3 -- sigmoid核函数

-d degree :设置核函数的度

-g gamma :在核函数中设置gamma

-r coef0 :在多项式/sigmoid 核函数中设置coef0

-m cachesize :MB中设置cache内存大小

-e epsilon : 设置终止条件的容许误差(缺省值为0.001

 

Library 的使用

=============

 

这些函数和结构在头文件`svm.h'中已经声明了。

你需要#include "svm.h" 在你的C/C++源文件中,然后把你的程序链接到`svm.cpp',这时你就能看到`svm-train.c' `svm-classify.c',来显示如何使用它们。

 

在分类测试数据之前,你需要用训练数据构建一个SVM模型(`svm_model')。这个模型被保存在一个文件里方便以后使用。一旦一个SVM模型创建好了,你就可以使用它来分类新的数据。

 

- Function: struct svm_model *svm_train(const struct svm_problem *prob,

                                   const struct svm_parameter *param);

 

这个函数根据你给定的训练数据和参数,创建和返回一个SVM模型。                                                                                 

      

                                                                                                                                                                                                                                                                                                                                                                                       

    struct svm_problem describes the problem:

      

       struct svm_problem

       {

              int l;

              signed char *y;

              struct svm_node **x;

       };

`l'为训练数据的个数,`y'是一个包含标签(+1/-1)的数组,`x' 是数组的指针,每一个指针都分别代表(svm_node数组)一个训练向量。                                  

例如,有如下的一个训练数据:

 

    LABEL    ATTR1    ATTR2    ATTR3    ATTR4    ATTR5

    ----- ----- ----- ----- ----- -----

      1           0    0.1        0.2        0    0

     -1           0    0.1        0.3      -1.2         0

      1           0.4        0    0    0    0

      1           0    0.1        0    1.4        0.5

     -1         -0.1       -0.2         0.1        1.1        0.1  

这时svm_problem 的各部分分别代表:                                     

l = 5

 

    y -> 1 -1 1 1 -1

 

    x -> [ ] -> (2,0.1) (3,0.2) (-1,?)

        [ ] -> (2,0.1) (3,0.3) (4,-1.2) (-1,?)

        [ ] -> (1,0.4) (-1,?)

        [ ] -> (2,0.1) (4,1.4) (5,0.5) (-1,?)

        [ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (-1,?)

(index,value)储存在结构`svm_node':

struct svm_node

       {

              int index;

              double value;

       };

 

index = -1 表明一个向量的结束。

struct svm_parameter describes the parameters of an SVM model:

 

       struct svm_parameter

       {

              int kernel_type;

              double degree;  // for poly

              double gamma;   // for poly/rbf/sigmoid

              double coef0;   // for poly/sigmoid

 

              // these are for training only

              double cache_size; // in MB

              double C;

              double eps;

       };

kernel_type can be one of LINEAR, POLY, RBF, SIGMOID.(几种核函数的类型)

 

    LINEAR: u'*v (线性)

    POLY:     (gamma*u'*v + coef0)^degree (多项式)

    RBF:       exp(-gamma*|u-v|^2) (径向基函数)

    SIGMOID:     tanh(gamma*u'*v + coef0) (SIGMOID函数)

cache_size 为核内存的大小,单位为MB

    C是惩罚函数的值,为一常数. (我们通常使用 1 1000)

    Eps是终止条件的容错误差 (我们通常使用 0.001)

 

    *注意* 因为 svm_model 包含指针指向 svm_problem, 如果你仍然使用由svm_train()创建的svm_model ,你可以使用svm_problem 不释放内存。(这里翻译的不好) 

- Function: int svm_save_model(const char *model_file_name,

                            const struct svm_model *model);

 

这个函数将一个模型保存到一个文件里,如果成功的话就返回0,如果不成功就返回-1.

  - Function: struct svm_model *svm_load_model(const char *model_file_name);

 

  这个函数从一个文件里读出一种模式,然后返回一个指针,如果找不到模式,就返回空指针.

   - Function: double svm_classify(const struct svm_model *model,

                            const struct svm_node *x);

 

这个函数使用一个模式来分类一个文本向量x,返回决策函数的值(正用+1表示,负用 -1表示)      

    - Function: void svm_destroy_model(struct svm_model *model);

 

这个函数用一个模式来释放内存.

 

其它的一些信息

============

 

Chih-Chung Chang and Chih-Jen Lin

Libsvm: Introduction and Benchmarks

http://www.csie.ntu.edu.tw/~cjlin/papers/q2.ps.gz

 

声明:

This work was supported in part by the National Science

Council of Taiwan via the grant NSC 89-2213-E-002-013.

The authors thank Chih-Wei Hsu and  Jen-Hao Lee

for many helpful discussions and comments.                                                                                                                                                                                               

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值