Search code examples
javamachine-learningwekainstances

How to create an `Instances` in Weka from a `List<Instance>`?


I am using Weka as a part of a much longer pipeline, and as a result, I can't afford to write all the data to a file or database just to create an Instances object. What I can do on-the-fly is to create a list of Instance objects.

From this page of their API I can see that Instances implements List<Instance>, but Weka doesn't seem to have a public constructor that will take a List<Instance> as argument. I tried a crude casting as well:

List<Instance> instanceList;
while ( ... ) {         // some conditional loop
    Instance inst = ... // more code where I am populating an individual instance
    instanceList.add(inst);
}
Instances instances = (Instances) instanceList;

In the last line above, Idea Intellij shows me a warning that I may run into a ClassCastException. How can I safely create Instances from my individual Instance objects?


Solution

  • You have to create one using this constructor, and then add them one by one to the new Instances object.

    Or switch to not Weka.