我有一个为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(), ?};
答案 0 :(得分:0)
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);