我有一个为 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/