2012-11-10 71 views
2

我使用JavaML庫創建了一個SparseInstace對象,我在該實例中放入了一些值,但是我找不到在其上分配標籤的解決方案。例如,創建實例的方式類似於:將標籤(對象名稱)添加到實例對象

{{2=1.0, 4=2.2};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

我如何添加這些標籤(對象名稱)到instaces?

+0

您的意思是「類標籤」,就像機器學習術語中的樣本分類一樣嗎?或者你的意思是「實例名稱」,以便以後可以識別該實例? – Zero3

回答

1

您正在設置的內容稱爲類標籤或類值,如文檔中所述。你可以用下面的方法 setClassValue(java.lang.Object value) 樣品的用法是

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

http://java-ml.sourceforge.net/content/creating-dataset – JoshuaJeanThree

0

您可以使用下面的構造函數:像一個JL​​abel

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

和使用對象指定標籤,它可以是任何東西,字符串,一個JButton

例如:

Instance tmpInstance = new SparseInstance(); 

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

問候

+0

這種方式是不正確的。 Put函數不需要像這樣的參數。 – JoshuaJeanThree

+0

好吧,你有理由,我看你是在將一個SpareInstance投入到一個實例中,也許你可以使用SparseInstace。喜歡它: SparseInstance tmpInstance = new SparseInstance(); tmpInstance.put(2,1.0,「myLabel」); –