2016-03-02 39 views
1

我有一个Java程序,它将使用我从here发现的K-Means算法将坐标聚类为2组。我成功地获得了每个组中的聚集元素,但我不确定如何检索每个聚类的质心位置。检索K-Mean Java程序中每个簇的质心位置

这是我的计划:

import java.util.List; 
import com.aliasi.util.Arrays; 
import java.util.ArrayList; 

public class PrgMain { 
    public static List cent = new ArrayList(); 

    public static void main (String args[]){ 
     List<DataPoint> dataPoints = new ArrayList<DataPoint>(); 
     dataPoints.add(new DataPoint(22,21,"p53")); 
     dataPoints.add(new DataPoint(19,20,"bcl2")); 
     dataPoints.add(new DataPoint(18,22,"fas")); 
     dataPoints.add(new DataPoint(1,3,"amylase")); 
     dataPoints.add(new DataPoint(2,2,"maltase")); 

     JCA jca = new JCA(2,1000,dataPoints); 
     jca.startAnalysis(); 
     int i = 0; 
     Centroid cen = null; 

     for (List<DataPoint> tempV : jca.getClusterOutput()){ 
      System.out.println("-----------Cluster"+(i+1)+"---------"); 
      for (DataPoint dpTemp : tempV){ 
       System.out.println(dpTemp.getObjName()+ 
            "["+dpTemp.getX()+"," +dpTemp.getY()+"]"); 
       cen = new Centroid(dpTemp.getX(), dpTemp.getY()); 
      } 
      i++; 
      System.out.println("Centroid for cluster "+ (i)+": "); 
      // Here's where I want to retrieve the centroid: 
      System.out.println(cen.getCx()+", "+ cen.getCy()); 
     } 
    } 
} 

有没有办法,我可以检索聚类后的重心位置的方法吗?

+0

jca.getCluster [i] .getCentroid()应该是woking – Fabich

+0

谢谢,但是当我尝试输出它时“System.out.println(jca.getCluster(i).getCentroid());” ,我得到的输出就像“Centroid @ 1db9742”。难道我做错了什么? – Cael

+1

cen = jca.getCluster [i] .getCentroid(); System.out.println(cen.getCx()+“,”+ cen.getCy()); – Fabich

回答

1

基于代码here您可以访问心我是这样的:

cen = jca.getCluster[i].getCentroid(); 

,然后打印坐标:

System.out.println(cen.getCx()+", "+ cen.getCy()); 
0

集群后 - jca.startAnalysis(),你需要计算出当前的重心通过使用: (假设你想获得第一簇的质心)

Cluster c1 = jca.getCluster(0); 
Centroid cen1 = new Centroid(0,0); 
cen1.setCluster(c1); 
cen1.calcCentroid(); 
System.out.println("Centroid for cluster 1: "); 
System.out.println(cen1.getCx()+", "+ cen1.getCy());