Search code examples
javamachine-learningsvmencog

SVM using Encog in Java for beginners


I am beginner in SVM. Could someone please help me to understand the concepts of SVM using Encog from the very basics?? It will be helpful with sample Java code.


Solution

  • In Encog SVM is just a classification or regression model and can be used mostly interchangably with other model types. I modified the Hello World XOR example to use it, you can see the results below.

    This is a decent intro to them: http://webdoc.nyumc.org/nyumc/files/chibi/user-content/Final.pdf This is a more basic intro to modeling in general, I wrote it for neural networks, but it applies to SVM as well: http://www.heatonresearch.com/content/non-mathematical-introduction-using-neural-networks

    package org.encog.examples.neural.xor;
    
    import org.encog.Encog;
    import org.encog.ml.data.MLData;
    import org.encog.ml.data.MLDataPair;
    import org.encog.ml.data.MLDataSet;
    import org.encog.ml.data.basic.BasicMLDataSet;
    import org.encog.ml.svm.SVM;
    import org.encog.ml.svm.training.SVMTrain;
    
    public class XORHelloWorld {
    
        /**
         * The input necessary for XOR.
         */
        public static double XOR_INPUT[][] = { { 0.0, 0.0 }, { 1.0, 0.0 },
                { 0.0, 1.0 }, { 1.0, 1.0 } };
    
        /**
         * The ideal data necessary for XOR.
         */
        public static double XOR_IDEAL[][] = { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } };
    
        /**
         * The main method.
         * @param args No arguments are used.
         */
        public static void main(final String args[]) {
    
            // create a SVM for classification, change false to true for regression     
            SVM svm = new SVM(2,false);
    
            // create training data
            MLDataSet trainingSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL);
    
            // train the SVM
            final SVMTrain train = new SVMTrain(svm, trainingSet);
            train.iteration();
            train.finishTraining();
    
            // test the SVM
            System.out.println("SVM Results:");
            for(MLDataPair pair: trainingSet ) {
                final MLData output = svm.compute(pair.getInput());
                System.out.println(pair.getInput().getData(0) + "," + pair.getInput().getData(1)
                        + ", actual=" + output.getData(0) + ",ideal=" + pair.getIdeal().getData(0));
            }
    
            Encog.getInstance().shutdown();
        }
    }