concept Silhouette Score in category machine learning

appears as: Silhouette Score
How Machine Learning Works MEAP V05

This is an excerpt from Manning's book How Machine Learning Works MEAP V05.

from sklearn.metrics import silhouette_score
 
silhouette_scores = []
for k in range(2, 11):
    model = KMeans(n_clusters=k, random_state=42)
    model.fit(scaled_X)
    silhouette_scores.append(silhouette_score(scaled_X, model.labels_))
   
best_k = np.argmax(silhouette_scores) + 2
best_silhouette = np.max(silhouette_scores)
print("Best k is: {}, with Silhouette Score: {:.2f}".format(best_k, best_silhouette))
 
plt.plot(np.arange(2, 11), silhouette_scores)
plt.xlabel("Number of Clusters")
plt.ylabel("Silhouette Score")
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest