Created
March 18, 2021 11:26
-
-
Save cydal/f954f3f0cf19d315c00b41c108f3d919 to your computer and use it in GitHub Desktop.
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
from sklearn.cluster import KMeans | |
from sklearn.metrics import silhouette_score | |
sum_of_sq_distance = [] | |
silhouette_score_list = [] | |
K = range(2, 80) | |
## Loop through K and calculate silhouette score | |
for k in K: | |
print("KMeans - Num Cluster - ", k) | |
km = KMeans(n_clusters=k, random_state=0).fit(x_train_lsa) | |
sum_of_sq_distance.append(km.inertia_) | |
silhouette_score_list.append(silhouette_score(x_train_lsa, km.labels_)) | |
# Plot sum of sq distance error | |
plt.plot(K, sum_of_sq_distance, 'bx-') | |
plt.xlabel('k') | |
plt.ylabel('Sum of Squared distances') | |
plt.title("Elbow Method") | |
plt.show() | |
# Plot sum of silhouette score | |
plt.plot(K, silhouette_score_list, 'bx-') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment