weka 中的 ARFF 输出会有所不同,具体取决于它是否增量保存



下面是一个程序,它显示了如果来自weka的ARFF保护程序以增量模式写入,字符串是如何错误输出的。如果将参数传递给程序,则下面的程序以增量模式运行,如果未传递任何参数,则以批处理模式运行。

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

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

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");
        }
    }
}

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

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。实例现在是一个与两个实现接口。

干杯穆基

相关内容

最新更新