How to calculate the Silhouette Score for each cluster separately in python
Asked Answered
F

1

6

You can easily extract the silhouette score with 1 line of code that averages the scores for all your clusters but how do you extract each of the intermediate scores from the scikit learn implementation of the silhouette score? I want to be able to extract this same score for each cluster individually, not only get the total score.

metrics.silhouette_score(x, y, metric='euclidean')
Flowerlike answered 26/1, 2020 at 15:8 Comment(0)
C
8

If your data looks something like this:

num_clusters = 3
X, y = datasets.load_iris(return_X_y=True)
kmeans_model = KMeans(n_clusters=num_clusters, random_state=1).fit(X)
cluster_labels = kmeans_model.labels_

You could use metrics.silhouette_samples to compute the silhouette coefficients for each sample, then take the mean of each cluster:

sample_silhouette_values = metrics.silhouette_samples(X, cluster_labels)

means_lst = []
for label in range(num_clusters):
    means_lst.append(sample_silhouette_values[cluster_labels == label].mean())

print(means_lst)                                                                             
[0.4173199215409322, 0.7981404884286224, 0.45110506043401194] # 1 mean for each of the 3 clusters
Cochard answered 26/1, 2020 at 16:48 Comment(2)
When I try to run the code you have provided, I get IndexError: too many indices for array, not sure why? The X and cluster_labels are just the dataframe with all the independent variables for X and dependent variable for cluster_labels right?Flowerlike
Make sure the X and clusterlabels variables are numpy arrays, instead of dataframes. Did you receive the IndexError when running the code using the sample iris dataset?Cochard

© 2022 - 2024 — McMap. All rights reserved.