在weka中向实例添加实例

我有一些arff文件。 我想按顺序阅读它们并创建一个大型数据集。 Instances.add(Instance inst)不会向实例添加字符串值,因此尝试setDataset()…但即使这样也会失败。 有没有办法为字符串完成直观正确的事情?

ArffLoader arffLoader = new ArffLoader(); arffLoader.setFile(new File(fName)); Instances newData = arffLoader.getDataSet(); for (int i = 0; i < newData.numInstances(); i++) { Instance one = newData.instance(i); one.setDataset(data); data.add(one); } 

这是来自邮件列表。 我之前保存过它

如何将两个数据文件a.arff和b.arff合并到一个数据列表中?

取决于你所说的合并。 您是否只想附加第二个文件(两者都具有相同的属性),或者您是否要添加合并属性(两者具有相同数量的实例)?

 In the first case ("append"): java weka.core.Instances append filename1 filename2 > output-file and the latter case ("merge"): java weka.core.Instances merge filename1 filename2 > output-file 

这是相关的Javadoc:http://weka.sourceforge.net/doc.dev/weka/core/Instances.html#main(java.lang.String [])

使用mergeInstances合并两个数据集。

  public static Instances mergeInstances(Instances first, Instances second) 

您的代码如下所示。 对于相同的实例编号。

 ArffLoader arffLoader = new ArffLoader(); arffLoader.setFile(new File(fName1)); Instances newData1 = arffLoader.getDataSet(); arffLoader.setFile(new File(fName2)); Instances newData2 = arffLoader.getDataSet(); Instances mergedData = Instances.mergeInstances( newData1 ,newData2); 

您的代码如下所示。 对于相同的属性编号。 我在weka中没有看到任何java方法。 如果您阅读代码,则有类似下面的内容。

 // Instances.java // public static void main(String[] args) { // read two files, append them and print result to stdout else if ((args.length == 3) && (args[0].toLowerCase().equals("append"))) { DataSource source1 = new DataSource(args[1]); DataSource source2 = new DataSource(args[2]); String msg = source1.getStructure().equalHeadersMsg(source2.getStructure()); if (msg != null) throw new Exception("The two datasets have different headers:\n" + msg); Instances structure = source1.getStructure(); System.out.println(source1.getStructure()); while (source1.hasMoreElements(structure)) System.out.println(source1.nextElement(structure)); structure = source2.getStructure(); while (source2.hasMoreElements(structure)) System.out.println(source2.nextElement(structure)); }