def calculate_distance_stats(elements, matrix): """ Calculates the mean, dispersion and radius of all the distances to the central element of a set of elements. @param elements: The elements we are working with. @param matrix: The used condensed matrix. @return: Mean, std deviation and radius of all the elements with respect to their central element. """ cluster = Cluster(None, elements) medoid = cluster.calculate_medoid(matrix) # We also get a 0 distance from the medoid vs itself (it is contained in 'elements') distances = get_distances_of_elements_to(medoid, elements, matrix) return numpy.mean(distances), numpy.std(distances), numpy.max(distances)
def evaluate(self, clustering, matrix): """ Mean is approximated to medoid. """ update_medoids(clustering, matrix) global_cluster = Cluster(None, clustering.get_all_clustered_elements()) global_cluster.prototype = global_cluster.calculate_medoid(matrix) global_variance = numpy.var(get_distances_of_elements_to(global_cluster.prototype, global_cluster.all_elements, matrix)) variances = [self.cluster_variance(cluster,matrix) for cluster in clustering.clusters] sum_ci = numpy.sum(variances) Cmp = sum_ci / (len(clustering.clusters)*global_variance) return Cmp
def calculate_mean_center_differences(decomposed_cluster, matrix): """ Given a mixed decomposed cluster, it calculates the mean of all center differences (giving a qualitative view of how separated the inner subclusters are). @param decomposed_cluster: A MIXED decomposed cluster. @param matrix: The condensed distance matrix used. @return: The mean of center distances. """ centers = [] for traj_id in decomposed_cluster: cluster = Cluster(None, decomposed_cluster[traj_id]) centers.append(cluster.calculate_medoid(matrix)) center_distances = [] for i in range(len(centers)-1): for j in range(i+1, len(centers)): center_distances.append(matrix[centers[i],centers[j]]) return numpy.mean(center_distances)