Usage of LibSvm——In Java

Maven Dependency:

<dependency>
    <groupId>tw.edu.ntu.csie</groupId>
    <artifactId>libsvm</artifactId>
    <version>3.23</version>
</dependency>

Code:

public class svmTest {
    private String testFile = "$path/heart_scale";

    //output model file by training
    private String modelFile = "$path/heart_scale.model";

    //svm training parameters, depends on scenarios
    public String[] trainArgs = {"-s", "3", testFile, modelFile};

    private String outputFile = "$path/heart_scale.output";

    //svm predicting parameters
    private String[] predictArgs = {testFile, modelFile, outputFile};
    private static svmTest libSVM = null;
    
    private svmTest() {
    }

    public static svmTest getInstance() {
        if (libSVM == null)
            libSVM = new svmTest();
        return libSVM;
    }

    /*
     * Training model
     */
    public void trainByLibSVM() {
        try {
            svm_train.main(trainArgs);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
     * Predict and return accuracy
     */
    public double predictByLibSVM() {
        double accuracy = 0;
        try {
            svm_predict.main(predictArgs);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return accuracy;
    }

    public static void main(String[] args) {
        svmTest svmTest = svmTest.getInstance();

        System.out.println("Start training model"); //training model
        svmTest.trainByLibSVM();

        System.out.println("Start predicting result"); //predicting result
        svmTest.predictByLibSVM();
    }
}

 

Notes: Only part of the content is included in following files, but doesn’t affect the result.

Test file: heart_scale

+1 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.419847 9:-1 10:-0.225806 12:1 13:-1 
-1 1:0.583333 2:-1 3:0.333333 4:-0.603774 5:1 6:-1 7:1 8:0.358779 9:-1 10:-0.483871 12:-1 13:1
+1 1:0.166667 2:1 3:-0.333333 4:-0.433962 5:-0.383562 6:-1 7:-1 8:0.0687023 9:-1 10:-0.903226 11:-1 12:-1 13:1
-1 1:0.458333 2:1 3:1 4:-0.358491 5:-0.374429 6:-1 7:-1 8:-0.480916 9:1 10:-0.935484 12:-0.333333 13:1
-1 1:0.875 2:-1 3:-0.333333 4:-0.509434 5:-0.347032 6:-1 7:1 8:-0.236641 9:1 10:-0.935484 11:-1 12:-0.333333 13:-1
-1 1:0.5 2:1 3:1 4:-0.509434 5:-0.767123 6:-1 7:-1 8:0.0534351 9:-1 10:-0.870968 11:-1 12:-1 13:1
+1 1:0.125 2:1 3:0.333333 4:-0.320755 5:-0.406393 6:1 7:1 8:0.0839695 9:1 10:-0.806452 12:-0.333333 13:0.5
+1 1:0.25 2:1 3:1 4:-0.698113 5:-0.484018 6:-1 7:1 8:0.0839695 9:1 10:-0.612903 12:-0.333333 13:1
+1 1:0.291667 2:1 3:1 4:-0.132075 5:-0.237443 6:-1 7:1 8:0.51145 9:-1 10:-0.612903 12:0.333333 13:1
+1 1:0.416667 2:-1 3:1 4:0.0566038 5:0.283105 6:-1 7:1 8:0.267176 9:-1 10:0.290323 12:1 13:1
-1 1:0.25 2:1 3:1 4:-0.226415 5:-0.506849 6:-1 7:-1 8:0.374046 9:-1 10:-0.83871 12:-1 13:1
-1 2:1 3:1 4:-0.0943396 5:-0.543379 6:-1 7:1 8:-0.389313 9:1 10:-1 11:-1 12:-1 13:1
-1 1:-0.375 2:1 3:0.333333 4:-0.132075 5:-0.502283 6:-1 7:1 8:0.664122 9:-1 10:-1 11:-1 12:-1 13:-1
+1 1:0.333333 2:1 3:-1 4:-0.245283 5:-0.506849 6:-1 7:-1 8:0.129771 9:-1 10:-0.16129 12:0.333333 13:-1
-1 1:0.166667 2:-1 3:1 4:-0.358491 5:-0.191781 6:-1 7:1 8:0.343511 9:-1 10:-1 11:-1 12:-0.333333 13:-1
-1 1:0.75 2:-1 3:1 4:-0.660377 5:-0.894977 6:-1 7:-1 8:-0.175573 9:-1 10:-0.483871 12:-1 13:-1
+1 1:-0.291667 2:1 3:1 4:-0.132075 5:-0.155251 6:-1 7:-1 8:-0.251908 9:1 10:-0.419355 12:0.333333 13:1
+1 2:1 3:1 4:-0.132075 5:-0.648402 6:1 7:1 8:0.282443 9:1 11:1 12:-1 13:1
-1 1:0.458333 2:1 3:-1 4:-0.698113 5:-0.611872 6:-1 7:1 8:0.114504 9:1 10:-0.419355 12:-1 13:-1
-1 1:-0.541667 2:1 3:-1 4:-0.132075 5:-0.666667 6:-1 7:-1 8:0.633588 9:1 10:-0.548387 11:-1 12:-1 13:1

 

Training output model file: heart_scale.model

svm_type epsilon_svr
kernel_type rbf
gamma 0.07692307692307693
nr_class 2
total_sv 200
rho 0.225124523813918
SV
0.8256231549478107 1:0.708333 2:1.0 3:1.0 4:-0.320755 5:-0.105023 6:-1.0 7:1.0 8:-0.419847 9:-1.0 10:-0.225806 12:1.0 13:-1.0 
-1.0 1:0.583333 2:-1.0 3:0.333333 4:-0.603774 5:1.0 6:-1.0 7:1.0 8:0.358779 9:-1.0 10:-0.483871 12:-1.0 13:1.0 
1.0 1:0.166667 2:1.0 3:-0.333333 4:-0.433962 5:-0.383562 6:-1.0 7:-1.0 8:0.0687023 9:-1.0 10:-0.903226 11:-1.0 12:-1.0 13:1.0 
-1.0 1:0.458333 2:1.0 3:1.0 4:-0.358491 5:-0.374429 6:-1.0 7:-1.0 8:-0.480916 9:1.0 10:-0.935484 12:-0.333333 13:1.0 
-0.6684518722119502 1:0.875 2:-1.0 3:-0.333333 4:-0.509434 5:-0.347032 6:-1.0 7:1.0 8:-0.236641 9:1.0 10:-0.935484 11:-1.0 12:-0.333333 13:-1.0 
-1.0 1:0.5 2:1.0 3:1.0 4:-0.509434 5:-0.767123 6:-1.0 7:-1.0 8:0.0534351 9:-1.0 10:-0.870968 11:-1.0 12:-1.0 13:1.0 
1.0 1:0.125 2:1.0 3:0.333333 4:-0.320755 5:-0.406393 6:1.0 7:1.0 8:0.0839695 9:1.0 10:-0.806452 12:-0.333333 13:0.5 
-1.0 1:0.291667 2:1.0 3:1.0 4:-0.132075 5:-0.237443 6:-1.0 7:1.0 8:0.51145 9:-1.0 10:-0.612903 12:0.333333 13:1.0 
0.055736268804158946 1:0.416667 2:-1.0 3:1.0 4:0.0566038 5:0.283105 6:-1.0 7:1.0 8:0.267176 9:-1.0 10:0.290323 12:1.0 13:1.0 
-1.0 1:0.25 2:1.0 3:1.0 4:-0.226415 5:-0.506849 6:-1.0 7:-1.0 8:0.374046 9:-1.0 10:-0.83871 12:-1.0 13:1.0 
-1.0 2:1.0 3:1.0 4:-0.0943396 5:-0.543379 6:-1.0 7:1.0 8:-0.389313 9:1.0 10:-1.0 11:-1.0 12:-1.0 13:1.0 
1.0 1:0.333333 2:1.0 3:-1.0 4:-0.245283 5:-0.506849 6:-1.0 7:-1.0 8:0.129771 9:-1.0 10:-0.16129 12:0.333333 13:-1.0 
-1.0 1:0.166667 2:-1.0 3:1.0 4:-0.358491 5:-0.191781 6:-1.0 7:1.0 8:0.343511 9:-1.0 10:-1.0 11:-1.0 12:-0.333333 13:-1.0 
-1.0 1:0.75 2:-1.0 3:1.0 4:-0.660377 5:-0.894977 6:-1.0 7:-1.0 8:-0.175573 9:-1.0 10:-0.483871 12:-1.0 13:-1.0 
-1.0 1:-0.291667 2:1.0 3:1.0 4:-0.132075 5:-0.155251 6:-1.0 7:-1.0 8:-0.251908 9:1.0 10:-0.419355 12:0.333333 13:1.0 
-0.39511700007121386 1:0.458333 2:1.0 3:-1.0 4:-0.698113 5:-0.611872 6:-1.0 7:1.0 8:0.114504 9:1.0 10:-0.419355 12:-1.0 13:-1.0 
-1.0 1:-0.541667 2:1.0 3:-1.0 4:-0.132075 5:-0.666667 6:-1.0 7:-1.0 8:0.633588 9:1.0 10:-0.548387 11:-1.0 12:-1.0 13:1.0 
-0.5986029563861933 1:0.583333 2:1.0 3:1.0 4:-0.509434 5:-0.52968 6:-1.0 7:1.0 8:-0.114504 9:1.0 10:-0.16129 12:0.333333 13:1.0 
0.9152917350861255 1:-0.208333 2:1.0 3:-0.333333 4:-0.320755 5:-0.456621 6:-1.0 7:1.0 8:0.664122 9:-1.0 10:-0.935484 12:-1.0 13:-1.0 
-0.9410722714814128 1:-0.416667 2:1.0 3:1.0 4:-0.603774 5:-0.191781 6:-1.0 7:-1.0 8:0.679389 9:-1.0 10:-0.612903 12:-1.0 13:-1.0

 

Predict output file: heart_scale.output

0.8997082102821107
-0.07873341649784465
-0.30688374550514624
0.9569748542427231
-0.9003495033282937
0.13047679074676993
0.671782656927115
0.9898418146982656
1.1165702388359793
0.9000064222642
0.317435747321112
0.8374652723281483
-0.9761869763232929
-0.34837893074884385
-0.7681050089194917
-0.8997200470396612
1.1687593635200502
0.9038333920935692
-0.9002922603101432
-0.32870064832445084

Leave a Reply

Your email address will not be published. Required fields are marked *