开发语言:JAVA

liblinear版本:liblinear-1.94.jar (下载地址:

1.下载 liblinear-1.94.jar,导入工程

在工程上右键---->Properties----->选中Java Build Path----->选中Libraries标签----->点击Add External JARs。

找到需要添加的jar包,确定即可。

2.创建LibLinear类 (类名自选)

代码如下:

1 packageliblinear;2

3 importjava.io.File;4 importjava.io.IOException;5 importjava.util.ArrayList;6 importjava.util.List;7

8 importde.bwaldvogel.liblinear.Feature;9 importde.bwaldvogel.liblinear.FeatureNode;10 importde.bwaldvogel.liblinear.Linear;11 importde.bwaldvogel.liblinear.Model;12 importde.bwaldvogel.liblinear.Parameter;13 importde.bwaldvogel.liblinear.Problem;14 importde.bwaldvogel.liblinear.SolverType;15

16 public classLibLinear{17 public static void main(String[] args) throwsException {18 //loading train data

19 Feature[][] featureMatrix = new Feature[5][];20 Feature[] featureMatrix1 = { new FeatureNode(2, 0.1), new FeatureNode(3, 0.2) };21 Feature[] featureMatrix2 = { new FeatureNode(2, 0.1), new FeatureNode(3, 0.3), new FeatureNode(4, -1.2)};22 Feature[] featureMatrix3 = { new FeatureNode(1, 0.4) };23 Feature[] featureMatrix4 = { new FeatureNode(2, 0.1), new FeatureNode(4, 1.4), new FeatureNode(5, 0.5) };24 Feature[] featureMatrix5 = { new FeatureNode(1, -0.1), new FeatureNode(2, -0.2), new FeatureNode(3, 0.1), new FeatureNode(4, -1.1), new FeatureNode(5, 0.1) };25 featureMatrix[0] =featureMatrix1;26 featureMatrix[1] =featureMatrix2;27 featureMatrix[2] =featureMatrix3;28 featureMatrix[3] =featureMatrix4;29 featureMatrix[4] =featureMatrix5;30 //loading target value

31 double[] targetValue = {1,-1,1,-1,0};32

33 Problem problem = newProblem();34 problem.l = 5; //number of training examples:训练样本数

35 problem.n = 5; //number of features:特征维数

36 problem.x = featureMatrix; //feature nodes:特征数据

37 problem.y = targetValue; //target values:类别

38

39 SolverType solver = SolverType.L2R_LR; //-s 0

40 double C = 1.0; //cost of constraints violation

41 double eps = 0.01; //stopping criteria

42

43 Parameter parameter = newParameter(solver, C, eps);44 Model model =Linear.train(problem, parameter);45 File modelFile = new File("model");46 model.save(modelFile);47 //load model or use it directly

48 model =Model.load(modelFile);49

50 Feature[] testNode = { new FeatureNode(1, 0.4), new FeatureNode(3, 0.3) };//test node51 double prediction =Linear.predict(model, testNode);52 System.out.print("classification result: "+prediction);53 }54 }

运行后得到testNode的分类结果:

3.参数说明

1. SolverType是solver的类型,可以是如下一种:

分类器:

L2R_LR:L2-regularized logistic regression (primal)

L2R_L2LOSS_SVC_DUAL:L2-regularized L2-loss support vector classification (dual)

L2R_L2LOSS_SVC:L2-regularized L2-loss support vector classification (primal)

L2R_L1LOSS_SVC_DUAL:L2-regularized L1-loss support vector classification (dual)

MCSVM_CS:supportvector classification by Crammer and Singer

L1R_L2LOSS_SVC:L1-regularized L2-loss support vector classification

L1R_LR:L1-regularized logistic regression

L2R_LR_DUAL:L2-regularized logistic regression (dual)

回归模型:

L2R_L2LOSS_SVR:L2-regularized L2-loss support vector regression (primal)

L2R_L2LOSS_SVR_DUAL:L2-regularized L2-loss support vector regression (dual)

L2R_L1LOSS_SVR_DUAL:L2-regularized L1-loss support vector regression (dual)

2. C是约束violation的代价参数 (默认为1)

3. eps是迭代停止条件的容忍度tolerance

本程序采用的训练样本如下(5个训练样本,5维特征):

label

feature1

feature2

feature3

feature4

feature5

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

0

-0.1

-0.2

0.1

1.1

0.1

测试样本为testNode变量:(0.4,0,0.3,0,0)

本文为原创博客,若转载请注明出处。

liblinear java_liblinear参数及使用方法(原创)相关推荐

  1. php的strpos不支持数字,php使用strpos判断字符串中数字类型子字符串出错的解决方法 原创...

    php使用strpos判断字符串中数字类型子字符串出错的解决方法 原创 这里有新鲜出炉的 PHP 面向对象编程,程序狗速度看过来! PHP 开源脚本语言 PHP(外文名: Hypertext Prep ...

  2. java get请求 参数_HttpServletRequest获取GET请求参数5种方法

    首页 > Java Web > SpringMvc应用 > HttpServletRequest HttpServletRequest获取GET请求参数5种方法 HttpServle ...

  3. java unlimited_具有无限参数的Java方法(Java method with unlimited arguments)

    具有无限参数的Java方法(Java method with unlimited arguments) Spring框架使用方法,您可以根据需要传递尽可能多的参数. 我想写一个函数,也可以采取无限量的 ...

  4. tomcat7修改内存 win_详解Windows下调整Tomcat启动参数的实现方法

    Windows下调整Tomcat启动参数 Tomcat默认可以使用的内存为128MB,对于访问数比较多.比较吃内存的应用来说,这是不够的.我们可以通过调整Java虚拟机的启动参数来增加Tomcat可使 ...

  5. 新浪微博开放平台API中page参数的使用方法

    新浪微博开放平台API中page参数的使用方法 在做项目的时候,有时候要调用新浪微博开放平台的API.说实话,新浪微博的API功能很强大,但是API文档写的不是很清楚. 我在做项目的时候,调用了很多A ...

  6. android优雅私有方法注释,带有注释参数的私有方法的Android java.lang.VerifyError

    我有一个非常简单的项目可以编译,但是无法在Emulator上启动.问题在于这种方法: private void bar(@Some String a) {} // java.lang.VerifyEr ...

  7. .net mvc actionresult 返回字符串_072-SpringMVC后端控制器接收参数、处理器方法返回值类型、拦截器、文件上传下载...

    1. 注解式开发之annotation-driven解释 (1) mvc注解驱动在哪个文件中配置? 在springmvc中配置 (2) 配置mvc注解驱动使用哪个标签? 2. 注解式开发之视图解析器 ...

  8. PHP中使用RabiitMQ---各项参数的使用方法

    RabbitMQ在PHP使用,我在这里对RabbitMQ的各项方法和参数进行了一些梳理,有不足的地方还望各位大神指点.想要使用rabbitMQ消息队列,首先需要安装 php_amqp.dll 扩展 和 ...

  9. java 传入参数_Java 中方法参数的传递

    java中方法参数传递看似有两种,值传递和引用传递.而实际上都是值传递. 所谓值传递,传递的是变量的副本就是说,在将变量a作为参数传进方法中时,方法中接收到的是a的副本,在方法中对参数的修改实际上修改 ...

最新文章

  1. 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
  2. JSP/Servlet 工作原理
  3. 【Linux 内核 内存管理】RCU 机制 ① ( RCU 机制简介 | RCU 机制的优势与弊端 | RCU 机制的链表应用场景 )
  4. Android APK方式换肤实现原理
  5. Little Sub and Counting
  6. Html跨页面调用函数,一个cshtml如何在另一个cshtml中调用一个函数?
  7. python中sorted函数的用法_Python3 中sorted() 函数的用法
  8. Java常见面试题:对象的访问定位的两种方式
  9. python怎么绘制坐标图像_如何使用给定的坐标和python opencv在图像中绘制点?
  10. paip.提升用户体验---论文本编辑器的色彩方案
  11. 修改硬盘固件的木马 探索方程式(EQUATION)组织的攻击组件
  12. 主流微波雷达感应语音播报雷达感应照明灯LD602模块
  13. python中的Pickle文件和npy文件
  14. 接口测试彻底弄懂Session、Cookie、Token的区别及联系hold住面试官--hold住了开3万,hold不住开3K!
  15. android屏幕亮度自动调节解析,基于人体视觉特性的Android手机屏幕亮度自适应调节算法研究...
  16. 基于并行思想的简单算法优化(最值,排序)
  17. java笔迹比对_[笔迹]java-其他
  18. 远程访问的类型 使用的协议和身份验证方法介绍
  19. android sim卡工具,手机sim卡工具包老是弹出来怎么办?sim卡工具包不断弹出删除方法...
  20. 华文慕课操作系统陈向群第三章课后习题解析

热门文章

  1. audio unit 混音
  2. 手机应该放哪里 如何把危害的程度降到最低
  3. win7怎么把计算机删除文件,windows7文件夹删除不了需要权限怎么办_win7删除电脑文件需要权限解决方法-win7之家...
  4. 阿里云 Linux服务器 挂载硬盘
  5. JAVA社区物业管理系统计算机毕业设计Mybatis+系统+数据库+调试部署
  6. java尚硅谷 项目三《开发团队调度项目》最细致流程、总结
  7. lsm mysql_LSM树(Log-StructuredMergeTree)存储引擎
  8. android手机闹铃
  9. 设备树之DTS与DTB格式
  10. Nacos 报错:Request cannot be executed; I/O reactor status: STOPPED