Libsvm 三个主要函数介绍

Libsvm是一款高效、易于使用的SVM分类与回归软件,支持多种SVM模型及内核函数,并提供了自动模型选择工具。本文档详细介绍了Libsvm的安装、数据格式、主要函数用法、模型训练流程及参数设置等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Libsvm 是一个关于svm分类和回归的简单易用高效的软件.他能解决C-SVM 分类, nu-SVM分类, one-class-SVM, epsilon-SVM 回归, and nu-SVM回归.同时还为C-SVM 分类提供了一个自动模型选择工具.该文档解释了如何使用libsvm.

 

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

上可获得Libsvm .

使用前请先阅读版权文件.

 

内容目录

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

-快速开始

-安装和数据格式

-svm-train 用法

-svm-predict 用法

-svm-scale 用法

-实用的提示

-例子

-核函数的预先计算

-库的用法

-java版本

-建立windows二进制文件

-附加工具:子采样,参数选择,格式检查,等

-matlab/octave 接口

-Python接口

-附加信息

 

快速开始

==========

 

函数和结构体在头文件svm.h中申明.你需要在c\c++源文件中包含svm.h,还有链接svm.cpp.可以在svm-train.c' 和`svm-predict.c'文件中查看如何使用的例子.我们定义了LIBSVM_VERSION,这样你就能检查版本号.l

 

在你分类测试数据之前,你需要构建一个SVM模型来训练数据.模型能被保存在一个文件中以备后来使用.一旦一个SVM模型是有效的,你就能用来分类新的数据.

 

函数: struct svm_model *svm_train(const structsvm_problem *prob,const struct svm_parameter *param);

该函数返回一个SVM模型,根据给予的训练数据和参数.

结构体 svm_problem 描述问题:

struct svm_problem

      {

           int l;

           double *y;

           struct svm_node **x;

      };

参数l 表示训练样本的数目,y 是一个包含目标值的数组(所属的类标签).x 是一个指针数组,每个指针指向一个训练向量(svm_node).

例如,我们有如下的数据:

    LABEL      ATTR1    ATTR2    ATTR3    ATTR4    ATTR5

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

      1                0   0.1       0.2       0   0

      2                0   0.1       0.3     -1.2       0

      1                0.4       0   0   0   0

      2                0   0.1       0   1.4       0.5

      3              -0.1      -0.2        0.1       1.1       0.1

 

 那么svm_problem的成员是:

 

    l = 5

 

    y -> 1 2 1 2 3

 

    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 表示向量的结束. 注意索引必须是按照递增的顺序.

 

结构体 svm_parameter描述了SVM模型的参数:

structsvm_parameter

       {

              int svm_type;

              int kernel_type;

              int 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 eps;   /* stopping criteria 迭代终止的标准,默认为0.00001 in nu-SVC,0.001 in others*/

              double C;      /* for C_SVC, EPSILON_SVR, and NU_SVR  惩罚系数*/

              int nr_weight;            /* for C_SVC */

              int *weight_label;      /* for C_SVC */

              double* weight;         /* for C_SVC */

              double nu;    /* for NU_SVC, ONE_CLASS, and NU_SVR */

              double p;      /* for EPSILON_SVR */

              int shrinking;       /* use the shrinking heuristics */

              int probability; /* do probabilityestimates */

       };

svm_type  可以是如下选择:

C_SVC:         C-SVM classification

    NU_SVC:       nu-SVMclassification

    ONE_CLASS:           one-class-SVM

    EPSILON_SVR:  epsilon-SVM regression

    NU_SVR:       nu-SVMregression

 

kernel_type可以是如下选择:

LINEAR: u'*v

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

    RBF:    exp(-gamma*|u-v|^2)

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

    PRECOMPUTED: kernel values intraining_set_file

 

注意:如果你一直在使用svm_train得到的模型svm_model,你不能释放svm_problem所占用的内存,因为svm_model包含指向svm_problem的指针.

 

注意: 为了避免错误的参数,你应该使用svm_check_parameter()来检查参数在调用svm_train()之前.

 

  struct svm_model 存储了训练步骤里产生的模型.

    不建议直接的访问结构体内的数据,程序员可以使用接口函数得到想要的值.

 

      struct svm_model

       {

              struct svm_parameter param;       /* parameter */

              intnr_class;         /* number of classes, =2 in regression/one class svm */

              int l;                     /* total #SV */

              struct svm_node **SV;             /* SVs (SV[l]) */

              double **sv_coef;      /* coefficients for SVs in decisionfunctions (sv_coef[k-1][l]) 表示每个支持向量对应的关系 */

              double *rho;        /* constants in decision functions(rho[k*(k-1)/2]) */

              double *probA;          /* pairwise probability information */

              double *probB;

 

              /* for classification only */

 

              int *label;            /* label of each class (label[k]) */

              int *nSV;              /* number of SVs for each class (nSV[k]) */

                                   /* nSV[0] +nSV[1] + ... + nSV[k-1] = l */

              /* XXX */

              int free_sv;          /* 1 if svm_model is created bysvm_load_model*/

                                   /* 0 ifsvm_model is created by svm_train */

       };

 

    param 表示过去得到的模型参数.

 

    nr_class 表示类的数目.如果是2表示回归和 one-class SVM.

 

    l 是支持向量的数目. SV and sv_coef 分别是支持向量和相应的系数. 假定有

    k 个类. 对于j类中的数据, 相应的sv_coef 包含 (k-1) y*alpha 向量,

    alpha's 是下面两类问题的解:

    1 vs j, 2 vs j, ..., j-1 vs j, j vs j+1, jvs j+2, ..., j vs k

    而且 y=1 是对于前 j-1 个向量, while y=-1 对于剩下的k-j 个向量. 比如这里有4个类, sv_coef 和 SV 像这样:

 

        +-+-+-+--------------------+

        |1|1|1|                    |

        |v|v|v| SVs from class 1  |

        |2|3|4|                    |

        +-+-+-+--------------------+

        |1|2|2|                    |

        |v|v|v| SVs from class 2  |

        |2|3|4|                    |

        +-+-+-+--------------------+

        |1|2|3|                    |

        |v|v|v| SVs from class 3  |

        |3|3|4|                    |

        +-+-+-+--------------------+

        |1|2|3|                    |

        |v|v|v| SVs from class 4  |

        |4|4|4|                    |

        +-+-+-+--------------------+

 

    See svm_train() for an example of assigningvalues to sv_coef.

 

    rho is the bias term (-b). probA and probBare parameters used in

    probability outputs. If there are kclasses, there are k*(k-1)/2

    binary problems as well as rho, probA, andprobB values. They are

    aligned in the order of binary problems:

    1 vs2, 1 vs 3, ..., 1 vs k, 2 vs 3, ..., 2 vs k, ..., k-1 vs k.

 

    label contains labels in the training data.

 

    nSV is the number of support vectors ineach class.

 

    free_sv is a flag used to determine whetherthe space of SV should

    be released in free_model_content(structsvm_model*) and

    free_and_destroy_model(struct svm_model**).If the model is

    generated by svm_train(), then SV points todata in svm_problem

    and should not be removed. For example,free_sv is 0 if svm_model

    iscreated by svm_train, but is 0 if created by svm_load_model.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值