Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a new Instance in weka

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

like image 960
TeFa Avatar asked Aug 25 '12 00:08

TeFa


People also ask

What is instance in Weka?

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.

What are attributes in Weka?

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.

What is Weka API?

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.


1 Answers

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
--------------------------
like image 127
Atilla Ozgur Avatar answered Sep 28 '22 08:09

Atilla Ozgur