def affectation(centroids, points): """ To affect data (points) to centroids of clusters to form new cluster @type centroids: list of lists @param centroids: list of lists containing coordinates of centroids @type points: list of lists @param points: list of lists containing coordianates of data (points) @type clusters[:]: list of lists of lists @return clusters[:]: list of lists of lists containing groups of points. Each group is equivalent to a cluster. """ clusters = [] for i in range(len(centroids)): clusters.append([]) for point in points: distance_to_centroid = distance.EuclideanDistance(point[:-1], centroids[0][:-1]) count = 0 index = 0 for centroid in centroids[1:]: distance_min = distance_to_centroid distance_to_centroid = distance.EuclideanDistance(point[:-1], centroid[:-1]) if distance_min > distance_to_centroid: count += 1 distance_min = distance_to_centroid index = count else: count += 1 clusters[index].append(point) return clusters[:]
def variations(previous_centroids,centroids, epsilon = 0.001): # Here we choice epsilon handling algorithm's convergence. """ To verify whether clustering converged @type previous_centroids: list of list @param previous_centroids: list of centroids before update @type centroids: list of list @param centroids: list of updated centroids @type epsilon: float @param epsilon: parameter that is compared to the difference between previous_centroids and centroids @type sqrt(var) < epsilon: bool @return sqrt(var) < epsilon: indicates whether clustering converged or not """ var = 0 for i in range (len(centroids)): var += (distance.EuclideanDistance(centroids[i],previous_centroids[i]))**2 return sqrt(var) < epsilon