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

你可能感兴趣的:(weka)