如何将WEKA中的分类器Model提取出来

来源:互联网 发布:mac vm共享的文件在哪 编辑:程序博客网 时间:2024/06/05 06:15

源自:http://weka.wikispaces.com/Serialization


最近的一个项目涉及到一些数据挖掘的内容, 因此,接触了WEKA,不得不承认,对于菜鸟来说,WEKA,确实是一个很不错的工具。他的好处太多了,不一一赘述,

刚开始,就有个以为就是,用WEKA训练好的model,以后如何使用,这次知道了,看如下翻译:




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 java.io.Serializable interface. 
这里面主要是说串行化和反串行化,这个是讲类成员信息存储到字符串,然后存下来,毫无争议。
Members of an object that are not supposed to be serialized, need to be prefixed with the keyword transient.

In the following you'll find some Java code snippets for serializing and deserializing a J48 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 cls, train it with a dataset /some/where/data.arff, and save the built model to a file /some/where/j48.model.


下面的代码就是串行化过程。


// 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();



Note:
In versions > 3.5.5 this is even easier. The last couple of lines shrink to this:

如下是高版本中,更简洁的方法。

 // serialize model weka.core.SerializationHelper.write("/some/where/j48.model", cls);



Deserializing

Here the previously saved model is deserialized as cls and again available for classification.

 // deserialize model ObjectInputStream ois = new ObjectInputStream(                           new FileInputStream("/some/where/j48.model")); Classifier cls = (Classifier) ois.readObject(); ois.close();




Note:

With versions > 3.5.5 it is even easier:
 // deserialize model Classifier cls = (Classifier) weka.core.SerializationHelper.read("/some/where/j48.model");



0 0
原创粉丝点击