我正在使用 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 对象。
最佳答案
好吧,这很简单,我只需要使用 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;
}
关于java - 从文件中保存和加载 SMO weka 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22201949/