import numpy as np
from sklearn import datasets
from sklearn.metrics import pairwise_distances
from sklearn.preprocessing import LabelEncoder
def silhouette_samples(X, labels, metric='euclidean', **kwds):
le = LabelEncoder()
labels = le.fit_transform(labels)
unique_labels = le.classes_
distances = pairwise_distances(X, metric=metric, **kwds)
intra_clust_dists = np.ones(distances.shape[0], dtype=distances.dtype)
inter_clust_dists = np.inf * intra_clust_dists
for curr_label in unique_labels:
mask = curr_label == labels
current_distances = distances[mask]
n_samples_curr_lab = np.sum(mask) - 1
if n_samples_curr_lab != 0:
intra_clust_dists[mask] = np.sum(current_distances[:, mask], axis=1) / n_samples_curr_lab
for other_label in unique_labels:
if other_label != curr_label:
Silhouettes:聚类结果衡量指标
最新推荐文章于 2022-02-24 00:11:57 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)