weka: 生成并保存ARFF格式数据

package org.honghe;
 
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSink;
 
public class AttTest {
     
     /**
      *
      */
     private static void test() throws Exception{
         FastVector  atts;
         FastVector  attVals;
         Instances   data;
         double []    vals;
         
         //1. set up attributes
         atts = new FastVector();
         // - numeric
         atts.addElement( new Attribute( "att1" ));
         // - nominal
         attVals = new FastVector();
         for ( int i = 0 ; i < 5 ; i++) {
             attVals.addElement( "val" + (i + 1 ));
         }
         //数组中添加数组
         atts.addElement( new Attribute( "att2" , attVals));
         // - string
         atts.addElement( new Attribute( "att3" , (FastVector) null ));
         // - date
         atts.addElement( new Attribute( "att4" , "yyyy-MM-dd" ));
         
         //2. create Instances object
         data = new Instances( "myRelation" , atts, 0 );
         
         //3. fill with data
         //first instance
         vals = new double [data.numAttributes()];
         // - numeric
         vals[ 0 ] = Math.PI;
         // - nominal
         vals[ 1 ] = attVals.indexOf( "val3" );
         // - string
         vals[ 2 ] = data.attribute( 2 ).addStringValue( "This is a string!" );
         // - date
         vals[ 3 ] = data.attribute( 3 ).parseDate( "2012-11-22" );
         // add
         data.add( new Instance( 1.0 , vals));
         
         // 4. output data
         System.out.println(data);
         
         // 5. write to file
         DataSink.write( "/dict/arffData.arff" , data);
     }
     
     public static void main(String[] args) throws Exception {
         test();
     }
}
参考:  http://weka.wikispaces.com/Creating+an+ARFF+file
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值