Let start with some highlights.
- weka stores every instance value as double[]. Therefore you create double[] instanceValue1 and add values to this array.
- Even strings are stored in this double[]. You add your string values with following code:
instanceValue1[0] = dataRaw.attribute(0).addStringValue("This is a string!");
- Instance is interface, and it is implemented by two classes, { SparseInstance , DenseInstance}. We add our value array to dataset using following code :
dataRaw.add(new DenseInstance(1.0, instanceValue1));
Here is the complete running example:
import java.util.ArrayList;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
public class Program {
public static void main(String[] args) {
ArrayList<Attribute> atts = new ArrayList<Attribute>(2);
ArrayList<String> classVal = new ArrayList<String>();
classVal.add("A");
classVal.add("B");
atts.add(new Attribute("content",(ArrayList<String>)null));
atts.add(new Attribute("@@class@@",classVal));
Instances dataRaw = new Instances("TestInstances",atts,0);
System.out.println("Before adding any instance");
System.out.println("--------------------------");
System.out.println(dataRaw);
System.out.println("--------------------------");
double[] instanceValue1 = new double[dataRaw.numAttributes()];
instanceValue1[0] = dataRaw.attribute(0).addStringValue("This is a string!");
instanceValue1[1] = 0;
dataRaw.add(new DenseInstance(1.0, instanceValue1));
System.out.println("After adding a instance");
System.out.println("--------------------------");
System.out.println(dataRaw);
System.out.println("--------------------------");
double[] instanceValue2 = new double[dataRaw.numAttributes()];
instanceValue2[0] = dataRaw.attribute(0).addStringValue("This is second string!");
instanceValue2[1] = 1;
dataRaw.add(new DenseInstance(1.0, instanceValue2));
System.out.println("After adding second instance");
System.out.println("--------------------------");
System.out.println(dataRaw);
System.out.println("--------------------------");
}
}
Its output is following:
Before adding any instance
--------------------------
@relation TestInstances
@attribute content string
@attribute @@class@@ {A,B}
@data
--------------------------
After adding a instance
--------------------------
@relation TestInstances
@attribute content string
@attribute @@class@@ {A,B}
@data
'This is a string!',A
--------------------------
After adding second instance
--------------------------
@relation TestInstances
@attribute content string
@attribute @@class@@ {A,B}
@data
'This is a string!',A
'This is second string!',B
--------------------------
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…