2014-11-23 83 views
0

我使用WEKA gui訓練並創建了Nativebases。我將模型文件保存到了我的電腦中,現在我想用它來對我的Java代碼中的單個實例進行分類。我想獲得屬性「集羣」的預測。我做的是以下幾點:使用Java對Weka中的單實例進行分類

package level_4_project_weka; 

import java.util.ArrayList; 
import weka.classifiers.Classifier; 
import weka.core.Attribute; 
import weka.core.DenseInstance; 
import weka.core.Instance; 
import weka.core.Instances; 

public class wekaClass2 { 



private Instance inst_co; 



String classify(
     float area_of_disease1, 
     float length_of_disease1, 
     int s_area_count1, 
     float sd_disease_r1, 
     float sd_disease_g1, 
     float sd_disease_b1, 
     float sd_background_r1, 
     float sd_background_g1, 
     float sd_background_b1, 
     int m_disease_r1, 
     int m_disease_g1, 
     int m_disease_b1, 
     int m_background_r1, 
     int m_background_g1, 
     int m_background_b1, 
     String fluid_filled_blisters1, 
     String feel_itchy1) { 

    String result = null; 
    try { 

     ArrayList<Attribute> attributeList = new ArrayList<>(17); 

     Attribute area_of_disease = new Attribute("area_of_disease"); 
     Attribute length_of_disease = new Attribute("length_of_disease"); 
     Attribute s_area_count = new Attribute("s_area_count"); 
     Attribute sd_disease_r = new Attribute("sd_disease_r"); 
     Attribute sd_disease_g = new Attribute("sd_disease_g"); 
     Attribute sd_disease_b = new Attribute("sd_disease_b"); 
     Attribute sd_background_r = new Attribute("sd_background_r"); 
     Attribute sd_background_g = new Attribute("sd_background_g"); 
     Attribute sd_background_b = new Attribute("sd_background_b"); 
     Attribute m_disease_r = new Attribute("m_disease_r"); 
     Attribute m_disease_g = new Attribute("m_disease_g"); 
     Attribute m_disease_b = new Attribute("m_disease_b"); 
     Attribute m_background_r = new Attribute("m_background_r"); 
     Attribute m_background_g = new Attribute("m_background_g"); 
     Attribute m_background_b = new Attribute("m_background_b"); 
     Attribute fluid_filled_blisters = new Attribute("fluid_filled_blisters"); 
     Attribute feel_itchy = new Attribute("feel_itchy"); 

     ArrayList<String> classVal = new ArrayList<>(); 
     classVal.add("melanoma"); 
     classVal.add("eczema"); 
     classVal.add("impetigo"); 
     //classVal.add("ClassB"); 


     attributeList.add(area_of_disease); 
     attributeList.add(length_of_disease); 
     attributeList.add(s_area_count); 
     attributeList.add(sd_disease_r); 
     attributeList.add(sd_disease_g); 
     attributeList.add(sd_disease_b); 
     attributeList.add(sd_background_r); 
     attributeList.add(sd_background_g); 
     attributeList.add(sd_background_b); 
     attributeList.add(m_disease_r); 
     attributeList.add(m_disease_g); 
     attributeList.add(m_disease_b);    
     attributeList.add(m_background_r); 
     attributeList.add(m_background_g); 
     attributeList.add(m_background_b); 
     attributeList.add(fluid_filled_blisters); 
     attributeList.add(feel_itchy); 

     attributeList.add(new Attribute("@@[email protected]@",classVal)); 

     Instances data = new Instances("TestInstances",attributeList,0); 


     // Create instances for each pollutant with attribute values latitude, 
     // longitude and pollutant itself 
     inst_co = new DenseInstance(data.numAttributes()); 
     data.add(inst_co); 

     // Set instance's values for the attributes "latitude", "longitude", and 
     // "pollutant concentration" 
     inst_co.setValue(area_of_disease,area_of_disease1); 
     inst_co.setValue(length_of_disease,length_of_disease1); 
     inst_co.setValue(s_area_count,s_area_count1); 
     inst_co.setValue(sd_disease_r,sd_disease_r1); 
     inst_co.setValue(sd_disease_g,sd_disease_g1); 
     inst_co.setValue(sd_disease_b,sd_disease_b1); 
     inst_co.setValue(sd_background_r,sd_background_r1); 
     inst_co.setValue(sd_background_g,sd_background_g1); 
     inst_co.setValue(sd_background_b,sd_background_b1); 
     inst_co.setValue(m_disease_r,m_disease_r1); 
     inst_co.setValue(m_disease_g,m_disease_g1); 
     inst_co.setValue(m_disease_b,m_disease_b1);    
     inst_co.setValue(m_background_r,m_background_r1); 
     inst_co.setValue(m_background_g,m_background_g1); 
     inst_co.setValue(m_background_b,m_background_b1); 
     inst_co.setValue(fluid_filled_blisters,fluid_filled_blisters1); 
     inst_co.setValue(feel_itchy,feel_itchy1); 
     // inst_co.setMissing(cluster); 

     // load classifier from file 
     Classifier cls_co = (Classifier) weka.core.SerializationHelper 
       .read("C:/Users/Lahiru/Documents/NetBeansProjects/level_4_project_weka/diseases.model"); 

     double result1 = cls_co.classifyInstance(inst_co); 
     System.out.println("a" + result1); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return result; 
    } 


    } 

,但是當我加入

 `inst_co.setValue(fluid_filled_blisters,fluid_filled_blisters1); 
     inst_co.setValue(feel_itchy,feel_itchy1);` 

我得到了下面的錯誤。

java.lang.IllegalArgumentException: Attribute neither nominal nor string! 
at weka.core.AbstractInstance.setValue(AbstractInstance.java:518) 
at level_4_project_weka.wekaClass2.classify(wekaClass2.java:112) 
at level_4_project_weka.Level_4_project_weka.PredictDisease(Level_4_project_weka.java:102) 
at level_4_project_weka.Level_4_project_weka.main(Level_4_project_weka.java:27) 

我知道,這個錯誤是因爲這兩個字符串變量而發生的。但我不知道如何處理字符串。任何人都可以告訴我正確的方法來做到這一點?

+0

[創造WEKA的Java API的字符串的屬性(可能重複http://stackoverflow.com/questions/6639802/creating-a-string-attribute-in-weka- java-api) – 2014-11-24 08:39:13

回答

0

見Weka的文檔:

http://weka.wikispaces.com/Creating+an+ARFF+file

詳細介紹瞭如何創建具有正確的類型信息的屬性。

參見: creating a string attribute in weka java API

+0

不,它不是。因爲我也看到了這個鏈接,但這是一個老辦法。與weka.jar新版本鞋FastVactors作爲貶值。所以這就是爲什麼我把這個問題放在這裏得到新的答案。謝謝你的回答。我期待更好ona – user3565768 2014-11-24 16:06:55

+0

新Weka罐建議使用什麼? (但它仍然是同樣的問題,但是......只需要一個更新的答案...) – 2014-11-24 18:18:47

+0

(據我所知,它仍然以同樣的方式;除了你也可以使用'ArrayList'而不是'FastVector '...) – 2014-11-24 18:22:23

相關問題