Example: SVOneClass

In this example, a one class support vector machine is used to detect examples in the test data that do not belong to the target population. The target population is the uniform distribution.


import com.imsl.datamining.supportvectormachine.*;
import com.imsl.stat.*;

public class SupportVectorMachineEx3 {

    public static void main(String[] args) throws Exception {

        SVOneClass.VariableType[] ex3VarType = {
            SVOneClass.VariableType.ONE_CLASS,
            SVOneClass.VariableType.QUANTITATIVE_CONTINUOUS
        };

        // 1000 training patterns
        int nTrain = 1000;
        // 100 test patterns
        int nTest = 100;
        int nContaminated = 10;
        int nAttributes = 1;

        double[][] xyTrain = new double[nTrain][nAttributes + 1];
        double[][] xyTest = new double[nTest][2];

        // Create the training set from a uniform distribution.
        Random r = new Random(123457);
        r.setMultiplier(16807);
        for (int i = 0; i < nTrain; i++) {
            xyTrain[i][1] = r.nextFloat();
            xyTrain[i][0] = 1;
        }
        //Construct an SVOneClass object.
        SVOneClass svm1 = new SVOneClass(xyTrain, 0, ex3VarType);
        svm1.setNuParameter(0.001);
        // Train with the training data. 
        svm1.fitModel();

        // Create a testing set from a uniform distribution. 
        for (int i = 0; i < nTest; i++) {
            xyTest[i][1] = r.nextFloat();
            xyTest[i][0] = 1;
        }

        //Contaminate the testing set with deviates from a normal distribution.
        for (int i = 0; i < nContaminated; i++) {
            xyTest[i * 10][1] = r.nextNormal() * Math.sqrt(.2) + .1;
        }
        double[] predictedClass = svm1.predict(xyTest);
        System.out.println("\n\n\n                   Classification Results\n");
        for (int i = 0; i < nTest; i++) {
            if (predictedClass[i] != 1.0) {
                System.out.println("\n The " + i + "-th observation may not "
                        + "belong to the target distribution.");
            }
        }
    }
}

Output




                   Classification Results


 The 0-th observation may not belong to the target distribution.

 The 20-th observation may not belong to the target distribution.

 The 30-th observation may not belong to the target distribution.

 The 40-th observation may not belong to the target distribution.

 The 60-th observation may not belong to the target distribution.

 The 70-th observation may not belong to the target distribution.
Link to Java source.