2015-02-24 61 views
0

我想執行一個k均值算法無法處理任何類屬性! k均值的java

我在Eclipse中使用此秧雞

我有這樣的代碼

public class demo { 
    public demo() throws Exception { 
     // TODO Auto-generated constructor stub 
     BufferedReader breader = null; 
     breader = new BufferedReader(new FileReader(
       "D:/logiciels/weka-3-7-12/weka-3-7-12/data/iris.arff")); 
     Instances Train = new Instances(breader); 
     Train.setClassIndex(Train.numAttributes() - 1); 
     SimpleKMeans kMeans = new SimpleKMeans(); 
     kMeans.setSeed(10); 
     kMeans.setPreserveInstancesOrder(true); 
     kMeans.setNumClusters(3); 
     kMeans.buildClusterer(Train); 
     int[] assignments = kMeans.getAssignments(); 
     int i = 0; 
     for (int clusterNum : assignments) { 
      System.out.printf("Instance %d -> Cluster %d", i, clusterNum); 
      i++; 
     } 
     breader.close(); 
    } 
    public static void main(String[] args) throws Exception { 
     // TODO Auto-generated method stub 
     new demo(); 
    } 
} 

但有此異常

Exception in thread "main" weka.core.WekaException: weka.clusterers.SimpleKMeans: Cannot handle any class attribute! 
    at weka.core.Capabilities.test(Capabilities.java:1295) 
    at weka.core.Capabilities.test(Capabilities.java:1208) 
    at weka.core.Capabilities.testWithFail(Capabilities.java:1506) 
    at weka.clusterers.SimpleKMeans.buildClusterer(SimpleKMeans.java:595) 
    at wakaproject.demo.<init>(demo.java:24) 
    at wakaproject.demo.main(demo.java:37) 

我已閱讀了一些解決方案,但我不知道問題出在哪裏

感謝您提前

回答

2

錯誤:即SimpleKMeans不能處理類屬性

Exception in thread "main" weka.core.WekaException: weka.clusterers.SimpleKMeans: Cannot handle any class attribute! 

狀態。這是因爲K-means是一種無監督的學習算法,這意味着不應該定義類。然而,代碼中的一行設置了類的值。

如果您修改代碼如下,它的工作原理。

public class demo { 
    public demo() throws Exception { 
     // TODO Auto-generated constructor stub 
     BufferedReader breader = null; 
     breader = new BufferedReader(new FileReader(
       "D:/logiciels/weka-3-7-12/weka-3-7-12/data/iris.arff")); 
     Instances Train = new Instances(breader); 
     //Train.setClassIndex(Train.numAttributes() - 1); // comment out this line 
     SimpleKMeans kMeans = new SimpleKMeans(); 
     kMeans.setSeed(10); 
     kMeans.setPreserveInstancesOrder(true); 
     kMeans.setNumClusters(3); 
     kMeans.buildClusterer(Train); 
     int[] assignments = kMeans.getAssignments(); 
     int i = 0; 
     for (int clusterNum : assignments) { 
      System.out.printf("Instance %d -> Cluster %d", i, clusterNum); 
      i++; 
     } 
     breader.close(); 
    } 
    public static void main(String[] args) throws Exception { 
     // TODO Auto-generated method stub 
     new demo(); 
    } 
}