我有一个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());
}
}
}
有没有办法可以在聚类后检索质心位置?
答案 0 :(得分:1)
根据代码here,您可以通过以下方式访问质心:
cen = jca.getCluster[i].getCentroid();
然后打印坐标:
System.out.println(cen.getCx()+", "+ cen.getCy());
答案 1 :(得分: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());