从wekaUI调用kmeans算法后,最后生成的的结果文件中,聚类中心向量表达的不够清晰。参照网上的做法,采用java code中调用weka中的kmeans算法。 这段代码在网络上转载颇多,一时找不到出处。代码如下
:
java code中调用kmeans聚类
import java.io.*;
import weka.clusterers.SimpleKMeans;
import weka.core.DistanceFunction;
//import weka.core.EuclideanDistance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
public class SimpleCluster {
public static void main(String[] args) {
// TODO Auto-generated method stub
Instances ins = null;
Instances tempIns = null;
SimpleKMeans KM = null;
//DistanceFunction disFun = null;
try{
File file= new File("F:\\cluster\\tobeClustered.arff");
ArffLoader loader = new ArffLoader();
loader.setFile(file);
ins = loader.getDataSet();
KM = new SimpleKMeans();
//设置聚类要得到的类别数量
KM.setNumClusters(10);
KM.buildClusterer(ins);
tempIns = KM.getClusterCentroids();
System.out.println("聚类中心");
System.out.println("CentroIds: " + tempIns);
}catch(Exception e){
e.printStackTrace();
}
}
}
import weka.clusterers.SimpleKMeans;
import weka.core.DistanceFunction;
//import weka.core.EuclideanDistance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
public class SimpleCluster {
public static void main(String[] args) {
// TODO Auto-generated method stub
Instances ins = null;
Instances tempIns = null;
SimpleKMeans KM = null;
//DistanceFunction disFun = null;
try{
File file= new File("F:\\cluster\\tobeClustered.arff");
ArffLoader loader = new ArffLoader();
loader.setFile(file);
ins = loader.getDataSet();
KM = new SimpleKMeans();
//设置聚类要得到的类别数量
KM.setNumClusters(10);
KM.buildClusterer(ins);
tempIns = KM.getClusterCentroids();
System.out.println("聚类中心");
System.out.println("CentroIds: " + tempIns);
}catch(Exception e){
e.printStackTrace();
}
}
}