Cannot handle any class attribute! kmeans java

跟風遠走 提交于 2019-12-12 10:24:41

问题


I want execute a k-means algorithm

i use for this weka in eclipse

i have this code

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();
    }
}

but a have this exception

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)

i have read some solutions but i don't undsund where is th e problem

thanks you in advance


回答1:


The error:

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

states that SimpleKMeans cannot handle a class attribute. This is because K-means is an unsupervised learning algorithm, meaning that there should be no class defined. Yet, one line in the code sets the class value.

If you modify the code as follows, it works.

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();
    }
}


来源:https://stackoverflow.com/questions/28706595/cannot-handle-any-class-attribute-kmeans-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!