liblinear参数及使用方法(原创)

开发语言: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类 (类名自选)

代码如下:

 1 package liblinear;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.ArrayList;
 6 import java.util.List;
 7 
 8 import de.bwaldvogel.liblinear.Feature;
 9 import de.bwaldvogel.liblinear.FeatureNode;
10 import de.bwaldvogel.liblinear.Linear;
11 import de.bwaldvogel.liblinear.Model;
12 import de.bwaldvogel.liblinear.Parameter;
13 import de.bwaldvogel.liblinear.Problem;
14 import de.bwaldvogel.liblinear.SolverType;
15 
16 public class LibLinear{
17     public static void main(String[] args) throws Exception {
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 = new Problem();
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 = new Parameter(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 node
51         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. 是约束violation的代价参数 (默认为1)

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

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

labelfeature1feature2feature3feature4feature5
100.10.200
-100.10.3-1.20
10.40000
-100.101.40.5
0-0.1-0.20.11.10.1

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


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

转载于:https://www.cnblogs.com/tec-vegetables/p/4046437.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值