Creating a relational Attribute for Multi Instance Data

Ulrich Mayring <[email protected]> Thu, 14 Dec 2023 18:25:16 +0100
Newsgroups gmane.comp.ai.weka
Message-ID <[email protected]>
Hi folks,

I am trying to create an Instances object in Java, which will contain 
multi instance data.

If you run the code below, then you will see a 
"java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 
0", which is thrown from the last line in the try-block (where 
toString() is called on "rawData").

However, if I define the attribute "bag" as a string attribute (instead 
of relational), then everything works great. So I think I must have made 
some mistake in the way I am creating the relational attribute.


import java.util.ArrayList;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instances;

public class MultiInstance {

     public static void main(String[] args) {
         try {
             ArrayList<Attribute> headerAtts = new ArrayList<>();
             headerAtts.add(new Attribute("content1", 
(ArrayList<String>)null));
             headerAtts.add(new Attribute("content2", 
(ArrayList<String>)null));
             Instances header = new Instances("header", headerAtts, 0);

             ArrayList<Attribute> atts = new ArrayList<>();

             ArrayList<String> bagIdVals = new ArrayList<>();
             bagIdVals.add("bag1");
             atts.add(new Attribute("bagids", bagIdVals));

             atts.add(new Attribute("bag", header));

             Instances dataRaw = new Instances("data", atts, 0);
             System.out.println("Before adding any instance");
             System.out.println("--------------------------");
             System.out.println(dataRaw);
             System.out.println("--------------------------");

             double[] instanceValue = new double[dataRaw.numAttributes()];
             instanceValue[0] = 0;
             instanceValue[1] = 
dataRaw.attribute(1).addStringValue("First Instance Value\\nSecond 
Instance Value");

             dataRaw.add(new DenseInstance(1.0, instanceValue));

             System.out.println("After adding a instance");
             System.out.println("--------------------------");
             System.out.println(dataRaw);
         }
         catch (Exception ex) {
             ex.printStackTrace();
         }
     }

}

_______________________________________________
Wekalist mailing list -- [email protected]
Send posts to [email protected]
To unsubscribe send an email to [email protected]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html