zoukankan      html  css  js  c++  java
  • 【转载】java 代码调用kmeans进行聚类

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

               }

            }


        

    }
  • 相关阅读:
    默哀STAND SILENTLY!
    用虚拟机优化Windows(update:2008.4.24)
    UE的心情指数?
    God of War III 的发售日期?
    2009/8/15应该是一个愉快的夜晚.为林肯公园中国10月演唱会做好准备
    北京2008奥运会完美谢幕!
    《The Pursuit of Happyness / 当幸福来敲门》(2006)
    2007林肯公园上海演唱会观后感(实况像片/MP3) update:2008.1.31
    2008早上好
    Active Object C++智能指针实现
  • 原文地址:https://www.cnblogs.com/finallyliuyu/p/1968147.html
Copyright © 2011-2022 走看看