Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

java - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
--------------------------

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...