Data ScienceClustering, reduction and recommendations

K-means: choose features before choosing the number of clusters

PK
Pankit Kumar
Sr. Data Scientist at Parexel (a Goldman Sachs–backed company) · 20 September 2026 · 2 min read
Technically reviewed by Ishaan Sharma
In this article (3 sections)

K-means minimizes squared Euclidean distance to centroids. Features and units therefore define what “close” means before the algorithm ever considers the number of clusters. Searching k on a weak representation only optimizes a weak question.

Hold k fixed and change the representation

Our synthetic fixture has 360 customers and three behaviour features: visits over 90 days, average order and return rate. The generator contains three authored behaviour types, but those labels never enter clustering. We use them only afterward to audit whether a representation recovered the construction.

With the three behaviour features standardized and k=3, mean silhouette is 0.6196 and adjusted Rand index against generator labels is 1.0. Clustering the raw units yields silhouette 0.6081 but ARI only 0.6587 because average order dominates smaller-scale variables.

Adding a standardized random irrelevant_code feature lowers silhouette to 0.4306. ARI remains high at 0.9917 in this easy fixture, but the weaker separation shows that equal scaling does not make an irrelevant feature useful.

The KMeans documentation defines its inertia objective. The metric treats every included coordinate as part of customer similarity.

python
from unsupervised_cases import kmeans_case

r = kmeans_case()
assert r['rows'] == 360
assert r['features'] == ['visits_90d', 'average_order', 'return_rate']
assert r['scaled_behavior']['generator_ari'] == 1
assert r['raw_unscaled']['generator_ari'] < .7
assert r['scaled_with_irrelevant_code']['silhouette'] < r['scaled_behavior']['silhouette']
print(r)

Run this in the unsupervised lab. Generator ARI is unavailable in a real segmentation project unless an independent reference taxonomy exists.

Define similarity from the decision

Include features only when their meaning, availability and time window match the segmentation use case. Monetary, frequency and rate features may need transformations for skew and recency. Highly correlated variables can double-count one behaviour. Identifiers and arbitrary codes should not enter a Euclidean space.

Scaling expresses a weighting choice. Standardization gives each feature unit variance in the fitting population; it does not prove equal business importance. Robust scaling may help with extreme values, while domain weights require documented justification and sensitivity analysis.

Fit transformations on the intended training or reference period. When assigning later customers, reuse the saved scaler and centroids. Recomputing scaling each period changes the coordinate system and makes movement harder to interpret.

After the feature contract is stable, compare plausible k values with silhouette, stability, profiles and minimum useful segment size. A cluster should support a distinguishable action or analysis; a high internal score alone does not create value.

Exercise: remove each behaviour feature one at a time and repeat the cluster audit. Add a duplicated average-order column and show how it changes distance weighting. Write the feature contract you would require before any customer receives a segment-driven action.

NeuraPath's Data Science course teaches clustering as a representation decision. Feature meaning comes before elbow plots and cluster counts.

Continue learning

This article is part of the Clustering, reduction and recommendations sequence. Use the neighbouring tasks when you need the prerequisite or the next application.

PK
Pankit Kumar
Lead Instructor, NeuraPath Academy

Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.

This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.

Explore Data Science
Counselling is free · no obligation

Not sure which programme fits?

Tell us your background and we will map it to the right entry point — including saying so when a cheaper programme is the better fit. A counsellor replies within one working day.