【问题标题】:Save and load SMO weka model from file从文件中保存和加载 SMO weka 模型
【发布时间】:2014-04-07 18:12:28
【问题描述】:

我正在使用 Weka SMO 对我的训练数据进行分类,并且我想轻松地在我的 SMO 模型文件中保存和加载。我创建了一个保存方法,以便将分类器存储到文件中。我的代码如下:

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 对象。

【问题讨论】:

    标签: java model weka


    【解决方案1】:

    好吧,这很简单,我只需要使用 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 2021-12-15
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多