Libsvm使用说明(三)

  • Function: double svm_predict(const struct svm_model *model,
    const struct svm_node *x);

    这个函数为一个测试向量x在提供的一个模式下做分类或回归。
    对于分类模式,返回的是x被预测的类。
    对于回归模式,返回的是用该模式计算x的函数值。
    对于 one-class 模式,返回 +1 或 -1 。

  • Function: void svm_cross_validation(const struct svm_problem *prob,
    const struct svm_parameter *param, int nr_fold, double *target);

    这个函数执行交叉验证。数据被分成 nr_fold 折。在已设的参数下,按顺序用剩下的训练集的训练出的模 式来验证每一折。验证过程中预测的 labels (所有可能的实体)被储存在名为 target 数组中。
    svm_prob 的格式与 svm_train() 的格式一样。

  • Function: int svm_get_svm_type(const struct svm_model *model);

    这个函数提供模式的 svm_type 。可能的 svm_type 值在 svm.h 已定义。

  • Function: int svm_get_nr_class(const svm_model *model);

    对于分类模式,这个函数提供类的数目。对于回归或一个 one-class 模式,返回值为 2 。

  • Function: void svm_get_labels(const svm_model model, int label)

    对于一个分类模式,这个函数输出 labels 的名字,存进一个名为 label 的数组。
    对于回归和 one-class 模式,label 是未改变的。

  • Function: double svm_get_svr_probability(const struct svm_model *model);

    对于一个有概率信息的回归模式,这个函数输出一个值 sigma > 0。
    对于测试数据,我们认为这样一个概率模式:
    目标值 = 预测值 + z, z: Laplace
    分布 e^(-|z|/sigma)/(2sigma)

    如果模式不是用于 svr 或不包含所需的信息,返回 0 。

  • Function: double svm_predict_values(const svm_model *model,
    const svm_node x, double dec_values)

    这个函数给出一个在给定模式下测试向量x的决定值,并返回预测的 label(类)或者函数值(回归)。

    对于有 nr_class 个类的分类模式,这个函数在 dec_values 数组中提供 nr_class*(nr_class-1)/2 个决 定值,nr_class 可从 svm_get_nr_class 函数获得。
    顺序是 label[0] vs. label[1], …,label[0] vs. label[nr_class-1],
    label[1] vs. label[2], …,label[nr_class-2] vs. label[nr_class-1],
    label可由 svm_get_labels 函数得到。
    返回值是 x 被预测的类。注意当 nr_class = 1 时这个函数不给出任何决定值。

    对于回归模式,dec_values[0] 和返回值都是在该模式下计算的 x 的函数值。
    对于 one-class 模式,dec_values[0] 是 x 的决定值,返回值是 +1/-1 。

  • Function: double svm_predict_probability(const struct svm_model *model,
    const struct svm_node x, double prob_estimates);

    这个函数在有概率信息的模式下为测试向量 x 做分类或回归。

    对于一个有概率信息的分类模式,这个函数在 prob_estimates 数组中给出 nr_class 个概率估计。
    nr_class 可从 svm_get_nr_class 函数获得。返回有最大概率的类。
    对于回归或 one-class SVM,prob_estimates 数组未改变并且返回值与 svm_predict 一样。

  • Function: const char *svm_check_parameter(const struct svm_problem *prob,
    const struct svm_parameter *param);

    这个函数检查参数是否在可行的范围之内。这个函数应该被 svm_train() 和 svm_cross_validation()使用之前调用。如果参数可行,它返回 NULL ,否则返回一个错误信息。

  • Function: int svm_check_probability_model(const struct svm_model *model);

    这个函数检查模式是否包含做概率估计时所需的信息。如果有,返回 +1 。否则,返回 0 。
    这个函数应该在 svm_get_svr_probability 和 svm_predict_probability只用之前调用。

  • 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: void svm_free_model_content(struct svm_model *model_ptr);

    这个函数释放被模式结构中的条目使用的内存。

  • Function: void svm_free_and_destroy_model(struct svm_model **model_ptr_ptr);

    这个函数释放模式使用的内存并摧毁模式结构。
    等价于 svm_destroy_model ,version 3.0 以后的版本中被免去。

  • Function: void svm_destroy_param(struct svm_parameter *param);

    这个函数释放设定参数时候使用的内存。

  • Function: void svm_set_print_string_function(void (print_func)(const char ));

    用户可以用一个函数确定他们的输出格式。用 svm_set_print_string_function(NULL);
    向默认的标准输出中输出。

Java版本

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

预编译的 java 类文件‘libsvm.jar’和其源码文件在 java 文件夹下。使用下列命令来运行程序,

java -classpath libsvm.jar svm_train <arguments>
java -classpath libsvm.jar svm_predict <arguments>
java -classpath libsvm.jar svm_toy
java -classpath libsvm.jar svm_scale <arguments>

注意你需要 Java 1.5 (5.0) 或更高来运行它。

你也许需要添加 Java runtime library (像是 classes.zip) 到classpath中。
你也可能要增加 maximum Java heap size.

库的使用与C版本类似。有以下函数可用:

public class svm {
    public static final int LIBSVM_VERSION=321; 
    public static svm_model svm_train(svm_problem prob, svm_parameter param);
    public static void svm_cross_validation(svm_problem prob, svm_parameter param, int nr_fold, double[] target);
    public static int svm_get_svm_type(svm_model model);
    public static int svm_get_nr_class(svm_model model);
    public static void svm_get_labels(svm_model model, int[] label);
    public static void svm_get_sv_indices(svm_model model, int[] indices);
    public static int svm_get_nr_sv(svm_model model);
    public static double svm_get_svr_probability(svm_model model);
    public static double svm_predict_values(svm_model model, svm_node[] x, double[] dec_values);
    public static double svm_predict(svm_model model, svm_node[] x);
    public static double svm_predict_probability(svm_model model, svm_node[] x, double[] prob_estimates);
    public static void svm_save_model(String model_file_name, svm_model model) throws IOException
    public static svm_model svm_load_model(String model_file_name) throws IOException
    public static String svm_check_parameter(svm_problem prob, svm_parameter param);
    public static int svm_check_probability_model(svm_model model);
    public static void svm_set_print_string_function(svm_print_interface print_func);
}

库在”libsvm” 包中.
注意,在 Java 版本中,svm_node[] 并不以一个 index = -1 的节点结束。

用户可以指定输出格式用:

your_print_func = new svm_print_interface()
    { 
        public void print(String s)
        {
            // your own format
        }
    };
    svm.svm_set_print_string_function(your_print_func);

编译Windows二进制文件

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

Windows 二进制文件在‘windows’目录下。按以下步骤用 Visual C++ 编译它们:

1)打开DOS命令行 (或Visual Studio命令行) 切换到 libsvm目录.
若 VC++ 的环境变量还未设置, 键入

"C:\Program Files\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"

你可能不得不更改上面命令根据你自己VC++的版本或安装的位置。

2)键入

nmake -f Makefile.win clean all

3) (可选) 构建共享库 libsvm.dll, 键入

nmake -f Makefile.win lib

4) (可选) To build 32-bit windows binaries, you must
(1) Setup “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\vcvars32.bat” instead of vcvars64.bat
(2) Change CFLAGS in Makefile.win: /D _WIN64 to /D _WIN32

Another way is to build them from Visual C++ environment. See details in libsvm FAQ.

附加工具: Sub-sampling, Parameter Selection, Format checking,等

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

详见tools目录中的README.

MATLAB/OCTAVE接口

===============================
Please check the file README in the directory ‘matlab’.

Python接口

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

See the README file in python directory.

补充

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

If you find LIBSVM helpful, please cite it as

Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for support
vector machines. ACM Transactions on Intelligent Systems and
Technology, 2:27:1–27:27, 2011. Software available at
http://www.csie.ntu.edu.tw/~cjlin/libsvm

LIBSVM implementation document is available at
http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf

For any questions and comments, please email cjlin@csie.ntu.edu.tw

Acknowledgment:
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 their group members and users
for many helpful discussions and comments. They are listed in
http://www.csie.ntu.edu.tw/~cjlin/libsvm/acknowledgements

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
libsvm是一个常用的支持向量机(SVM)库,用于分类和回归任务。下面是一个简单的libsvm使用教程: 1. 安装libsvm:你可以从libsvm官方网站上下载最新版本的libsvm库,并按照指示进行安装。 2. 数据准备:libsvm使用特定的数据格式。对于分类任务,数据应该是一个文本文件,每一行代表一个样本,以空格分隔特征和特征值。例如: ``` label1 feature1:value1 feature2:value2 ... label2 feature1:value1 feature2:value2 ... ``` 对于回归任务,数据的格式与分类任务类似,但标签是连续的实数。 3. 数据加载:使用libsvm提供的API加载数据。在C++中,你可以使用`svm_problem`结构来存储训练数据。使用`svm_load_problem`函数从文件中加载数据,并将其转换为`svm_problem`结构。 4. 参数设置:选择合适的参数配置。libsvm提供了一系列参数,如SVM类型、核函数、惩罚系数等。你可以通过交叉验证或其他方法来选择最佳参数配置。 5. 模型训练:使用训练数据和参数配置来训练SVM模型。使用`svm_train`函数,将训练数据和参数作为输入,得到一个训练好的模型。 6. 模型预测:使用训练好的模型进行预测。对于分类任务,使用`svm_predict`函数,将测试数据作为输入,得到预测结果。对于回归任务,使用`svm_predict_values`函数,得到预测的连续值。 以上是libsvm的基本使用教程。你可以参考libsvm的文档和示例代码,进一步学习和使用libsvm库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值