Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply classifier in Weka's Explorer?

Let's say, I've build a model (e.g. J4.8 tree) and evaluated it with cross-validation. How can I use this model to classify new dataset? I know, I can set a file with the data to classify with "Supplied test set" option, mark "Output predictions" in "More options" window and run classification again. It will produce nearly what I need, but it seems to be a very strange workflow. Also, it re-creates all the model, which can take unnecessary time. Is there more straightforward way to do classification with already built model?

like image 393
ffriend Avatar asked Mar 15 '11 23:03

ffriend


1 Answers

There couple of ways to this.

First one

You may use command line to save and load your model, -l and -d command line switches allows you to do this.

From weka docs

-l 
    Sets model input file. In case the filename ends with '.xml',
    a PMML file is loaded or, if that fails, options are loaded
    from the XML file.
-d 
    Sets model output file. In case the filename ends with '.xml',
    only the options are saved to the XML file, not the model.

Second one

Also after you generate your model use second click to save and load your model. See following image

Third one

Also you may generate java code for your classifier. This way you save your classifier and re-use it again.Follow this steps.

  1. Click More Options button.
  2. From opened dialog , choose output source code.
  3. Give Classifier Name more meaningful name.

These steps will output java classes for your j48 classifier. Below class WekaJ48ForIris is created by weka for use with Iris dataset.You may need to refactor it some to make it more useful.

class WekaJ48ForIris {

  public static double classify(Object[] i)
    throws Exception {

    double p = Double.NaN;
    p = WekaJ48ForIris.N26a305890(i);
    return p;
  }
  static double N26a305890(Object []i) {
    double p = Double.NaN;
    if (i[3] == null) {
      p = 0;
    } else if (((Double) i[3]).doubleValue() <= 0.6) {
      p = 0;
    } else if (((Double) i[3]).doubleValue() > 0.6) {
    p = WekaJ48ForIris.N18c079301(i);
    } 
    return p;
  }
  static double N18c079301(Object []i) {
    double p = Double.NaN;
    if (i[3] == null) {
      p = 1;
    } else if (((Double) i[3]).doubleValue() <= 1.7) {
    p = WekaJ48ForIris.N4544b022(i);
    } else if (((Double) i[3]).doubleValue() > 1.7) {
      p = 2;
    } 
    return p;
  }
  static double N4544b022(Object []i) {
    double p = Double.NaN;
    if (i[2] == null) {
      p = 1;
    } else if (((Double) i[2]).doubleValue() <= 4.9) {
      p = 1;
    } else if (((Double) i[2]).doubleValue() > 4.9) {
    p = WekaJ48ForIris.N3a0872863(i);
    } 
    return p;
  }
  static double N3a0872863(Object []i) {
    double p = Double.NaN;
    if (i[3] == null) {
      p = 2;
    } else if (((Double) i[3]).doubleValue() <= 1.5) {
      p = 2;
    } else if (((Double) i[3]).doubleValue() > 1.5) {
      p = 1;
    } 
    return p;
  }
}
like image 171
Atilla Ozgur Avatar answered Oct 08 '22 15:10

Atilla Ozgur