Serialization

toc

[|Serialization] is the process of saving an object in a persistent form, e.g., on the harddisk as a bytestream. **Deserialization** is the process in the opposite direction, creating an object from a persistently saved data structure. In [|Java], an object can be serialized if it imports the interface.

Members of an object that are not supposed to be serialized, need to be prefixed with the keyword.

In the following you'll find some Java code snippets for serializing and deserializing a classifier. Of course, serialization is not limited to classifiers. Most schemes in Weka, like clusterers and filters, are also serializable.

= Serializing = Here we create a J48 classifier, train it with a dataset , and save the built model to a file. code format="java" // create J48 Classifier cls = new J48; // train Instances inst = new Instances(                   new BufferedReader( new FileReader("/some/where/data.arff"))); inst.setClassIndex(inst.numAttributes - 1); cls.buildClassifier(inst); // serialize model ObjectOutputStream oos = new ObjectOutputStream(                           new FileOutputStream("/some/where/j48.model")); oos.writeObject(cls); oos.flush; oos.close; code In versions > 3.5.5 this is even easier. The last couple of lines shrink to this: code format="java" // serialize model weka.core.SerializationHelper.write("/some/where/j48.model", cls); code
 * Note:**

= Deserializing = Here the previously saved model is deserialized as and again available for classification. code format="java" // deserialize model ObjectInputStream ois = new ObjectInputStream(                          new FileInputStream("/some/where/j48.model")); Classifier cls = (Classifier) ois.readObject; ois.close; code With versions > 3.5.5 it is even easier: code format="java" // deserialize model Classifier cls = (Classifier) weka.core.SerializationHelper.read("/some/where/j48.model"); code
 * Note:**

= Serialization in Weka = The Explorer serializes the classifier and the training header together. This makes it easy to test whether a dataset is compatible with the dataset the classifier was trained with. The commandline option **-d ** of the //developer version// stores the training header as well (>= 3.5.7, since 30/10/07), the //book version// only stores the classifier object. In order to read serialized models that contain the header information as well, you can use the method of the. For serializing models with their datasets, use (only available in Weka >3.6.1 and >3.7.0, or snapshot as of 19/06/2009).

= See also =
 * Use Weka in your Java code

= Links =
 * [|Java Serialization Specifications]
 * [|Java Serialization //Secrets//]