2012-04-17 34 views
1

下面是一个程序,显示weka的ARFF保存程序以增量模式写入时如何错误地输出字符串。如果参数传递给程序,下面的程序以增量模式运行,如果没有参数传递,程序以批处理模式运行。weka中的ARFF输出根据是否增量保存而有所不同

请注意,在批处理模式下,ARFF文件包含字符串...正常操作。 在增量模式下,ARFF文件包含整数代替字符串......奇怪!

有关如何让ARFF formater以增量格式输出字符串的任何想法?

import java.io.File; 
import java.io.IOException; 

import weka.core.Attribute; 
import weka.core.FastVector; 
import weka.core.Instance; 
import weka.core.Instances; 
import weka.core.converters.ArffSaver; 
import weka.core.converters.Saver; 

public class ArffTest { 
    static Instances instances; 
    static ArffSaver saver; 
    static boolean flag=false; 

    public static void addData(String ticker, double price) throws IOException{ 
     int numAttr = instances.numAttributes(); // same for 
     double[] vals = new double[numAttr]; 
     int i=0; 
     vals[i++] = instances.attribute(0).addStringValue(ticker); 
     vals[i++] = price; 
     Instance instance = new Instance(1.0, vals); 
     if (flag) 
      saver.writeIncremental(instance); 
     else 
      instances.add(instance); 
    } 

    public static void main(String[] args) { 
     if(args.length>0){ 
      flag=true; 
     } 
     FastVector atts = new FastVector();   // attributes 
     atts.addElement(new Attribute("Ticker", (FastVector)null));// symbol 
     atts.addElement(new Attribute("Price")); // price that order exited at. 

     instances = new Instances("Samples", atts, 0); // create header 
     saver = new ArffSaver(); 
     saver.setInstances(instances); 
     if(flag) 
      saver.setRetrieval(Saver.INCREMENTAL); 

     try{ 
      saver.setFile(new File("test.arff")); 
      addData("YY", 23.0); 
      addData("XY", 24.0); 
      addData("XX", 29.0); 
      if(flag) 
       saver.writeIncremental(null); 
      else 
       saver.writeBatch(); 
     }catch(Exception e){ 
      System.out.println("Exception"); 
     } 
    } 
} 

回答

1

您忘了将新创建的实例添加到数据集。

Instance instance = new DenseInstance(1.0, vals); 
instance.setDataset(instances); //Add instance! 
if (flag) 
    saver.writeIncremental(instance); 
else 
    instances.add(instance); 

的实例必须能够访问数据集检索字符串 属性。如果不是,它只是写出索引。

除此之外,我建议使用Weka 3.7.6。实例现在是一个具有两个实现的接口。

欢呼声, Muki

+0

感谢您的修复。我想我需要转向更新的weka版本。 – fodon 2012-05-05 18:32:32