2011-03-15 27 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; 
    } 
}