def single_silhouette_dendrogram(dist_matrix, Z, threshold, mode='clusters',
method='single', sample_names=None):
"""Compute the average silhouette at a given threshold.
Parameters
----------
dist_matrix : array-like
Precomputed distance matrix between points.
Z : array-like
Linkage matrix, results of scipy.cluster.hierarchy.linkage.
threshold : float
Specifies where to cut the dendrogram.
mode : ('clusters', 'thresholds'), optional
Choose what to visualise on the x-axis.
Returns
-------
x : float
Based on mode, it can contains the number of clusters or threshold.
silhouette_avg : float
The average silhouette.
"""
cluster_labels = fcluster(Z, threshold, 'distance')
nclusts = np.unique(cluster_labels).shape[0]
save_results_clusters("res_{}_{:03d}_clust.csv".format(method, nclusts),
sample_names, cluster_labels)
try:
silhouette_list = silhouette_samples(dist_matrix, cluster_labels,
metric="precomputed")
silhouette_avg = np.mean(silhouette_list)
x = max(cluster_labels) if mode == 'clusters' else threshold
except ValueError as e:
if max(cluster_labels) == 1:
x = 1 if mode == 'clusters' else threshold
silhouette_avg = 0
else:
raise(e)
return x, silhouette_avg
评论列表
文章目录