简体   繁体   中英

Save and load SMO weka model from file

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM