How to apply classifier in Weka's Explorer?

你离开我真会死。 提交于 2019-12-04 08:15:30

There are special class SerializedClassifier in misc package, it takes model file as parameter and has mock training phase.

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

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

    , 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;
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!