Juan1997xd
Juan1997xd

Reputation: 3

Algorithm K-Means

Friends, I hope your help, I am developing the K-Means algorithm in Java, so I have already performed for cycles to do the respective centroid operations with the Data and determine the distances, but the distances of each Centroid are in different for cycles. Now what I require is to put all those results in oneself outside the cycles. I enclose part of the code.

 System.out.println("\n" + "----------" + "\n" + "Cluster K" + "\n" + "----------");
        System.out.println();
        for (int i = 0; i < Datos.length; i++) {
            distanciaK = (float) Math.sqrt(Math.pow(Datos[i][0] - Datos[10][0], 2) + Math.pow(Datos[i][1] - Datos[10][1], 2) + Math.pow(Datos[i][2] - Datos[10][2], 2) + Math.pow(Datos[i][3] - Datos[10][3], 2) + Math.pow(Datos[i][4] - Datos[10][4], 2));
            System.out.println(distanciaK);

        }

        System.out.println("\n" + "----------" + "\n" + "Cluster M" + "\n" + "----------");
        System.out.println();
        for (int i = 0; i < Datos.length; i++) {
            distanciaM = (float) Math.sqrt(Math.pow(Datos[i][0] - Datos[12][0], 2) + Math.pow(Datos[i][1] - Datos[12][1], 2) + Math.pow(Datos[i][2] - Datos[12][2], 2) + Math.pow(Datos[i][3] - Datos[12][3], 2) + Math.pow(Datos[i][4] - Datos[12][4], 2));
            System.out.println(distanciaM);

        }

        System.out.println();

Code Output

Cluster K

9.0 8.485281 9.380832 6.3245554 4.2426405 3.6055512 7.615773 8.83176 2.828427 5.8309517 0.0 4.7958317 5.477226 5.196152


Cluster M

5.196152 4.8989797 9.486833 6.928203 4.8989797 3.8729835 7.071068 7.071068 3.1622777 4.2426405 5.477226 6.708204 0.0 1.0

Upvotes: 0

Views: 46

Answers (1)

QuickSilver
QuickSilver

Reputation: 4045

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Import above packages Created Map with as cluster name and value as list representing the cluster. Adding the means to the respective map entry.

System.out.println("\n" + "----------" + "\n" + "Cluster K" + "\n" + "----------");
        System.out.println();
        Map<String,List<Float>> clusterMeanInfo = new HashMap<>();
        clusterMeanInfo.put("Cluster K",new ArrayList<>());
        clusterMeanInfo.put("Cluster M",new ArrayList<>());
        for (int i = 0; i < Datos.length; i++) {
            distanciaK = (float) Math.sqrt(Math.pow(Datos[i][0] - Datos[10][0], 2) + Math.pow(Datos[i][1] - Datos[10][1], 2) + Math.pow(Datos[i][2] - Datos[10][2], 2) + Math.pow(Datos[i][3] - Datos[10][3], 2) + Math.pow(Datos[i][4] - Datos[10][4], 2));
            System.out.println(distanciaK);
            clusterMeanInfo.get("Cluster K").add(distanciaK);

        }

        System.out.println("\n" + "----------" + "\n" + "Cluster M" + "\n" + "----------");
        System.out.println();
        for (int i = 0; i < Datos.length; i++) {
            distanciaM = (float) Math.sqrt(Math.pow(Datos[i][0] - Datos[12][0], 2) + Math.pow(Datos[i][1] - Datos[12][1], 2) + Math.pow(Datos[i][2] - Datos[12][2], 2) + Math.pow(Datos[i][3] - Datos[12][3], 2) + Math.pow(Datos[i][4] - Datos[12][4], 2));
            System.out.println(distanciaM);
            kMeans.add(distanciaM);
            clusterMeanInfo.get("Cluster K").add(distanciaM);
        }

        for (String key :clusterMeanInfo.keySet()) {
            System.out.print(key + ' ');
            clusterMeanInfo.get(key).forEach( value -> {
                System.out.print(value + ' ');
            });
            System.out.println();
        }

Upvotes: 1

Related Questions