문제

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top