问题
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