无法处理任何 class 属性!均值 java

Cannot handle any class attribute! kmeans java

我想执行一个 k-means 算法

我在 eclipse 中使用这个 weka

我有这个代码

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)

我已经阅读了一些解决方案,但我不明白问题出在哪里

提前致谢

错误:

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

声明 SimpleKMeans 无法处理 class 属性。这是因为 K-means 是一种无监督学习算法,这意味着不应该定义 class。然而,代码中的一行设置了 class 值。

如果您按如下方式修改代码,就可以了。

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