Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Reading Data

davidpicard edited this page · 5 revisions

Although it is very easy to write your own data importer, JKernelMachines currently supports libsvm and csv data format natively. For this purpose, the class LibSVMImporter provides a method to read a libsvm data file and return the associated list of TrainingSample:

public static List<TrainingSample<double[]>> importFromFile(String filename) throws IOException;

The main limitation of this importer is that features have to be double vectors, hence the List<TrainingSample<double[]>> that is returned.

The following simple example (available in fr.lip6.test.io.TestLibSVM) reads two libsvm data files. It then trains an SVM with Gaussian kernel on the first list and evaluate the error rate on the second list.

    String trainfile = "train.txt"
    String testfile = "test.txt"
    List<TrainingSample<double[]>> trainlist = null;
    List<TrainingSample<double[]>> testlist = null;

    //parsing train
    try {
        trainlist = LibSVMImporter.importFromFile(trainfile);
    }
    catch (IOException e) {
        System.out.println("Error parsing file "+trainfile);
        return;
    }

    // parsing test
    try {
        testlist = LibSVMImporter.importFromFile(testfile);
    }
    catch (IOException e) {
        System.out.println("Error parsing file "+testfile);
        return;
    }

    //learning
    DoubleGaussL2 kernel = new DoubleGaussL2();
    kernel.setGamma(2.0);
    LaSVM<double[]> svm = new LaSVM<double[]>(kernel);
    svm.setC(10);
    svm.setE(5);

    svm.train(trainlist);

    //evaluation
    double err = 0;
    for(TrainingSample<double[]> t : testlist) {
        double v = svm.valueOf(t.sample);
        if( v * t.label < 0) {
            err++;
        }
    }

    System.out.println("error rate: "+(err/testlist.size()));
Something went wrong with that request. Please try again.