1
votes

I have a Java Program that will cluster coordinates into 2 groups using K-Means algorithm which I found from here. I have succeeded in getting the clustered elements in each group, but I'm not sure how to retrieve the centroid position of each cluster.

This is my program:

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

Is there a way that I can retrieve the centroid position after clustering?

2
jca.getCluster[i].getCentroid() should be woking - Fabich
Thanks but when I try to output it "System.out.println(jca.getCluster(i).getCentroid());" , I get output like "Centroid@1db9742". Am I doing something wrong? - Cael
cen = jca.getCluster[i].getCentroid(); System.out.println(cen.getCx()+", "+ cen.getCy()); - Fabich

2 Answers

1
votes

Based on the code here you can access the centroid i this way :

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

And then print the coordinates :

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

after clustering - jca.startAnalysis(), you need to calculate the current centroid by using: (suppose you want to get the centroid of the first cluster)

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