java - 使用 Java 代码向 ARFF 添加问号

标签 java weka arff

我有一个为 Weka 准备 ARFF 文件的代码。它将用于使用 j48 算法中已构建的模型对数据进行分类。

在 ARFF 文件中,我需要在最后一列添加问号,以便 Weka 尝试对数据进行分类。我有这个代码:

        ArrayList<Attribute> atts = new ArrayList<Attribute>();
        ArrayList<String> classVal = new ArrayList<String>();
        classVal.add("C1");
        classVal.add("C2");
        atts.add(new Attribute("a"));
        atts.add(new Attribute("b"));
        atts.add(new Attribute("c"));
        atts.add(new Attribute("d"));
        atts.add(new Attribute("@@class@@", classVal));
        Instances dataRaw = new Instances("TestInstances", atts, 0);
        dataRaw.setClassIndex(dataRaw.numAttributes() - 1);
        double[] instanceValue1 = new double[] { p.getIncludedLength(), p.getTimestampSeconds()};
        dataRaw.add(new DenseInstance(1.0, instanceValue1));
        ArffSaver saver = new ArffSaver();
        saver.setInstances(dataRaw);
        saver.setFile(new File(path3));
        saver.setDestination(new File(path3));
        saver.writeBatch();

因此,作为最后一个元素应该有一个 ?,它不是 double 值。我该如何添加它?

double[] instanceValue1 = new double[] { p.getIncludedLength(), p.getTimestampSeconds(), ?};

最佳答案

DenseInstance di = new DenseInstance(3);  // Sets all 3 values to missing
di.setValue(0, p.getIncludedLength());
di.setValue(1, p.getTimestampSeconds());

或者,如果数组中确实有大量值,则可以使用零值作为占位符并尝试 setMissing()

double[] instanceValue1 = new double[] { p.getIncludedLength(), p.getTimestampSeconds(), 0.0}
DenseInstance di = new DenseInstance(1.0, instanceValue1);
di.setMissing(2);

关于java - 使用 Java 代码向 ARFF 添加问号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36051636/

相关文章:

python - 如何在 Weka 中使用 Python 数据?

java - 如何使用 WEKA API 学习贝叶斯网络(结构+参数)?

python - 使用 zipfile 和 scipy.io.arff 从 ZIP 读取 ARFF

java - 如何从棉花糖中的应用程序库中获取绘制覆盖权限

java - 每天同时使所有 Activity session 过期

java - 从 ArrayList 生成定义的集合

java - 组合不同arff文件的属性

java - 在旧代码中使用 SOLID 原则实现新功能

java - 如何在weka中实现成本敏感分类器?

java - 如何在 Java 中添加 vector 作为值