Save and load SMO weka model from file

蓝咒 提交于 2019-12-08 06:09:18

问题


I am using Weka SMO to classify my training data and I want to save and load easily to/from file my SMO model. I have created a save method in order to store Classifier to file. My code is the following:

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();

}

My problem is, how to convert ObjectInputStream to Classifier object.


回答1:


Ok it was an easy one, I ve just had to use 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;
}


来源:https://stackoverflow.com/questions/22201949/save-and-load-smo-weka-model-from-file

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