Created
May 19, 2021 09:05
-
-
Save SolClover/b743b226fd2d7e1ffc8cfe66671f32fd to your computer and use it in GitHub Desktop.
Plot Silhouette scores for GMM models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create empty list | |
S=[] | |
# Range of clusters to try (2 to 10) | |
K=range(2,11) | |
# Select data for clustering model | |
X = df_loc[['Latitude', 'Longitude']] | |
for k in K: | |
# Set the model and its parameters | |
model = GaussianMixture(n_components=k, n_init=20, init_params='kmeans') | |
# Fit the model | |
labels = model.fit_predict(X) | |
# Calculate Silhoutte Score and append to a list | |
S.append(metrics.silhouette_score(X, labels, metric='euclidean')) | |
# Plot the resulting Silhouette scores on a graph | |
plt.figure(figsize=(16,8), dpi=300) | |
plt.plot(K, S, 'bo-', color='black') | |
plt.xlabel('k') | |
plt.ylabel('Silhouette Score') | |
plt.title('Identify the number of clusters using Silhouette Score') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment