How can I add a new Instance to an existing Instances object that I created ?
Here is an example:
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);
I want to add a new instance to dataRaw. As far as I know I have to use dataRaw.add(Instance i) .... How can I create an instance object if the Instance class is an interface ?
Thanks in Advance
java.lang.Object | +----weka.core.Instance public class Instance extends Object implements Copyable, Serializable. Class for handling an instance. All values (numeric, nominal, or string) are internally stored as floating-point numbers.
This type of attribute represents a dynamically expanding set of nominal values. String attributes are not used by the learning schemes in Weka. They can be used, for example, to store an identifier with each instance in a dataset.
Weka is a collection of machine learning algorithms for data mining tasks. It contains tools for data preparation, classification, regression, clustering, association rules mining, and visualization.
Let start with some highlights.
instanceValue1[0] = dataRaw.attribute(0).addStringValue("This is a string!");
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
--------------------------
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With