【问题标题】:ARFF output in weka is different depending on if it incrementaly savedweka 中的 ARFF 输出根据是否增量保存而有所不同
【发布时间】:2012-04-22 22:32:29
【问题描述】:

下面是一个程序,它显示了如果来自 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");
        }
    }
}

【问题讨论】:

    标签: weka arff


    【解决方案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。实例现在是 具有两个实现的接口。

    干杯, 渚

    【讨论】:

    • 感谢您的修复。我想我确实需要迁移到较新的 weka 版本。
    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 2013-03-19
    • 2014-09-05
    • 2012-06-23
    • 2021-11-18
    • 2016-12-13
    • 2011-08-26
    • 1970-01-01
    相关资源
    最近更新 更多