Reputation: 1798
how is it possible to add a class attribute in a WEKA ARFF file by using Java?
In particular, my ARFF structure (according to what is stated here) should be:
@attribute text string
@attribute classifyIn {basketball,nonbasketball}
My question is: how to declare classifyIn
programmatically?
My procedure:
I declared the ARFF attributes as follows:
FastVector attributes = new FastVector(2);
attributes.addElement(new Attribute("text", (FastVector) null));
FastVector classes = new FastVector();
classes.addElement(className);
classes.addElement("non" + className);
attributes.addElement(new Attribute("class",classes));
and I am inserting entries as follows:
double[] newInst = new double[2];
newInst[0] = (double)data.attribute(0).addStringValue(textValue);
newInst[1] = (double)data.attribute(1).addStringValue(className);
where className
is either the string basketball
or the string nonbasketball
.
The error:
However, when I run the code, the following error appears:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
Here is a related question, which did not receive an answer.
Upvotes: 1
Views: 1664
Reputation: 1798
The code has to be modified in the following way:
newInst[1] = (double)data.attribute(1).indexOfValue(className);
Upvotes: 1