Re: simplest of code

Peter Reutemann <[email protected]>
Newsgroups gmane.comp.ai.weka
Message-ID <CAHoQ12+zdRTMUuFnPOMmJuyXQieX1rhKmYscRnvYp+YK1k2Jng@mail.gmail.com>
> Have errors with 'currentInstance' and 'vals'

Java requires you to define the type of variables, since it is a
statically typed language (unlike Python).

An IDE like IntelliJ Idea would have highlighted the errors for you.

I've attached a reworked version.

Cheers, Peter
-- 
Peter Reutemann
Dept. of Computer Science
University of Waikato, NZ
+64 (7) 858-5174 (office)
+64 (7) 577-5304 (home office)
http://www.cms.waikato.ac.nz/~fracpete/
http://www.data-mining.co.nz/

_______________________________________________
Wekalist mailing list -- [email protected]
Send posts to [email protected]
To unsubscribe send an email to [email protected]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
WekaApp.java (text/x-java, 3 KB)
package adams;

import weka.classifiers.Classifier;
import weka.core.DenseInstance;
import weka.core.Instances;
import weka.core.SerializationHelper;
import weka.core.Utils;

public class WekaApp {

  /** the model to use. */
  protected Classifier cls;

  /** the training data header. */
  protected Instances header;

  /**
   * Load model and header.
   *
   * @throws Exception if deserialization fails
   */
  public void init() throws Exception {
    // [A] load model and header of data
    // deserialize model
    Object[] objs = SerializationHelper.readAll("/home/fracpete/temp/j48.model");
    cls = (Classifier) objs[0];
    header = (Instances) objs[1];
  }

  /**
   * Returns the class label associated with the index.
   *
   * @param index the class label index
   * @return the label
   * @throws IllegalStateException if no header present
   */
  public String getLabel(int index) throws IllegalStateException {
    if (header == null)
      throw new IllegalStateException("No model/header loaded?");
    return header.classAttribute().value(index);
  }

  /**
   * Performs a prediction.
   *
   * @param v the value of the input variable to use for making a prediction
   * @return the predicted value (class index), returns -1 if it fails to make prediction
   * @throws Exception if prediction fails
   */
  public int currentInstance(double v) throws Exception {
    try {
      // [B] create instance
      // create the current instance
      double[] values = new double[header.numAttributes()];
      for (int i = 0; i < values.length; i++)
        values[i] = Utils.missingValue();
      // - numeric
      values[0] = v;
      DenseInstance currentInstance = new DenseInstance(1.0, values);
      currentInstance.setDataset(header);

      // [C] classify nominal class
      // classifyInstance() just returns the index of the predicted label (the one with the highest probability) as a double
      double prediction = cls.classifyInstance(currentInstance);
      System.out.println("prediction: " + prediction);

      return (int) prediction;
    }
    catch (Exception e) {
      System.err.println("Weka exception occurred: " + e);
      e.printStackTrace();
      return -1;
    }
  }

  /**
   * For testing.
   *
   * @param args ignored
   */
  public static void main(String[] args) {
    WekaApp wekaApp = new WekaApp();
    // this should only be called once (or whenever the model has changed and needs reloading)
    try {
      wekaApp.init();
    }
    catch (Exception e) {
      System.err.println("ERROR init: " + e);
      e.printStackTrace();
    }
    // make a prediction
    try {
      int pred = wekaApp.currentInstance(1.2345);  // TODO You need to provide input variable, this is just a dummy value
      if (pred != -1)
        System.out.println("Predicted label: " + wekaApp.getLabel(pred));
      else
        System.out.println("Failed to make prediction!");
    }
    catch (Exception e) {
      System.err.println("ERROR predict: " + e);
      e.printStackTrace();
    }
  }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.