sandeshMC

K means algorithm

Apr 16th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. import java.util.Vector;
  5.  
  6. public class KMeans {
  7.  
  8.     static Scanner sc = new Scanner(System.in);
  9.  
  10.     static double dataSet[][];
  11.     static int clusters;
  12.     static double centroids[][];
  13.     static double clusterDistanceArrays[][];
  14.     static Vector al[];
  15.  
  16.     public static void getElements() {
  17.         System.out.println("Enter the number of data elements: ");
  18.         int n = sc.nextInt();
  19.         dataSet = new double[n][];
  20.         System.out.println("Enter the elements : ");
  21.         for (int i = 0; i < dataSet.length; i++) {
  22.             dataSet[i] = new double[2];
  23.             dataSet[i][0] = sc.nextDouble();
  24.             dataSet[i][1] = sc.nextDouble();
  25.         }
  26.     }
  27.  
  28.     public static void setCentroids() {
  29.  
  30.         for (int i = 0; i < clusters; i++) {
  31.  
  32.             centroids[i] = dataSet[i];
  33.         }
  34.     }
  35.  
  36.     public static void printCentroids() {
  37.         int i = 0;
  38.         for (double x[] : centroids) {
  39.  
  40.             System.out.print("centroids " + i + ": ");
  41.             i++;
  42.  
  43.             for (double y : x) {
  44.                 System.out.print(y + " ");
  45.             }
  46.             System.out.println();
  47.         }
  48.  
  49.     }
  50.  
  51.     public static void clusterDistanceArrays() {
  52.         clusterDistanceArrays = new double[clusters][dataSet.length];
  53.     }
  54.  
  55.     public static double findDistance(double a, double b, double x, double y) {
  56.         //return Math.sqrt(Math.pow((2 - 1),2)+Math.pow((3 - 2), 2));
  57.         return Math.sqrt(Math.pow((a - x), 2) + Math.pow((b - y), 2));
  58.     }
  59.  
  60.     public static void calculateDistances() {
  61.  
  62.         for (int i = 0; i < centroids.length; i++) {
  63.             for (int j = 0; j < dataSet.length; j++) {
  64.  
  65.                 clusterDistanceArrays[i][j] = findDistance(dataSet[j][0], dataSet[j][1], centroids[i][0], centroids[i][1]);
  66.             }
  67.         }
  68.     }
  69.  
  70.     public static void printDistances() {
  71.         for (double x[] : clusterDistanceArrays) {
  72.             for (double y : x) {
  73.                 System.out.print(y + " ");
  74.             }
  75.             System.out.println();
  76.         }
  77.  
  78.     }
  79.  
  80.     public static void addIntoClusters() {
  81.         // al = new Vector[clusters];
  82.         initClusters();
  83.         for (int i = 0; i < dataSet.length; i++) {
  84.             int index = 0;
  85.             double smallest = clusterDistanceArrays[0][i];
  86.  
  87.             for (int j = 1; j < centroids.length; j++) {
  88.                 if (smallest >= clusterDistanceArrays[j][i]) {
  89.                     index = j;
  90.                     smallest = clusterDistanceArrays[j][i];
  91.                 }
  92.             }
  93.  
  94.             al[index].addElement(dataSet[i]);
  95.         }
  96.     }
  97.  
  98.     public static void updateCentroids() {
  99.         for (int i = 0; i < al.length; i++) {
  100.             double x[][] = new double[1][2];
  101.             for (int j = 0; j < al[i].size(); j++) {
  102.                 double c[][] = new double[1][2];
  103.                 c[0] = (double[]) al[i].get(j);
  104.                 x[0][0] += c[0][0];
  105.                 x[0][1] += c[0][1];
  106.  
  107.                 //System.out.print(Arrays.toString(c[0]));
  108.             }
  109.             x[0][0] = (double) (x[0][0] / al[i].size());
  110.             x[0][1] = (double) (x[0][1] / al[i].size());
  111.             centroids[i] = x[0];
  112.             //System.out.println(x[0][0]+" "+x[0][1]);
  113.         }
  114.  
  115.     }
  116.  
  117.     public static void printClusters() {
  118.         for (int i = 0; i < al.length; i++) {
  119.             System.out.print("cluster " + i + ": ");
  120.             for (int j = 0; j < al[i].size(); j++) {
  121.                 double c[][] = new double[1][2];
  122.                 c[0] = (double[]) al[i].get(j);
  123.                 System.out.print(Arrays.toString(c[0]));
  124.             }
  125.             System.out.println();
  126.         }
  127.     }
  128.  
  129.     public static void initClusters() {
  130.         al = new Vector[clusters];
  131.         for (int i = 0; i < al.length; i++) {
  132.             al[i] = new Vector(0);
  133.         }
  134.     }
  135.  
  136.     public static void main(String args[]) {
  137.         getElements();
  138.         System.out.println("Enter number of clusters: ");
  139.         clusters = sc.nextInt();
  140.         centroids = new double[clusters][2];
  141.         initClusters();
  142.         setCentroids();
  143.         clusterDistanceArrays();
  144.  
  145.         for (int i = 0; i < 50; i++) {
  146.             calculateDistances();
  147.             addIntoClusters();
  148.             updateCentroids();
  149.         }
  150.  
  151.         System.out.println("The clusters: ");
  152.         printClusters();
  153.         System.out.println("Final Centroids: ");
  154.         printCentroids();
  155.  
  156.     }
  157. }
  158.  
  159. /*
  160. run:
  161. Enter the number of data elements:
  162. 8
  163. Enter the elements :
  164. 2 10
  165. 2 5
  166. 8 4
  167. 5 8
  168. 7 5
  169. 6 4
  170. 1 2
  171. 4 9
  172. Enter number of clusters:
  173. 3
  174. The clusters:
  175. cluster 0: [2.0, 10.0][5.0, 8.0][4.0, 9.0]
  176. cluster 1: [2.0, 5.0][1.0, 2.0]
  177. cluster 2: [8.0, 4.0][7.0, 5.0][6.0, 4.0]
  178. Final Centroids:
  179. centroids 0: 3.6666666666666665 9.0
  180. centroids 1: 1.5 3.5
  181. centroids 2: 7.0 4.333333333333333
  182. BUILD SUCCESSFUL (total time: 1 minute 3 seconds)
  183. */
Advertisement
Add Comment
Please, Sign In to add comment