在weka中向实例添加实例 [英] Adding an instance to Instances in weka

查看:189
本文介绍了在weka中向实例添加实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a few arff files. I would like to read them sequentially and create a large dataset. Instances.add(Instance inst) doesn't add string values to the instances, hence the attempt to setDataset() ... but even this fails. Is there a way to accomplish the intuitively correct thing for strings?

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


推荐答案

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

This is from mailing list. I saved it before


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

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

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

Depends what merge you are talking about. Do you just want to append the second file (both have the same attributes) or do you want to add the merge the attributes (both have the same number of instances)?

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 [])

Here's the relevant Javadoc: http://weka.sourceforge.net/doc.dev/weka/core/Instances.html#main(java.lang.String[])

使用 mergeInstances 合并两个数据集。

 public static Instances mergeInstances(Instances first,
                                   Instances second)

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

Your code would be something like below. For same instance numbers.

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方法。如果您阅读代码,则如下所示。

Your code would be something like below. For same attribute numbers. I do not see any java method in weka. If you read code there is something like below.

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

这篇关于在weka中向实例添加实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆