Created
June 3, 2022 06:38
-
-
Save Abhayparashar31/c01583f56108537dd66b83f2cb93d4a8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from sklearn.cluster import KMeans | |
wcss = [] | |
silhouette_scores = [] | |
calinski_harabasz_scores = [] | |
davies_bouldin_scores = [] | |
for i in range(2, 7): | |
kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 42) | |
y_pred = kmeans.fit_predict(X) | |
wcss.append(kmeans.inertia_) | |
silhouette_scores.append(silhouette_score(X,y_pred)) | |
calinski_harabasz_scores.append(calinski_harabasz_score(X,y_pred)) | |
davies_bouldin_scores.append(davies_bouldin_score(X,y_pred)) | |
print(f'WCSS At k = {i}: ',kmeans.inertia_) | |
print('Silhouette Cofficient: ',silhouette_score(X,y_pred)) | |
print('Calinski-Harabasz Index: ',calinski_harabasz_score(X,y_pred)) | |
print('Davies-Bouldin Index: ',davies_bouldin_score(X,y_pred)) | |
print('--------------------------------') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment