3

I want to merge 2 arff files. They have same attributes, but the values under the attributes changes.

For example:

1.arff: weather(sunny, rainy). 50 instances
2.arff: weather(warm, cold, freezing). 30 instances

I want to create a new arff:

3.arff: weather(sunny,rainy,warm,cold,freezing). 80 instances

I tried in sample CLI (weka):

java weka.core.Instances 1.arff 2.arff > 3.arff
java weka.core.Instances append 1.arff 2.arff > 3.arff
java weka.core.Instances merge 1.arff 2.arff > 3.arff

None of them worked.

I will be grateful if any help.

Thank you very much.

2 Answers2

0

Have you tried using MergeSets class to merge the files. You can try that from here: http://bioweka.sourceforge.net/docs/api/bioweka/filters/universal/MergeSets.html

Also you can write a small code to merge two arff files:

import weka.core.converters.ArffLoader
import java.io.{File,FileWriter}

def combineAllArffs() {
  var arffLoader = new ArffLoader
  val arffDir: File = new File(s"Arff/")

  val arffList = arffDir.listFiles;
  var instances: Instances = null
  var structure: Instances = null

  if (arffList == null) {
    print(s"Warning: Arff list for '$mode' is empty.")
    return
  }

  for (arffFile <- arffList) {
    arffLoader.setFile(arffFile)
    if (instances == null) {
      instances = arffLoader.getDataSet
      structure = arffLoader.getStructure
    } else {
      var newInstances = arffLoader.getDataSet
      var i = 0
      while (i < newInstances.numInstances) {
        val instance = newInstances.instance(i)
        instances.add(instance)
        i += 1
      }
    }
    arffLoader.reset
  }

  val combinedFile = new File(s"Arff/Combined.arff")
  val fw = new FileWriter(combinedFile)
  fw.write(instances.toString)
  fw.close
}

This should work for your case.

Mayank Agarwal
  • 376
  • 2
  • 9
0

A few days ago, same issue came up and i wrote a small script with python. Here you can find it.

ugursogukpinar
  • 327
  • 1
  • 5