Skip to content

Reading Data

davidpicard edited this page · 5 revisions

Although it is very easy to write your own data importer, JKernelMachines currently only supports libsvm 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.

The following simple example (available in 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);

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

    DoubleGaussL2 kernel = new DoubleGaussL2();
    LaSVM<double[]> svm = new LaSVM<double[]>(kernel);


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

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