编程语言
首页 > 编程语言> > java – 在weka中向Instances添加实例

java – 在weka中向Instances添加实例

作者:互联网

我有一些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);
                }

解决方法:

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

how to merge two data file a.arff and b.arff into one data list?

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

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));
  }

标签:java,weka
来源: https://codeday.me/bug/20191005/1856166.html