Most beginner machine learning problems are supervised: you have labels, such as “churned” or “stayed”, and the model learns to predict them. Clustering is different. There are no labels; the goal is to find natural groups in the data. K-means is the most widely used clustering method and a common interview topic.
How k-means works
- Choose k, the number of clusters you want.
- Place k starting points (centroids), usually at random.
- Assign every data point to its nearest centroid.
- Move each centroid to the average position of the points assigned to it.
- Repeat steps 3 and 4 until the centroids stop moving.
The result is k groups where points in the same group are close to each other.
A business example
A retailer has, for each customer, the number of orders per year and the average order value. K-means with k = 3 might reveal frequent low-value buyers, occasional high-value buyers and inactive customers. Marketing can then treat each group differently.
Choosing k
- Elbow method: run k-means for k = 1 to 10 and plot the total within-cluster distance. Pick the point where the curve bends and adding clusters stops helping much.
- Silhouette score: measures how well each point fits its own cluster compared with others; higher is better.
- Business sense: five clusters nobody can act on are worse than three clear ones.
Common mistakes
- Not scaling features. K-means uses distance, so a feature measured in thousands (revenue) will dominate one measured in single digits (orders). Standardise first.
- Using raw categories. K-means needs numbers; encode categories or choose a different method.
- Expecting odd shapes. K-means finds round, similar-sized groups. Long or uneven clusters need other algorithms such as DBSCAN.
In Python
from sklearn.cluster import KMeans
model = KMeans(n_clusters=3, n_init=10, random_state=42)
labels = model.fit_predict(X_scaled)
Next, read feature engineering basics and classification models for beginners.
Clustering and other machine learning methods are covered with projects in our Data Science course in Bangalore.
Book a free demo class at our BTM Layout centre, and ask a counsellor for batch details.
