Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #1401

Clustering

Clustering groups data points so that points in the same group are more similar to each other than to points in other groups — with no labels given, no "correct" grouping told to the algorithm in advance. It's the primary technique of unsupervised learning.

Why Group Data With No Labels At All?

Sometimes there's no target variable to predict — you just want to understand a dataset's natural structure. "What are our distinct customer segments?" has no ground-truth answer sitting in a spreadsheet somewhere; clustering discovers a reasonable answer directly from how customers' features naturally group together.

The Three Main Clustering Approaches

ApproachCore IdeaFull Note
Centroid-basedGroup points around learned center pointsK-Means
HierarchicalBuild a tree of nested clusters, merged step by stepHierarchical Clustering
Density-basedGroup points in dense regions, flag sparse regions as noiseDBSCAN

Minimal Working Example

from sklearn.cluster import KMeans
import numpy as np

X = np.array([[2,2],[2,4],[2,6],[8,2],[8,4],[8,6]])   # two obvious groups

model = KMeans(n_clusters=2, random_state=42, n_init=10)
labels = model.fit_predict(X)
print(labels)   # e.g. [0 0 0 1 1 1] -- the algorithm found the two groups with no labels given

Why Clustering Is Harder to Evaluate Than Supervised Learning

Without ground-truth labels, there's no direct "accuracy" to compute. Evaluation instead relies on internal metrics — how tight and well-separated the resulting clusters are — covered in Clustering Evaluation and Silhouette Score — combined with a human sanity check of whether the groups actually make business sense.

Practical Use Cases

  • Customer segmentation for targeted marketing
  • Anomaly/fraud detection — points that don't fit any dense cluster stand out
  • Document/topic grouping with no predefined categories
  • Image compression — grouping similar colors into a smaller palette

Common Mistakes

  • Running any distance-based clustering algorithm on unscaled features — exactly the same scale-sensitivity issue as KNN.
  • Assuming clusters found by an algorithm automatically correspond to meaningful real-world categories — they need business-context validation, not just a good internal metric score.
  • Picking one clustering algorithm without considering that different algorithms make very different structural assumptions — see the comparison in each specific algorithm's note.

Interview Relevance

Q: "How would you validate a clustering result with no ground truth to compare against?" Use internal metrics like silhouette score or within-cluster variance, alongside a domain-expert review of whether the resulting clusters represent something genuinely meaningful and actionable.

Practice Question

A retailer wants to identify distinct shopping behavior groups among 50,000 customers with no predefined categories. Would this be a supervised or unsupervised problem, and which family of clustering algorithm would you start with?

Want to build clustering models on real datasets? CodingNow's Data Science course covers clustering and unsupervised learning with hands-on projects.

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →