Search code examples
classificationwekalibsvm

Weka library java: how to get the prospect of a classification?


I'm currently using the following code to perform a classification.

    DataSource source = new DataSource(caminhoDados);
    //System.out.println(source.getStructure());
    Instances data = source.getDataSet();

    // setting class attribute if the data format does not provide this information
    // For example, the XRFF format saves the class attribute information as well
    if (data.classIndex() == -1)
        data.setClassIndex(data.numAttributes() - 1);


    //initialize svm classifier
    LibSVM svm = new LibSVM();
    String opts = "-S 0 -K 0 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.0010 -P 0.1 -seed 1";
    svm.setOptions(weka.core.Utils.splitOptions(opts));

    svm.buildClassifier(data);

    Instance instancia = new Instance(5);
    instancia.setDataset(data);
    instancia.setValue(0, '1');
    instancia.setValue(1, '1');
    instancia.setValue(2, '0');
    instancia.setValue(3, '0');
    double resu = svm.classifyInstance(instancia);


    System.out.println("Predição: " + resu);

This code returns the sort that the new instance received . However I need also return the percentage of assurance that the algorithm has on the classification . Example: It was detected that the new instance has 80 % chance to be classified as POSITIVE.

Can anybody help me?

Thank you.


Solution

  • You can use "distributionForInstance()". This only works for classifiers capable of outputting distribution predictions. Please see "How to get predication value for an instance in weka?".