public FuzzyCMeansResults calculate() throws FileNotFoundException, UnsupportedEncodingException { membershipMatrix = new MembershipMatrix(numberOfPoints, numberOfClusters); List<Centroid> newCentroids = calculateNewCentroids(); List<Centroid> oldCentroids; double exponent = 2.0 / (fuzziness - 1.0); clock.clockStart(); for (int loop = 0; ; loop++) { prevMembershipMatrix = membershipMatrix; oldCentroids = newCentroids; setInfo("[Fuzzy_C_Means] Loop " + loop); for (int p = 0; p < numberOfPoints; p++) { setInfo("[Fuzzy_C_Means] Loop " + loop + " | point " + p); for (int c = 0; c < numberOfClusters; c++) { setInfo("[Fuzzy_C_Means] Loop " + loop + " | point " + p + " | cluster " + c); double TEMP_denominator = 0; double point_cluster_dist = euclideanDistance(points.get(p), newCentroids.get(c)); double TEMP_nominator = Math.pow((1.0 / point_cluster_dist), exponent); for (int r = 0; r < numberOfClusters; r++) { double summary_point_cluster_dist = euclideanDistance(points.get(p), newCentroids.get(r)); double ratio = Math.pow(point_cluster_dist / summary_point_cluster_dist, exponent); TEMP_denominator += Math.pow((1.0 / summary_point_cluster_dist), exponent); } membershipMatrix.set(p, c, TEMP_nominator / TEMP_denominator); } } newCentroids = calculateNewCentroids(); if (matrixDifferencesSmallerThanEpsilon()) { break; } } double time = clock.clockEnd(); System.out.println(newCentroids); setInfo( "Skin segmented. Fuzz: " + fuzziness + " Eps: " + epsilon + " Pix: " + numberOfPoints + " Time: " + time); return new FuzzyCMeansResults(newCentroids, membershipMatrix); }
private Centroid calculateCentroidOfIndex(int index) { double[] values = new double[pointsDimensions]; double nominator, denominator; for (int dim = 0; dim < pointsDimensions; dim++) { nominator = 0; denominator = 0; for (int i = 0; i < numberOfPoints; i++) { nominator += Math.pow(membershipMatrix.get(i, index), fuzziness) * points.get(i).getDim(dim); denominator += Math.pow(membershipMatrix.get(i, index), fuzziness); } values[dim] = nominator / denominator; } return new Centroid(values); }