2015-02-08 20 views
0

我有兩個Instances數據集:data是原始和是我試圖複製類值的人。這裏是我的代碼:如何複製weka中的數據集的類值?

FastVector attributes = new FastVector(); 
ArrayList<Instance> instances = new ArrayList<Instance>(); 
for(int i = 0; i <= 100; i++){ 
    Attribute newAttr = new Attribute("Stump" + i, i); 
    attributes.addElement(newAttr); 
} 
//make new instances 
Instances stumpyInsts = new Instances("Stumps", attributes, data.numInstances()); 

stumpyInsts.setClassIndex(stumpyInsts.numAttributes() - 1); 
Enumeration instEnum = stumpyInsts.enumerateInstances(); 
Enumeration somethingElseLOL = data.enumerateInstances(); 
while (instEnum.hasMoreElements()) { 
    Instance instance = (Instance) instEnum.nextElement(); 
    Instance other = (Instance) somethingElseLOL.nextElement(); 
    String s = other.stringValue(other.classIndex()); 
    instance.setValue(instance.classIndex(), s); 
} 

我不斷收到這個當我嘗試設置的值:

`java.lang.IllegalArgumentException: Attribute neither nominal nor string! 
at weka.core.Instance.setValue(Instance.java:687)` 

有誰知道爲什麼會發生?如果我在將實例添加到數據集之前嘗試執行setClassValue,或者如果使用這些值創建新字符串,我也會得到該錯誤。對我來說這並不合理,因爲stringValue顯然是返回一個字符串。

回答

1

java.lang.IllegalArgumentException: Attribute neither nominal nor string! 
    at weka.core.Instance.setValue(Instance.java:687) 

並不是指stringValue,但到instance類屬性的錯誤。當你做

stumpyInsts.setClassIndex(stumpyInsts.numAttributes() - 1); 

你告訴它的類是什麼指標,但不是說是應該是一個名義或字符串屬性。據this answer你會做這樣的事情

FastVector classAttr = new FastVector(); 
classAttr .addElement(new Attribute("class", (FastVector) null)); 

創建一個(類)的屬性,具有字符串或標稱值。

+0

我添加了我的代碼的前一部分,當我複製粘貼它時,我看到了我的錯誤所在。謝謝。 – RockOnRockOut 2015-02-08 05:27:55

+0

我的編輯描述了您的解決方案嗎?如果不是這樣,那麼把它包含在這裏是很好的,這樣其他具有相同問題的人就可以得到幫助。 – Sentry 2015-02-08 10:55:46

+0

差不多。我做了一個單獨的'FastVector標籤'並將兩個類標籤添加到它,然後將其用於class屬性。但是,謝謝你,你的原始答案幫助了我。 – RockOnRockOut 2015-02-08 18:44:46