2011-03-15 33 views
5

比方说,我已经建立了一个模型(例如J4.8树)并用交叉验证对其进行了评估。我如何使用此模型来分类新数据集?我知道,我可以使用“提供的测试集”选项对数据进行设置,在“更多选项”窗口中标记“输出预测”并再次运行分类。它会产生我所需要的几乎,但它似乎是一个非常奇怪的工作流程。此外,它会重新创建所有模型,这会花费不必要的时间。有没有更直接的方法来使用已建模型进行分类?如何在Weka的资源管理器中应用分类器?

回答

2

misc包中有特殊的SerializedClassifier类,它以模型文件为参数,具有模拟训练阶段。

+0

所以,我明白没有特别的按钮“分类”,这个选项似乎是最合适的。我接受这个答案。 – ffriend 2011-03-17 17:38:08

6

这里有几种方法。

第一个

你可以使用命令行来保存和加载模型,-l和-d命令行开关可以让你做到这一点。

从秧鸡文档

 
-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. 

第二个

而且在您产生的模型使用第二点击保存和加载模型。见following image

第三个

你也可以生成Java代码的分类。这样你可以保存你的分类器并重新使用它。按照这个步骤。

  1. 单击更多选项按钮。
  2. 从打开dialog开始,选择输出源代码。
  3. 给分类器名称更有意义的名称。

这些步骤将为您的j48分类器输出java类。 WekaJ48ForIris下面是weka创建的与Iris数据集一起使用的示例。您可能需要重构它以使其更有用。

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; 
    } 
}