Skip to content

Instantly share code, notes, and snippets.

@isaacarroyov
Created November 20, 2020 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacarroyov/a03a58fe79dd6657eead2966fe7d65f3 to your computer and use it in GitHub Desktop.
Save isaacarroyov/a03a58fe79dd6657eead2966fe7d65f3 to your computer and use it in GitHub Desktop.
application of the silhouette method
from sklearn.metrics import silhouette_samples, silhouette_score
#create a list of K-Means models. Each element is a KMeans model with a specific number of clusters
kmeans2_sil= [KMeans(n_clusters=i, random_state=11, init='k-means++').fit(df_standardized) for i in range(1,15)]
#calculamos el silhouette score a cada modelo (de diferente número de clusters)
sil_values = [ silhouette_score(df_standardized, model.labels_, random_state=11)
for model in kmeans2_sil[1:]
]
#save the silhouette coefficient in an array
sil_values = np.array( sil_values )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment