开发语言:JAVA

开发工具:eclipse (下载地址 http://www.eclipse.org/downloads/)

liblinear版本:liblinear-1.94.jar (下载地址:http://liblinear.bwaldvogel.de/

更多信息请参考:http://www.csie.ntu.edu.tw/~cjlin/liblinear/

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

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

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

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

代码如下:

 package liblinear;

 import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import de.bwaldvogel.liblinear.Feature;
import de.bwaldvogel.liblinear.FeatureNode;
import de.bwaldvogel.liblinear.Linear;
import de.bwaldvogel.liblinear.Model;
import de.bwaldvogel.liblinear.Parameter;
import de.bwaldvogel.liblinear.Problem;
import de.bwaldvogel.liblinear.SolverType; public class LibLinear{
public static void main(String[] args) throws Exception {
//loading train data
Feature[][] featureMatrix = new Feature[5][];
Feature[] featureMatrix1 = { new FeatureNode(2, 0.1), new FeatureNode(3, 0.2) };
Feature[] featureMatrix2 = { new FeatureNode(2, 0.1), new FeatureNode(3, 0.3), new FeatureNode(4, -1.2)};
Feature[] featureMatrix3 = { new FeatureNode(1, 0.4) };
Feature[] featureMatrix4 = { new FeatureNode(2, 0.1), new FeatureNode(4, 1.4), new FeatureNode(5, 0.5) };
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) };
featureMatrix[0] = featureMatrix1;
featureMatrix[1] = featureMatrix2;
featureMatrix[2] = featureMatrix3;
featureMatrix[3] = featureMatrix4;
featureMatrix[4] = featureMatrix5;
//loading target value
double[] targetValue = {1,-1,1,-1,0}; Problem problem = new Problem();
problem.l = 5; // number of training examples:训练样本数
problem.n = 5; // number of features:特征维数
problem.x = featureMatrix; // feature nodes:特征数据
problem.y = targetValue; // target values:类别 SolverType solver = SolverType.L2R_LR; // -s 0
double C = 1.0; // cost of constraints violation
double eps = 0.01; // stopping criteria Parameter parameter = new Parameter(solver, C, eps);
Model model = Linear.train(problem, parameter);
File modelFile = new File("model");
model.save(modelFile);
// load model or use it directly
model = Model.load(modelFile); Feature[] testNode = { new FeatureNode(1, 0.4), new FeatureNode(3, 0.3) };//test node
double prediction = Linear.predict(model, testNode);
System.out.print("classification result: "+prediction);
}
}

运行后得到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. 是约束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参数及使用方法(原创)的更多相关文章

  1. url 传递参数(特殊字符)解决方法

    url 传递参数(特殊字符)解决方法 首先设置 apache 配置文件, server.xml 在 port=8080 那一行中加上 URIEcoding=GBK 有些符号在URL中是不能直接传递的, ...

  2. VS2013中带命令行参数的调试方法---C++

    今天先记录一下(也是传说中大神喜欢装逼的comment line)c++中向主函数int main(int argc,char** argv )传递4中方法,欢迎添加新方法, 然后可以参考别人写的很好 ...

  3. 低功耗蓝牙BLE之连接事件、连接参数和更新方法

    转自:http://blog.csdn.net/zzfenglin/article/details/51304084 连接事件 在一个连接当中,主设备会在每个连接事件里向从设备发送数据包.一个连接事件 ...

  4. Swift开发第十篇——可变参数函数&初始化方法顺序

    本篇分为两部分: 一.Swift中的可变参数函数 二.初始化方法的顺序 一.Swift中的可变参数函数 可变参数函数指的是可以接受任意多个参数的函数,在 OC 中,拼接字符串的函数就属于可变参数函数 ...

  5. Shell脚本中判断输入参数个数的方法投稿:junjie 字体:[增加 减小] 类型:转载

    Shell脚本中判断输入参数个数的方法 投稿:junjie 字体:[增加 减小] 类型:转载   这篇文章主要介绍了Shell脚本中判断输入参数个数的方法,使用内置变量$#即可实现判断输入了多少个参数 ...

  6. asp.net获取当前页面文件名,参数,域名等方法。统一session验证和权限验证的方法

    转:http://blog.csdn.net/llll29550242/article/details/6054323 ASP.net后台获取当前页面的文件名 System.IO.Path.GetFi ...

  7. NHibernate各种数据库连接参数文件配置方法说明

    //NHibernate各种数据库连接参数文件配置方法说明 //配置文件Config/Hibernate.cfg.xml内容如下所示: <?xml version="1.0" ...

  8. [五]java函数式编程归约reduce概念原理 stream reduce方法详解 reduce三个参数的reduce方法如何使用

    reduce-归约 看下词典翻译: 好的命名是自解释的 reduce的方法取得就是其中归纳的含义 java8 流相关的操作中,我们把它理解 "累加器",之所以加引号是因为他并不仅仅 ...

  9. PID控制最通俗的解释与PID参数的整定方法

    转自->这里 PID是比例.积分.微分的简称,PID控制的难点不是编程,而是控制器的参数整定.参数整定的关键是正确地理解各参数的物理意义,PID控制的原理可以用人对炉温的手动控制来理解.阅读本文 ...

随机推荐

  1. NYOJ 221 Tree (二叉树)

    题目链接 描述 Little Valentine liked playing with binary trees very much. Her favorite game was constructi ...

  2. Go语言 5 函数

    文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ 今天,我们来学习Go语言编程的第五章,函数.首先简单说一下函数的概念和作用.函数是一 ...

  3. window10_使用技巧

    1.系统关机文件 @echo offshutdown -s -t 0 2.终端常用命令 notepad 3.解决浏览器跨域 --disable-web-security --user-data-dir ...

  4. 组合数+逆元 A - Chat Group Gym - 101775A

    题目链接:https://cn.vjudge.net/contest/274151#problem/A 具体思路:我们可以先把所有的情况算出来,为2^n.然后不合法的情况减去就可以了.注意除法的时候要 ...

  5. python设计模式之迭代器与生成器详解(五)

    前言 迭代器是设计模式中的一种行为模式,它提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示.python提倡使用生成器,生成器也是迭代器的一种. 系列文章 python设计模 ...

  6. 安装ssh-keygen

    转载自:http://www.daoan.com/forums/index.php?forumid=5&mods=topicdisplay&postid=4 sudo apt-get ...

  7. windos8设置cpu数量和内存大小

    转自:http://smilejay.com/2012/03/windows_cpu_memory_setting/ Windows 8(测试版)在作为Xen Guest中的benchmark测试.我 ...

  8. 【转载】WebDriver(C#)之十点使用心得

    使用Selenium WebDriver驱动浏览器测试的过程中多多少少会遇到一些折腾人的问题,总结了一部分,做下分享. 一.隐藏元素处理(element not visible) 使用WebDrive ...

  9. 《secret》读书笔记

    这是在从大连到深圳飞机上看完的一本书,也是大学毕业时室友整理书籍给我的.正好回来的途中,一气呵成读完了. 全书讲了几个事情,其中费了很大篇幅就围绕一个主题,当然书题了——秘密,这个秘密就是“吸引力法则 ...

  10. openstack环境下的虚拟机通过浮动IP访问后能ping通外网IP不能ping通域名

    1.环境简介 openstack环境下构造Ubuntu系统的VM,VM配置受管子网和自管子网,同时绑定浮动IP 2.通过浮动IP访问VM后,ping www.baidu.com失败,但是通过IP地址p ...