Development language: Java
Development tools: Eclipse (http://www.eclipse.org/downloads)
Liblinear version: liblinear-1.94.jar (: http://liblinear.bwaldvogel.de /)
For more information, see: http://www.csie.ntu.edu.tw /~ Cjlin/liblinear/
1. Download liblinear-1.94.jar, import Project
Right-click the project ----> properties -----> select Java build path -----> select the libraries tag -----> click Add external jars.
Find the jar package to be added and click OK.
2. Create a liblinear class (class name is optional)
The Code is as follows:
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 data19 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 value31 double [] targetvalue = {1,-,-}; 32 33 problem = new problem (); 34 problem. L = 5; // number of training examples: number of training samples 35 problem. n = 5; // number of features: feature dimension 36 problem. X = featurematrix; // feature nodes: feature data 37 problem. y = targetvalue; // target values: Category 38 39 solvertype solver = solvertype. l2r_lr; //-s 040 double C = 1.0; // cost of constraints violation41 double EPS = 0.01; // stopping criteria42 43 parameter = new parameter (solver, C, EPS); 44 model = linear. train (problem, parameter); 45 file modelfile = new file ("model"); 46 model. save (modelfile); 47 // load model or use it directly48 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}
Run the command to obtain the classification result of testnode:
3. parameter description
The training samples used in this program are as follows (5 training samples, 5 dimensions ):
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 |
Test sample: testnode variable: (0.4, 0, 0.3, 0)
This article is an original blog. If it is reproduced, please indicate the source.
Liblinear parameters and usage (original)