簡體   English   中英

從文件中保存並加載SMO weka模型

[英]Save and load SMO weka model from file

我正在使用Weka SMO對我的訓練數據進行分類,我希望能夠輕松地保存和加載我的SMO模型文件。 我創建了一個save方法,以便將Classifier存儲到文件中。 我的代碼如下:

private static Classifier loadModel(Classifier c, String name, File path) throws Exception {

  FileInputStream fis = new FileInputStream("/weka_models/" + name + ".model");
  ObjectInputStream ois = new ObjectInputStream(fis);             

return c;
}

private static void saveModel(Classifier c, String name, File path) throws Exception {


    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(
                new FileOutputStream("/weka_models/" + name + ".model"));

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    oos.writeObject(c);
    oos.flush();
    oos.close();

}

我的問題是,如何將ObjectInputStream轉換為Classifier對象。

好吧,這是一個簡單的,我只需要使用readObject。

    private static Classifier loadModel(File path, String name) throws Exception {

    Classifier classifier;

    FileInputStream fis = new FileInputStream(path + name + ".model");
    ObjectInputStream ois = new ObjectInputStream(fis);

    classifier = (Classifier) ois.readObject();
    ois.close();

    return classifier;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM