Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Volume1/Chapter7/k_means_clustering/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
What it does :

1. This code generates random data in 2D and runs the k-means algorithm on the dataset using the value of k provided by the user.
1. This code generates random data in 2D and runs the k-means algorithm on the dataset using the value of k provided by the user. Then calculates the Silhouette score.

Dependancies :

Expand Down
129 changes: 84 additions & 45 deletions Volume1/Chapter7/k_means_clustering/k_means_clustering.ipynb

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions Volume1/Chapter7/k_means_clustering/k_means_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
# Import kmeans module
from sklearn.cluster import KMeans

# Import silhouette_score function
from sklearn.metrics import silhouette_score

# ------------------------------------ Generate and plot the 2D dataset -------------------------------------------

# Generate the two dimensional dataset
Expand Down Expand Up @@ -59,6 +62,14 @@
# Show the clusters with their centroids
plt.show()

# --------------------------------------- Calculate the Silhoutte Score --------------------------------------------

# Calculate Silhoutte Score
silhoutte_score = silhouette_score(X, kmeans.labels_, metric='euclidean')

# Print the Silhoutte score
print ('Silhouetter Score:', silhoutte_score)