我的libsvm文档java 文档

上传人:第*** 文档编号:34242197 上传时间:2018-02-22 格式:DOC 页数:23 大小:448.50KB
返回 下载 相关 举报
我的libsvm文档java 文档_第1页
第1页 / 共23页
我的libsvm文档java 文档_第2页
第2页 / 共23页
我的libsvm文档java 文档_第3页
第3页 / 共23页
我的libsvm文档java 文档_第4页
第4页 / 共23页
我的libsvm文档java 文档_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《我的libsvm文档java 文档》由会员分享,可在线阅读,更多相关《我的libsvm文档java 文档(23页珍藏版)》请在金锄头文库上搜索。

1、LibSVM(JAVA)二次开发接口调用及源码更改的文档浙江大学协调服务研究所文档整理:陈伟 chenweishaoxing#下载 libsvm方法:google libsvm 找到官网下载:http:/www.csie.ntu.edu.tw/cjlin/libsvm/ ,其中图片中椭圆的解压文档下载下来 libsvm 工具包有几个版本的,其中 python 的最经典,用的人比较多,还支持 matlab,C+等等。我们用的 java 版的,就到解压开的 java 文件夹中!java 文件夹导入到 eclipse 工程中创建一个 java 工程,把上图的源码复制到 eclipse 中,如同所示在

2、工程下创建一个文件夹,里面存放训练测试用的数据首次调用的 Demo 举例在 java 的工程中创建一个属于自己的包,然后写一个 mian 类。如图ComMain.javapackage com.endual.paper.main;import java.io.IOException;import service.svm_predict;import service.svm_train;public class ComMain public static void main(String args) throws IOException String arg = trainfiletrain1.

3、txt, /存放 SVM 训练模型用的数据的路径trainfilemodel_r.txt; /存放 SVM 通过训练数据训 / /练出来的模型的路径String parg=trainfiletrain2.txt, /这个是存放测试数据trainfilemodel_r.txt, /调用的是训练以后的模型trainfileout_r.txt; /生成的结果的文件的路径System.out.println(.SVM 运行开始.); /创建一个训练对象svm_train t = new svm_train(); /创建一个预测或者分类的对象svm_predict p= new svm_predict(

4、); t.main(arg); /调用p.main(parg); /调用6.运行工程就可以看到了结果了Libsvm 二次开发的首先要熟悉调用接口的源码你一定会有疑问:SVM 的参数怎么设置,cross-validation 怎么用。那么我们首先来说明一个问题,交叉验证在一般情况下要自己开发自己写。Libsvm 内置了交叉验证,但是如果我希望用同交叉验证的数据用决策树来做,怎么办,显然Libsvm 并没有保存交叉验证的数据。=我已经将注释写在了源码中。Svm_train 类的文档说明package service;import libsvm.*;import java.io.*;import j

5、ava.util.*;public class svm_train private svm_parameter param; / set by parse_command_lineprivate svm_problem prob; / set by read_problemprivate svm_model model;private String input_file_name; / set by parse_command_lineprivate String model_file_name; / set by parse_command_lineprivate String error_

6、msg;private int cross_validation;private int nr_fold;private static svm_print_interface svm_print_null = new svm_print_interface()public void print(String s) ;private static void exit_with_help()System.out.print(Usage: svm_train options training_set_file model_filen+options:n+-s svm_type : set type

7、of SVM (default 0)n+ 0 - C-SVCn+ 1 - nu-SVCn+ 2 - one-class SVMn+ 3 - epsilon-SVRn+ 4 - nu-SVRn+-t kernel_type : set type of kernel function (default 2)n+ 0 - linear: u*vn+ 1 - polynomial: (gamma*u*v + coef0)degreen+ 2 - radial basis function: exp(-gamma*|u-v|2)n+ 3 - sigmoid: tanh(gamma*u*v + coef0

8、)n+ 4 - precomputed kernel (kernel values in training_set_file)n+-d degree : set degree in kernel function (default 3)n+-g gamma : set gamma in kernel function (default 1/num_features)n+-r coef0 : set coef0 in kernel function (default 0)n+-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-S

9、VR (default 1)n+-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)n+-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)n+-m cachesize : set cache memory size in MB (default 100)n+-e epsilon : set tolerance of termination criterion (default 0.001)n+

10、-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)n+-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)n+-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)n+-v n : n-fold cross validation mod

11、en+-q : quiet mode (no outputs)n);System.exit(1);private void do_cross_validation()int i;int total_correct = 0;double total_error = 0;double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;double target = new doubleprob.l;svm.svm_cross_validation(prob,param,nr_fold,target);if(param.svm_type = sv

12、m_parameter.EPSILON_SVR | param.svm_type = svm_parameter.NU_SVR)for(i=0;i= argv.length 这个应该是先用 i 再加 1,那么下面的操作的时候就是 i = i + 1 了(i=5)if(+i=argv.length)exit_with_help(); /如果执行了第二个 if,那么会执行到这里了。这里的 i = 5switch(argvi-1.charAt(1) /用到的字符串仍然是 argv5-1=argv4,解析的是第 2 个字符。case s: /设置 svm 的类型param.svm_type = ato

13、i(argvi); /这个赋值就是将 argv5,赋值过去了break;case t: /设置 svm 的核函数类型param.kernel_type = atoi(argvi);break;case d: /设置 svm 参数 d 的大小,用于多项式核函数param.degree = atoi(argvi);break;case g: /赋值 gamma 的param.gamma = atof(argvi);break;case r: /赋值 coef0 的值param.coef0 = atof(argvi);break;case n: /赋值 n 的值param.nu = atof(arg

14、vi);break;case m: /赋值缓存的值param.cache_size = atof(argvi);break;case c: /赋值的是惩罚因子的大小param.C = atof(argvi);break;case e: /赋值的 eps 的值param.eps = atof(argvi);break;case p: /赋值*我不想写下去了,因为在实际的应用中,我还没用用到下面的参数。抱歉。param.p = atof(argvi);break;case h: /param.shrinking = atoi(argvi);break;case b: /要不要打印出分类的准确率的值

15、param.probability = atoi(argvi);break;case q:print_func = svm_print_null;i-;break;case v: /设置的交叉验证的值cross_validation = 1; /开启交叉验证nr_fold = atoi(argvi);if(nr_fold = 2n);exit_with_help();break;case w:+param.nr_weight;int old = param.weight_label;param.weight_label = new intparam.nr_weight;System.arraycopy(old,0,param.weight_label,0,param.nr_weight-1);double old = param.weight;param.weight =

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号