Search code examples
javadatasetinstance-variablessparse-matrix

Adding label(object name) to an Instance object


I created a SparseInstace object using JavaML library, I put some values into that instance but I could not find a solution to assign label on it. For example, the instance is created like

{{2=1.0, 4=2.2};null} 

I want to add specific string value instead of that "null" area. Here is my code:

package helloworld;

import java.io.IOException;
import net.sf.javaml.core.Dataset;
import net.sf.javaml.core.DefaultDataset;
import net.sf.javaml.core.Instance;
import net.sf.javaml.core.SparseInstance;
import net.sf.javaml.matrix.Matrix;
import net.sf.javaml.tools.InstanceTools;
import net.sf.javaml.tools.data.FileHandler;

public class WekaT2 {

public static void main(String[] args) throws IOException, Exception {

    Dataset data = new DefaultDataset();
    Instance tmpInstance = new SparseInstance();

        tmpInstance.put(2, 1.0);
        tmpInstance.put(4, 2.2);
        data.add(tmpInstance);       
    }
}

http://java-ml.sourceforge.net/api/0.1.7/net/sf/javaml/core/SparseInstance.html

How can I add these labels(object names) into the instaces?


Solution

  • What you are trying to set is called the class label or class value as it is stated in the documentation. You can use the following method setClassValue(java.lang.Object value) Sample usage would be

        Instance tmpInstance = new SparseInstance();
        tmpInstance.put(2, 1.0);
        tmpInstance.put(4, 2.2);
        tmpInstance.setClassValue("positive");