Skip to content

Instantly share code, notes, and snippets.

@KhyatiMahendru
Created June 17, 2019 06:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save KhyatiMahendru/2f7ff7c17ea13a28d5023f3aefa047e2 to your computer and use it in GitHub Desktop.
Save KhyatiMahendru/2f7ff7c17ea13a28d5023f3aefa047e2 to your computer and use it in GitHub Desktop.
from sklearn.metrics import silhouette_score
sil = []
kmax = 10
# dissimilarity would not be defined for a single cluster, thus, minimum number of clusters should be 2
for k in range(2, kmax+1):
kmeans = KMeans(n_clusters = k).fit(x)
labels = kmeans.labels_
sil.append(silhouette_score(x, labels, metric = 'euclidean'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment