Sometimes, it is useful to build a model and save it for later use. This can be achieved with the java.io.Serializable
interface that saves an object into a bytestream as a file. The opposite direction, that is, deserialization, restores the bytestream into the original object.
We will first build a sample model, that is, a J48
decision tree to demonstrate serialization and deserialization:
import java.io.BufferedReader; import java.io.FileReader; import weka.classifiers.trees.J48; import weka.core.Instances; ... J48 cls = new J48(); Instances inst = new Instances(new BufferedReader(new FileReader("dataset/titanic.arff"))); inst.setClassIndex(inst.numAttributes() - 1); cls.buildClassifier(inst); weka.core.SerializationHelper.write("j48.model", cls); J48 cls2 = (J48) weka.core.SerializationHelper.read("j48.model"); System.out.println(cls2);
First, we build a sample model, as shown in the Classification task.
Classifier cls = new J48(); Instances inst = new Instances(new BufferedReader(new FileReader("/dataset/titanic.arff"))); inst.setClassIndex(inst.numAttributes() - 1); cls.buildClassifier(inst);
Since classifiers have built-in support for serialization, simply call the write(String, Object)
static method, which is located in the weka.core.SerializationHelper
package with String
indicating filename and Object
as classifier:
weka.core.SerializationHelper.write("j48.model", cls);
When you need to load the saved classifier, use the read (String)
method located in the same package. Note, that the read()
method loads the model as Object
, so the model needs to be type-casted to an appropriate class, for example, J48
in our case:
J48 cls2 = (J48) weka.core.SerializationHelper.read("j48.model");
Classifier is now loaded in the variable named cls2
and can be used as shown in other recipes.