K-Means Clustering from Scratch

MediumClusteringUnsupervised LearningK-meansData VisualizationOptimization

Implement the K-means clustering algorithm from scratch and understand the fundamentals of unsupervised learning, including centroid initialization, cluster assignment, and convergence criteria.

Problem:

Implement the K-means clustering algorithm from scratch and understand the fundamentals of unsupervised learning, including centroid initialization, cluster assignment, and convergence criteria.

Examples:

Input: X = make_blobs(n_samples=300, centers=3)[0]
kmeans = KMeans(k=3)
kmeans.fit(X)
Output: Centroids shape: (3, 2)
Labels: [0, 1, 2]
Inertia: 423.12
Basic K-means clustering on blob dataset
Input: X = make_moons(n_samples=200)
kmeans = KMeans(k=2)
kmeans.fit(X)
Output: Non-spherical clusters detected
Silhouette score: 0.42
K-means limitations on non-spherical data

Constraints:

  • Must handle k > number of data points gracefully
  • Must implement early stopping if centroids converge
  • Must handle empty clusters

Code Editorpython

Run your code to see the output here.

Output

Run your code to see the output here.