Eclipse + libsvm-3.12 implement simple linear classification with SVM

Source: Internet
Author: User
Tags svm

0. Download The libsvm compressed package and decompress it to the local directory (from: http://www.csie.ntu.edu.tw /~ Cjlin/libsvm/index.html)
1. Create a JAVA project and import the libsvm package and its source code.
2. Write the test code and use the libsvm function for classification prediction.
3. Reference

1. Create a JAVA project and import the libsvm package and its source code.

1. After creating a JAVA project and main function, right-click Project => build path => configure build path, Java Build Path> Libraries> Add External JARs, and import libsvm. jar.

2. Associate libsvm Source code (for more information, see the Source code): Open libsvm. jar => Source attachment :( None) => Edit, External location => External Folder

Select the java directory under the libsvm-3.12, all the way OK.

[Go Top]

2. Compile the test code and use the libsvm function for classification prediction.

The test code is as follows:

1 import libsvm. svm; 2 import libsvm. svm_model; 3 import libsvm. svm_node; 4 import libsvm. svm_parameter; 5 import libsvm. svm_problem; 6 7 public class jmain {8 9/** 10 * @ param args11 */12 public static void main (String [] args) {13 // defines training set points a {10.0, 10.0} and point B {-10.0,-10.0}, corresponding to lable {1.0, -1.0} 14 svm_node pa0 = new svm_node (); 15 pa0.index = 0; 16 pa0.value = 10.0; 17 svm_node pa1 = new svm_node (); 18 pa 1. index =-1; 19 pa1.value = 10.0; 20 svm_node pb0 = new svm_node (); 21 pb0.index = 0; 22 pb0.value =-10.0; 23 svm_node pb1 = new svm_node (); 24 pb1.index = 0; 25 pb1.value =-10.0; 26 svm_node [] pa = {pa0, pa1}; // point a27 svm_node [] pb = {pb0, pb1 }; // point b28 svm_node [] [] datas = {pa, pb}; // vector table 29 double [] lables = {1.0,-1.0} of the training set }; // lable30 31 for a and B // defines the svm_problem object 32 svm_problem problem = new svm_problem (); 33 problem. l = 2; // number of vectors 34 problem. x = datas; // training set vector table 35 problem. y = lables; // the corresponding lable array 36 37 // defines svm_parameter object 38 svm_parameter param = new svm_parameter (); 39 param. svm_type = svm_parameter.C_SVC; 40 param. kernel_type = svm_parameter.LINEAR; 41 param. cache_size = 100; 42 param. eps = 0.00001; 43 param. C = 1; 44 45 // train SVM classification model 46 System. out. println (svm. svm_check_parameter (problem, param); // If the parameter is correct The svm. svm_check_parameter () function returns null; otherwise, the error description is returned. 47 svm_model model = svm. svm_train (problem, param); // svm. svm_train () Train the SVM classification model 48 49 // define the test data point c50 svm_node pc0 = new svm_node (); 51 pc0.index = 0; 52 pc0.value =-0.1; 53 svm_node pc1 = new svm_node (); 54 pc1.index =-1; 55 pc1.value = 0.0; 56 svm_node [] pc = {pc0, pc1 }; 57 58 // lable59 System of prediction test data. out. println (svm. svm_predict (model, pc); 60} 61}

The running result is:

null*optimization finished, #iter = 1nu = 0.0033333333333333335obj = -0.0033333333333333335, rho = 0.0nSV = 2, nBSV = 0Total nSV = 2-1.0

The first row of null is the output of svm. svm_check_parameter (problem, param), indicating that the parameter settings are correct. The-1.0 in the last row indicates that the predicted lable for the cpoint is-1.0.

Note the following:
1. svm. svm_train () is used for training, and svm. svm_predict () is used for prediction. svm_problem, svm_parameter, svm_model, and svm_node are used for "struct" objects.
2. svm_node indicates {vector component serial number, vector component value}. Many sparse matrices use this method to store data, which saves space. svm_node [] indicates a vector, svm_node.index of the last component of a vector is represented by-1. svm_node [] [] indicates a set of vectors, that is, the training set.

[Go Top]

Reference:

1. Video: one instance handles libsvm Classification "Learn SVM Step by Step" by faruto
2. How to Use libsvm for classification
3. Use of the function library libsvm2.88

[Go Top]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.