Most machine learning tutorials start with a target column: predict the price, predict churn. But a lot of business data has no target at all. The marketing team just wants to know "what kinds of customers do we have?" That is unsupervised learning, and K-Means and PCA are the two tools you will meet first.
K-Means: grouping similar rows
K-Means splits your rows into K groups (clusters) so that rows in the same group are similar to each other.
How it works:
- Pick K starting centres.
- Assign every row to its nearest centre.
- Move each centre to the average of its rows.
- Repeat steps 2 and 3 until the centres stop moving.
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
X = StandardScaler().fit_transform(customers[["monthly_spend", "visits", "avg_basket"]])
km = KMeans(n_clusters=4, n_init=10, random_state=42).fit(X)
customers["segment"] = km.labels_Always scale first
K-Means uses distance. If one column is spend in rupees (thousands) and another is visits per month (single digits), spend dominates every distance and the clusters are really just spend bands. Scaling puts columns on a comparable footing.
Choosing K
- Elbow method: plot the total within-cluster distance (inertia) for K = 1 to 10 and look for the point where adding clusters stops helping much.
- Silhouette score: measures how well each row fits its own cluster compared with the next one; higher is better.
- Business sense: four segments a marketing team can act on beat nine segments nobody can remember.
Describe the clusters
A cluster number means nothing to the business. Profile each one:
customers.groupby("segment")[["monthly_spend", "visits", "avg_basket"]].mean()Then name them: "frequent small-basket shoppers", "occasional big spenders", and so on. The naming is where an analyst adds most value.
Limitations
K-Means assumes clusters are roughly round and similar in size, is sensitive to outliers, and will always return K groups even if the data has no real groups. Treat the result as a useful simplification, not the truth.
PCA: fewer columns, most of the information
Imagine a survey with twelve questions about a product. Many answers move together: people who rate "build quality" highly also rate "durability" highly. PCA finds new combined columns, called principal components, that capture as much of the variation as possible in as few columns as possible.
from sklearn.decomposition import PCA
X = StandardScaler().fit_transform(survey)
pca = PCA(n_components=2).fit(X)
pca.explained_variance_ratio_ # share of variation each component keeps
scores = pca.transform(X)Interpreting components
Look at the loadings (pca.components_): how much each original column contributes to each component. If the first component loads heavily on quality, durability and finish, you might call it "product quality score". If the second loads on price and value questions, call it "value for money". Now twelve columns have become two clear scores.
When to use PCA
- To summarise many correlated measures into a few scores.
- To plot high-dimensional data in two dimensions.
- To reduce noise or speed up another model.
Like K-Means, PCA needs scaled data, and its components must be interpreted by a human.
Using them together
A common workflow: scale the data, use PCA to reduce it to a few components, run K-Means on those components, then plot the clusters on the first two components. This makes clusters easier to visualise and often more stable.
Learn by experimenting
Drag points and change K in the K-Means playground to see how clusters form and where K-Means fails. Then practise segmenting bank card customers and summarising subject marks with PCA.
Practise what you just read
Reading helps; writing the code yourself is what sticks. Start here: