在 K-Mean Java 程序中检索每个簇的质心位置

Retrieving centroid position of each cluster in a K-Mean Java program

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

这是我的程序:

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

有没有办法在聚类后检索质心位置?

基于代码 here 你可以这样访问质心 i :

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

然后打印坐标:

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

聚类后 - 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());