zoukankan      html  css  js  c++  java
  • 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维特征):

    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)


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

  • 相关阅读:
    个人作业-数组3
    Java编程思想
    19年春第三周学习
    个人作业-数组2
    自我介绍
    个人作业-数组
    19春第二周学习心得
    fiddler--华为手机下载安装fiddler证书
    RF接口测试----post请求
    RF接口测试----get请求
  • 原文地址:https://www.cnblogs.com/tec-vegetables/p/4046437.html
Copyright © 2011-2022 走看看