1. First, download the libsvm package from Professor Lin zhiren. There are many versions of libsvm. Here we find the Java version. For example:
2. Access this file and we can see the following file structure: the file circled in red is the file to be introduced into the Java project. copy the file first and press Ctrl + V later. 3. create a common Java project in myeclipse and press Ctrl + V in the SRC file. Create a service folder for better file structure, specifically put svm_predict.java, svm_scale.java and other four java files scattered in the root directory. Commain is a main method for calling. trainfile mainly stores test and training data, and the Lib folder is not required. 4. Prepare training data. Libsvm can be executed only by preparing data in its own format. The format requirements are as follows: label1 index1: value1 index2: value2... label2 index1: value1 index2: value2 ...... label indicates that the label index indicates the label, and value indicates the value under the label. For example, there are four students in the class, two men and two women. The height and weight of the two boys were 171, 65, 173, and 66, respectively. The height and weight of the two girls were 156, 45, And, respectively. Boys use label 1 instead of girls use-1. In this way, we can create data in the following format: 1 1: 171 2: 651 1: 173 2: 662. Save it to the trainfilefile named train1.txt. Someone will doubt that the first column is the height, the second column shows the weight. Why do we need to mark 1 and 2? In fact, in the actual prediction classification, not all attributes of each row have values, but only a part of the data. In this way, a label is required to tell the program, which column is the attribute value. 5. The prediction data is also prepared in the same way as the training data. Because we are forecasting, we can replace all the labels with 1 or something else. Save it to the trainfilefile named train2.txt 6. Write the code in the main function.
Package COM. libsvmtest. main; import Java. io. ioexception; import service. svm_predict; import service. svm_train; public class commain {/*** @ Param ARGs * @ throws ioexception */public static void main (string [] ARGs) throws ioexception {// todo auto-generated method stubstring [] Arg = {"trainfile \ train1.txt ", // path for storing the data used by the SVM Training Model "trainfile \ model_r.txt "}; // store the SVM training data. // The trained model path string [] parg = {"trainfile \ train2.txt ", // store the test data "trainfile \ model_r.txt", // call the trained model "trainfile \ out_r.txt "}; // The path of the generated result file is system. out. println ("........ SVM running starts .......... "); // create a training object svm_train T = new svm_train (); // create a prediction or classification object svm_predict P = new svm_predict (); T. main (ARG); // call p. main (parg); // call }}7.runtime, the final result is stored in out_r.txt, which stores a matrix of 1 row and n columns, and N corresponds to the number of predictions. Each column value is a classification with predictions for each column.