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 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.