開發語言: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. 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)
本文為原創博客,若轉載請注明出處。