K-Means Clustering
K-Means Clustering
The most popular unsupervised algorithm to group similar data points into K clusters.
What is K-Means Clustering?
K-Means is an unsupervised Machine Learning algorithm used to divide a dataset into K distinct, non-overlapping clusters based on similarity.
Why is it Called "K-Means"?
- K = the number of clusters you want.
- Means = the average (centroid) of each cluster.
How Does K-Means Work?
Step-by-Step Algorithm
- Choose the number of clusters K.
- Randomly initialize K centroids.
- Assign each data point to its nearest centroid.
- Calculate the mean of each cluster.
- Update centroids using these new means.
- Repeat steps 3–5 until centroids stop changing.
- Final clusters are formed.
Mathematical Formula
K-Means tries to minimize the within-cluster sum of squares (WCSS):
Where:
- K = number of clusters
- Cᵢ = cluster i
- x = data point
- μᵢ = centroid of cluster i
Distance Used in K-Means
K-Means uses Euclidean Distance to measure similarity between data points:
How to Choose the Right K?
Choosing the correct K is one of the most important steps. Common methods are:
Elbow Method
Plot WCSS vs K, choose K at the “elbow point” where the curve bends.
Silhouette Score
Measures how well points fit in their cluster vs other clusters (range: -1 to 1).
Davies-Bouldin Index
Lower value indicates better clustering quality.
Elbow Method — Python Example
pip install numpy matplotlib scikit-learn
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
# Generate sample data
X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.6, random_state=42)
# Compute WCSS for different K
wcss = []
for k in range(1, 11):
km = KMeans(n_clusters=k, random_state=42, n_init=10)
km.fit(X)
wcss.append(km.inertia_)
# Plot Elbow Curve
plt.plot(range(1, 11), wcss, marker="o")
plt.title("Elbow Method")
plt.xlabel("Number of Clusters (K)")
plt.ylabel("WCSS")
plt.show()
Full Python Example — K-Means in Action
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
# Generate dataset
X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.7, random_state=42)
# Apply K-Means with K=4
kmeans = KMeans(n_clusters=4, random_state=42, n_init=10)
kmeans.fit(X)
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
# Visualize
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap="viridis", s=50)
plt.scatter(centroids[:, 0], centroids[:, 1], c="red", marker="X", s=200, label="Centroids")
plt.title("K-Means Clustering")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.legend()
plt.show()
print("Centroids:\n", centroids)
Before vs After K-Means
| Before K-Means | After K-Means |
|---|---|
| Unlabeled scattered points | Organized clusters |
| No pattern visible | Clear grouping |
| Hard to analyze | Easy to interpret |
Real-Life Analogy
K-Means = Party Group Photo
At a party, people automatically gather in small groups based on shared interests (sports, music, food). K-Means works similarly — it groups data points into clusters based on similarity.
Key Properties of K-Means
- Requires specifying K in advance.
- Centroids represent the cluster center.
- Works best with numerical data.
- Sensitive to initialization and outliers.
- Produces spherical, similar-sized clusters.
Initialization Methods
Random Initialization
Centroids are placed randomly. Can lead to poor clustering if unlucky.
K-Means++ (Recommended)
Smartly places centroids far from each other — gives stable results.
KMeans(n_clusters=4, init="k-means++", random_state=42)
Real-World Applications of K-Means
Customer Segmentation
- Group customers by buying patterns
- Targeted marketing
Image Compression
- Group similar pixel colors
- Reduce image size
Document Clustering
- Topic grouping
- News article analysis
Healthcare
- Group patients by symptoms
- Personalized treatment plans
Fraud Detection
- Find unusual clusters
- Suspicious transactions
Recommendation Systems
- Group similar users
- Movie/song suggestions
Telecom
- Group customers by data usage
- Optimize networks
Education
- Group students by performance
- Customized teaching
Advantages of K-Means
- Simple, fast, and easy to implement.
- Works well on large datasets.
- Scales well with high dimensions.
- Produces clear, interpretable clusters.
- Widely supported in ML libraries.
Disadvantages
K-Means vs Other Clustering Algorithms
| Algorithm | Requires K? | Works on Non-Spherical Data? | Outlier Friendly? |
|---|---|---|---|
| K-Means | Yes | No | No |
| K-Medoids | Yes | Partial | Yes |
| DBSCAN | No | Yes | Yes |
| Hierarchical | No | Yes | Partial |
| GMM | Yes | Yes | Partial |
Common Evaluation Metrics
| Metric | Purpose |
|---|---|
| WCSS (Inertia) | Within-cluster sum of squares |
| Silhouette Score | Quality of clustering (−1 to 1) |
| Davies-Bouldin Index | Compactness & separation |
| Elbow Method | Find optimal number of clusters |
Common Mistakes to Avoid
Best Practices
Quick Tips
- Always standardize features.
- Use K-Means++ for better initialization.
- Try Elbow + Silhouette to choose K.
- Visualize using PCA in 2D.
- Remove outliers before clustering.
- Use
n_init=10for stable results.
Importance of K-Means
Most Used Algorithm
- Standard for clustering tasks
- Easy to learn and apply
Fast & Scalable
- Works with millions of rows
- Low computational cost
Business Impact
- Used in marketing analytics
- Customer segmentation
Foundation Algorithm
- Used in many ML pipelines
- Base of advanced clustering
Golden Rule
Key Takeaway
K-Means Clustering is one of the simplest yet most powerful unsupervised algorithms. It groups data points into K clusters using their mean centroids, making it ideal for tasks like customer segmentation, anomaly detection, and pattern discovery in real-world datasets.