将标签(对象名称)添加到实例对象

时间:2012-11-10 08:13:39

标签: java dataset instance-variables sparse-matrix

我使用JavaML库创建了一个SparseInstace对象,我在该实例中添加了一些值,但是我找不到在其上分配标签的解决方案。例如,实例创建类似

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

我想添加特定的字符串值而不是“null”区域。这是我的代码:

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

如何将这些标签(对象名称)添加到实例中?

2 个答案:

答案 0 :(得分:1)

您尝试设置的内容称为类标签或类值,如文档中所述。您可以使用以下方法 setClassValue(java.lang.Object value) 样本用法是

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

答案 1 :(得分:0)

您可以使用以下构造函数:

public SparseInstance(int noAttributes,
          double defaultValue,
          java.lang.Object classValue)

并使用一个对象来指定标签,它可以是JLabel,String,JButton等任何东西

例如:

Instance tmpInstance = new SparseInstance();

    tmpInstance.put(2, 1.0, "myLabel");

问候