K-Medians Clustering for Beginners
K-medians is a machine learning algorithm used to group data into clusters, especially useful when your data includes outliers or non-normal distributions.
Unlike K-means (which uses the average), K-medians uses the median value as the center of each cluster — making it more stable and less sensitive to unusual or extreme data points.
1. What Is Clustering?
Clustering is a type of unsupervised learning used to automatically group data points that are similar. It's often used for:
- Pattern recognition
- Customer segmentation
- Anomaly detection
- Recommendation systems
K-medians is especially useful when mean values can be misleading.
2. K-Means vs. K-Medians
| Feature | K-Means | K-Medians |
|---|---|---|
| Center type | Mean (average) | Median (middle value) |
| Outlier handling | Sensitive to extreme values | Robust to outliers |
| Speed | Faster (uses squared distances) | Slower (uses Manhattan distance) |
| Distance metric | Euclidean | Manhattan (L1) |
3. How K-Medians Works
- Choose K (the number of clusters you want)
- Pick random medians as initial centers
- Assign each data point to the nearest median (using Manhattan distance)
- Recalculate the median of each cluster
- Repeat steps 3–4 until clusters stabilize (converge)
4. When to Use K-Medians
- Your data has outliers or skewed values
- You need robust clustering
- You're working with 1D or low-dimensional data
- You want a simpler model than density-based clustering
5. Example Use Case
Suppose you're analyzing transaction amounts from an e-commerce site. Most purchases are normal, but a few are either errors or fraud attempts. Using K-means might pull cluster centers toward the extreme values.
K-medians would give you more representative groups by ignoring those outliers when calculating the center.
6. Limitations
- Slower than K-means on large datasets
- Harder to compute medians in high-dimensional space
- Less popular in standard libraries (but still supported in tools like
scikit-learn-contrib)7. Code Snippet (Python-style Pseudocode)
from sklearn_extra.cluster import KMedians
model = KMedians(n_clusters=3, metric='manhattan')
model.fit(data)
labels = model.labels_
Summary
K-medians is a reliable, robust clustering method when you want clean groups and your data isn’t perfectly clean. It’s especially useful in cybersecurity, finance, or real-world sensor data — any scenario where anomalies matter and means lie.
