gpt4 book ai didi

java - 简单的 K-Means 不处理 iris.arff

转载 作者:搜寻专家 更新时间:2023-11-01 01:29:30 24 4
gpt4 key购买 nike

我在下面有这个类,我根据 wiki 和论文中给出的示例构建它,为什么 SympleKMeans 不能处理数据?该类可以打印Datasource dados,所以处理文件没有问题,错误在构建上。

package slcct;

import weka.clusterers.ClusterEvaluation;
import weka.clusterers.SimpleKMeans;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;


public class Cluster {

public String path;
public Instances dados;
public String[] options = new String[2];

public Cluster(String caminho, int nclusters, int seed ){
this.path = caminho;
this.options[0] = String.valueOf(nclusters);
this.options[1] = String.valueOf(seed);

}

public void ledados() throws Exception{

DataSource source = new DataSource(path);
dados = source.getDataSet();
System.out.println(dados)

if(dados.classIndex()==-1){
dados.setClassIndex(dados.numAttributes()-1);
}
}

public void imprimedados(){
for(int i=0; i<dados.numInstances();i++)
{
Instance actual = dados.instance(i);
System.out.println((i+1) + " : "+ actual);
}
}

public void clustering() throws Exception{

SimpleKMeans cluster = new SimpleKMeans();
cluster.setOptions(options);
cluster.setDisplayStdDevs(true);
cluster.getMaxIterations();
cluster.buildClusterer(dados);

Instances ClusterCenter = cluster.getClusterCentroids();
Instances SDev = cluster.getClusterStandardDevs();
int[] ClusterSize = cluster.getClusterSizes();

ClusterEvaluation eval = new ClusterEvaluation();
eval.setClusterer(cluster);
eval.evaluateClusterer(dados);

for(int i=0;i<ClusterCenter.numInstances();i++){
System.out.println("Cluster#"+( i +1)+ ": "+ClusterSize[i]+" dados .");
System.out.println("Centróide:"+ ClusterCenter.instance(i));
System.out.println("STDDEV:" + SDev.instance(i));
System.out.println("Cluster Evaluation:"+eval.clusterResultsToString());

}

}
}

错误:

weka.core.WekaException: weka.clusterers.SimpleKMeans: Cannot handle any class attribute!

at weka.core.Capabilities.test(Capabilities.java:1097)
at weka.core.Capabilities.test(Capabilities.java:1018)
at weka.core.Capabilities.testWithFail(Capabilities.java:1297)
at weka.clusterers.SimpleKMeans.buildClusterer(SimpleKMeans.java:228)
at slcct.Cluster.clustering(Cluster.java:53)//Here.
at slcct.Clustering.jButton1ActionPerformed(Clustering.java:104)

最佳答案

我相信你不需要设置类索引,因为你在做聚类而不是分类。尝试关注 this guide for programmatic Java clustering .

关于java - 简单的 K-Means 不处理 iris.arff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5990267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com