Get forecast percentage in WEKA using your own Java code and model

Overview

I know that you can get the percentages of each forecast in the trained WEKA model using the graphical interface and command line parameters, as it is convenient to explain and demonstrate in the article "Prediction" documentation .

WHAT I WANT WITH MY WEKA OOOHH * LADY GAGA PIANO *

Predictions

I know that there are three ways to get these forecasts:

.MODEL , , , ( GUI Explorer, CSV ):

inst#,actual,predicted,error,distribution,
1,1:0,2:1,+,0.399409,*0.7811
2,1:0,2:1,+,0.3932409,*0.8191
3,1:0,2:1,+,0.399409,*0.600591
4,1:0,2:1,+,0.139409,*0.64
5,1:0,2:1,+,0.399409,*0.600593
6,1:0,2:1,+,0.3993209,*0.600594
7,1:0,2:1,+,0.500129,*0.600594
8,1:0,2:1,+,0.399409,*0.90011
9,1:0,2:1,+,0.211409,*0.60182
10,1:0,2:1,+,0.21909,*0.11101

predicted - , .MODEL.


WEKA API, , (PlainText, Evaluation). - k-fold, Evaluation.

StringBuffer predictionSB = new StringBuffer();
Range attributesToShow = null;
Boolean outputDistributions = new Boolean(true);

PlainText predictionOutput = new PlainText();
predictionOutput.setBuffer(predictionSB);
predictionOutput.setOutputDistribution(true);

Evaluation evaluation = new Evaluation(data);
evaluation.crossValidateModel(j48Model, data, numberOfFolds,
        randomNumber, predictionOutput, attributesToShow,
        outputDistributions);

System.out.println(predictionOutput.getBuffer());

WEKA

, .MODEL, .ARFF , " Weka Java-" "" aka " .MODEL Java- " ( smfh).

Java

.MODEL "Deserialization", > 3.5.5:

// deserialize model
Classifier cls = (Classifier) weka.core.SerializationHelper.read("/some/where/j48.model");

Instance - , classifyInstance. ( ):

// classify an Instance object (testData)
cls.classifyInstance(testData.instance(0));

" , explorer ( weka) eclipse java" , !

Javadocs

Javadocs Classifier ( ) Evaluation ( ), .

, , , classifyInstances Classifier:

. , . , , distributionForInstance().


WEKA .MODEL Java (, WEKA API)?

WHAT I WANT WITH MY WEKA OOOHH * LADY GAGA PIANO *

+4
1

, explorer ( weka) eclipse java.

, ( ). - J48, Weka Explorer. , Weka. "tree.model".

import weka.classifiers.Classifier;
import weka.core.Instances;

public class Main {

    public static void main(String[] args) throws Exception
    {
        String rootPath="/some/where/"; 
        Instances originalTrain= //instances here

        //load model
        Classifier cls = (Classifier) weka.core.SerializationHelper.read(rootPath+"tree.model");

        //predict instance class values
        Instances originalTrain= //load or create Instances to predict

        //which instance to predict class value
        int s1=0;

        //perform your prediction
        double value=cls.classifyInstance(originalTrain.instance(s1));

        //get the prediction percentage or distribution
        double[] percentage=cls.distributionForInstance(originalTrain.instance(s1));

        //get the name of the class value
        String prediction=originalTrain.classAttribute().value((int)value); 

        System.out.println("The predicted value of instance "+
                                Integer.toString(s1)+
                                ": "+prediction); 

        //Format the distribution
        String distribution="";
        for(int i=0; i <percentage.length; i=i+1)
        {
            if(i==value)
            {
                distribution=distribution+"*"+Double.toString(percentage[i])+",";
            }
            else
            {
                distribution=distribution+Double.toString(percentage[i])+",";
            }
        }
        distribution=distribution.substring(0, distribution.length()-1);

        System.out.println("Distribution:"+ distribution);
    }

}

:

The predicted value of instance 0: no  
Distribution: *1, 0
+3

All Articles