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

源自: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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值