Weka Predict API Example - Java Machine Learning AI

Java examples for Machine Learning AI:weka

Description

Weka Predict API Example

Demo Code

/*/*from  ww  w.j ava 2 s  .c  o  m*/
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import weka.core.Instances;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.evaluation.Prediction;
import weka.classifiers.trees.J48;

public class Main {

    public static void main(String[] args) throws Exception {
        String rootPath = "WekaAPICustomTest\\";

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

        //predict instance class values
        Instances originalTrain = new Instances(
                new BufferedReader(
                        new FileReader(
                                "WekaAPICustomTest\\test.arff"))); //load or create Instances to predict
        originalTrain.setClassIndex(originalTrain.numAttributes() - 1);
        //which instance to predict class value
        int s1 = 2;

        //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);

    }

}

Related Tutorials