Search code examples
pythonpython-3.xscikit-learncluster-analysisk-means

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


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')

Solution

  • 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