Machine Learning Algorithms

Chapter 9
Clustering and Segmentation

Post Graduate Program in Business Analytics
Machine Learning Algorithms   •   Post Graduate

Clustering and Segmentation

Introduction and Context

Clustering is an unsupervised machine-learning task in which observations are grouped so that members of the same group are more similar to one another than to members of other groups. Unlike classification, clustering does not begin with a labelled target variable. The analyst must determine whether meaningful structure exists, how many groups are useful, which similarity concept is appropriate, and how the resulting groups should inform decisions.

In business analytics, clustering is frequently used for customer segmentation, store and product grouping, fraud investigation, workforce planning, service operations, document organization, and exploratory analysis. Its value does not lie merely in producing coloured groups on a chart. A useful segmentation should be statistically defensible, operationally identifiable, sufficiently stable, and connected to differentiated action.

For an observation vector xᵢ = (xᵢ₁, xᵢ₂, …, xᵢₚ), where p is the number of measured features, clustering seeks a partition or structure over n observations. The word “similarity” is context-dependent. Euclidean distance may be appropriate for standardized continuous measurements, whereas cosine similarity may be preferable for high-dimensional text vectors and Gower distance may be more suitable for mixed numerical and categorical data.

Learning Objectives

By the end of this chapter, you should be able to:

  1. Explain the purpose, assumptions, and terminology of clustering and segmentation.
  2. Apply k-means, hierarchical, density-based, and probabilistic clustering methods.
  3. Select and justify distance or similarity measures.
  4. Determine a suitable number of clusters and validate cluster quality and stability.
  5. Interpret clusters without confusing statistical association with causal explanation.
  6. Translate segmentation results into customer, operational, and strategic actions.
  7. Recognize ethical, governance, computational, and managerial limitations.

Foundations: From Clusters to Actionable Segments

A cluster is a data-derived group. A segment is a managerial category used to support differentiated decisions. The two terms are related but not identical. A mathematically coherent cluster may be too small to serve profitably, impossible to identify at the time of decision, or indistinguishable in terms of appropriate action. Segmentation therefore adds business logic, feasibility, and governance to clustering.

Important terminology includes:

  • Feature: a measured input variable used to represent an observation.
  • Centroid: the arithmetic mean vector of observations assigned to a cluster.
  • Medoid: an actual observation whose total dissimilarity to other members is relatively low.
  • Partition: a division in which each observation belongs to one cluster.
  • Soft assignment: membership represented by probabilities or degrees rather than a single label.
  • Prototype: a representative point, such as a centroid or medoid.
  • Noise or outlier: an observation not adequately represented by a regular cluster.
  • Linkage: the rule used to define distance between groups in hierarchical clustering.
  • Density: the concentration of observations in a region of feature space.
  • Cluster validity: evidence that a clustering is internally coherent, externally useful, stable, and substantively meaningful.

A sound analytical workflow normally follows this sequence:

Clustering workflow from business question to action Businessquestion Prepare andrepresent data Cluster andcompare models Validate andprofile groups Act andmonitor A responsible segmentation lifecycle Feedback from deployment should trigger validation and, when needed, re-segmentation.
Clustering workflow from business question to action
Figure: Clustering workflow from business question to action

Figure: A clustering workflow linking technical analysis to managerial action.

Data Representation and Distance Measures

Clustering is highly sensitive to representation. Analysts should define the unit of analysis, observation window, treatment of missing values, handling of extreme values, and whether variables describe level, frequency, or change. Customer-level data, for example, may be aggregated using recency, frequency, monetary value, average basket size, channel share, or service contacts.

Scaling

If one feature is measured in dollars and another in counts, the larger numerical scale may dominate distance. A common standardization is the z-score:

zij = (xij − μj) / σj

Here, xᵢⱼ is the value of observation i on feature j, μⱼ is the sample mean of feature j, and σⱼ is its sample standard deviation. The result is dimensionless. This transformation is appropriate when relative deviations from the feature mean are meaningful and when extreme values do not make the mean and standard deviation misleading. Robust alternatives use the median and interquartile range. Min–max scaling maps values into a chosen interval, commonly [0, 1], but remains sensitive to extreme observations.

Common measures

For two p-dimensional vectors x and y, Euclidean distance is:

dE(x,y) = √[∑j=1p(xj − yj)2]

It is appropriate for continuous variables with comparable scales and a meaningful straight-line geometry. Squared Euclidean distance is often used computationally by k-means; it preserves the ordering of distances but is more sensitive to large deviations.

Manhattan distance is:

d1(x,y) = ∑j=1p|xj − yj|

It can be more robust than Euclidean distance when deviations accumulate across dimensions or when a coordinate-wise interpretation is useful.

Cosine similarity measures the angle between nonzero vectors:

cos(x,y) = (x · y) / (||x|| ||y||)

where x · y is the dot product and ||x|| is the Euclidean norm. Cosine similarity emphasizes direction rather than magnitude and is common for document or recommendation vectors. It is undefined for a zero vector.

For binary variables, the appropriate measure depends on meaning. If a value of 1 indicates possession of an attribute and 0 indicates absence, Jaccard similarity ignores joint zeros:

J(A,B) = a / (a + b + c)

where a is the number of attributes present in both observations, b is present only in A, and c is present only in B. This is useful when shared absences are not evidence of similarity. For mixed numerical, ordinal, and categorical data, Gower’s approach computes feature-wise dissimilarities, scales numerical differences, and combines them with suitable categorical comparisons. Analysts must specify how missing values and feature weights are treated.

Distance selection is a modelling assumption, not a clerical choice. If the chosen distance does not represent business similarity, an apparently excellent statistical result may be strategically meaningless.

k-Means Clustering

Principle and objective

k-means partitions n observations into k clusters by minimizing the within-cluster sum of squared Euclidean distances:

J = ∑r=1k ∑xi ∈ Cr ||xi − μr||²

Cᵣ is cluster r, μᵣ is its centroid, and ||xᵢ − μᵣ||² is squared Euclidean distance. The method assumes numerical features, meaningful means, roughly compact and convex groups, and a selected value of k. It is not inherently a probabilistic model and does not discover the globally optimal solution in general.

The algorithm alternates two steps:

  1. Assignment: assign each observation to the nearest current centroid.
  2. Update: replace each centroid with the mean of observations assigned to it.

Iterations stop when assignments, centroids, or the objective function change less than a chosen tolerance, or when a maximum iteration limit is reached. Because the objective is non-convex with respect to assignments and centroids jointly, different initializations can produce different solutions. k-means++ initialization generally spreads initial centroids and often improves practical performance, but does not remove the need for repeated runs.

Small worked example

Consider four standardized two-dimensional observations: A = (1, 1), B = (1, 2), C = (5, 5), and D = (6, 5), with k = 2. Suppose initial centroids are A and C. A and B are assigned to the first centroid, while C and D are assigned to the second. The updated centroids are:

μ1 = ((1+1)/2, (1+2)/2) = (1, 1.5)
μ2 = ((5+6)/2, (5+5)/2) = (5.5, 5)

The squared-distance contribution of A is 0.25, because its difference from μ₁ is (0, −0.5). For B it is also 0.25. For C, the contribution is 0.25, and for D it is 0.25. Thus the total objective after this update is 1.0. The assignments remain unchanged, so the algorithm has converged for this initialization. This example is deliberately simple; real results should be tested across multiple starts.

Strengths and weaknesses

k-means is fast, scalable, easy to explain, and effective for large numerical datasets with compact groups. However, it requires k in advance, is affected by scale and outliers, and tends to impose spherical or similarly sized structures. It may produce misleading partitions when clusters differ greatly in size, density, or shape. A centroid can also be an artificial point that does not correspond to any real customer.

Hierarchical Clustering

Hierarchical methods create a nested structure that can be represented by a dendrogram. In agglomerative clustering, each observation begins as a singleton and the closest groups are repeatedly merged. A cut across the dendrogram produces a selected number of clusters. Divisive methods begin with one group and recursively split it, but are less common in routine applications.

The definition of group-to-group distance is the linkage criterion:

  • Single linkage uses the closest pair across groups. It can detect elongated structures but is susceptible to chaining.
  • Complete linkage uses the farthest pair. It favours compact groups.
  • Average linkage uses the average pairwise distance and is often a compromise.
  • Ward’s method merges groups causing the smallest increase in within-cluster squared error. It is closely related to a variance-minimizing objective and normally uses Euclidean geometry.

Hierarchical clustering is valuable when the analyst wants to inspect solutions at several granularities, such as product families and subfamilies. Its limitations include sensitivity to distance and linkage choices, and the fact that early merges cannot generally be undone. Computational cost may also be substantial for very large datasets. A dendrogram is an exploratory representation, not proof that a particular cut is objectively correct.

Density-Based Clustering

Density-based methods define clusters as dense regions separated by lower-density areas. DBSCAN uses two key parameters: ε, the neighbourhood radius, and MinPts, the minimum number of points required to define a dense neighbourhood. A point is a core point if its ε-neighbourhood contains at least MinPts observations, counting the point itself. Points reachable from core points can form a cluster; isolated observations may be labelled noise.

DBSCAN can find non-spherical clusters and explicitly identify noise. It does not require the number of clusters in advance. It is useful for spatial events, geolocation, equipment anomalies, and transaction patterns with irregular shapes. However, one global ε is difficult when clusters have different densities, and performance deteriorates in high-dimensional spaces because distances become less discriminating. HDBSCAN extends the density-based idea by representing clusters across density levels and can be more effective when density varies, although its interpretation and parameterization still require care.

Probabilistic Clustering and Mixture Models

A Gaussian mixture model (GMM) treats observations as arising from a mixture of k component distributions. The density for observation x is:

p(x) = ∑r=1k πr ϕ(x | μr, Σr)

Here, πᵣ ≥ 0 are mixing proportions satisfying ∑πᵣ = 1; ϕ is a multivariate Gaussian density; μᵣ is the component mean vector; and Σᵣ is the covariance matrix. The model assumes that the chosen component family is a reasonable approximation, observations are conditionally independent given component membership, and the covariance specification is estimable.

Unlike k-means, GMM provides soft membership. The posterior probability that observation x belongs to component r is:

P(r|x) = [πr ϕ(x|μr,Σr)] / [∑s=1k πs ϕ(x|μs,Σs)]

The expectation–maximization (EM) algorithm alternates between estimating membership probabilities (E-step) and updating component parameters (M-step). Covariance matrices allow elliptical clusters and correlations among features. Nevertheless, mixture models can be sensitive to initialization, covariance singularity, distributional misspecification, and the selected number of components. Regularization and constrained covariance structures may be necessary.

The number of components can be compared using information criteria. For a fitted model with log-likelihood ℓ, q estimated parameters, and n observations:

AIC = 2q − 2ℓ
BIC = q ln(n) − 2ℓ

Lower values are preferred within the same dataset and likelihood framework. BIC penalizes model complexity more strongly as n grows. Neither criterion guarantees managerial usefulness or causal validity.

Choosing the Number of Clusters

No single method determines the “true” number of clusters in every business dataset. Analysts should combine several forms of evidence:

  1. Elbow analysis: plot within-cluster sum of squares against k and look for a diminishing-return point. The elbow may be ambiguous.
  2. Silhouette analysis: for observation i, let a(i) be its average distance to members of its own cluster and b(i) the smallest average distance to another cluster. Its silhouette is:
s(i) = [b(i) − a(i)] / max(a(i), b(i))

The value lies between −1 and 1 when distances are nonnegative. Values near 1 indicate good separation, values near 0 indicate overlap, and negative values suggest possible misassignment. The average silhouette summarizes a partition but can favour particular shapes and distances.

  1. Gap or information criteria: compare within-cluster dispersion with a reference distribution or compare penalized likelihoods.
  2. Stability: refit using resamples, time periods, or alternative initializations and examine whether memberships remain similar.
  3. External validity: determine whether clusters predict or differentiate outcomes not used to construct them, without claiming that clustering caused those outcomes.
  4. Business viability: consider segment size, reachability, economics, legal constraints, and whether actions can genuinely differ.

A useful validation framework distinguishes internal validity (cohesion and separation), stability (replication under perturbation), external validity (relationship to independent outcomes), and actionability (ability to support a decision).

Interpreting and Profiling Clusters

Cluster labels such as “high value” or “at risk” should be assigned only after examining feature distributions and business outcomes. Profiling should include cluster size, central tendency, dispersion, missingness, representative cases, and confidence or assignment strength. Means alone can hide skewness; medians, quantiles, and distributions are often necessary.

A practical profile table might include:

SegmentSizeBehavioural profileBusiness outcomePotential action
A18%Frequent, high-margin, digitalHigh retentionLoyalty and premium service
B42%Occasional, price-sensitiveModerate marginTargeted value bundles
C7%Low frequency, high service costNegative marginService redesign and controlled retention
D33%New or low-history customersUncertainOnboarding and measurement

The table is an interpretation, not an automatic output. Analysts should check whether the variables used to define the segments are available at the point when an intervention is made. If “future churn” was used in the clustering features, the resulting segment cannot fairly be described as available for an earlier retention decision.

Visualizations include pair plots, principal-component projections, heat maps, dendrograms, and geographic maps. Dimensionality reduction can aid communication but may distort distances; a two-dimensional plot should not be treated as definitive evidence of the full-dimensional structure.

Applied Case: Illustrative Retail Customer Segmentation

Background

A fictional omnichannel retailer wants to improve retention and marketing efficiency. It has twelve months of customer-level data containing order frequency, average order value, gross margin rate, digital-channel share, returns, and customer-service contacts. The objective is not simply to describe customers but to identify groups for differentiated retention and service strategies.

Problem and preparation

The analytics team defines the customer as the unit of analysis and uses only information available at the end of the observation period. Monetary measures are adjusted for returns and expressed consistently. Extremely influential values are reviewed rather than automatically deleted. Skewed frequency and spend variables are transformed where justified, and continuous features are robustly standardized. Customers with insufficient history are analysed separately because low activity may represent newness rather than low engagement.

The team compares k-means, Ward hierarchical clustering, and a Gaussian mixture model. A distance-based model is not applied directly to unprocessed categorical channel fields. Several values of k are evaluated using silhouette, resampling stability, profile interpretability, and projected campaign capacity.

Analysis and solution

A four-segment solution is selected. A fifth cluster slightly improves internal separation but is too small for a distinct programme and is unstable across monthly resamples. The four segments are:

  • Relationship customers: frequent purchases, high margin, high digital engagement, low service burden.
  • Value seekers: moderate frequency, high promotion responsiveness, lower margin.
  • Service-intensive customers: moderate revenue but unusually high contacts and returns.
  • Emerging customers: short history and low frequency, with insufficient evidence to label them as disengaged.

The team uses a mixture model’s membership probabilities to identify ambiguous cases. Customers with no dominant membership are assigned to a general communication pathway rather than receiving a highly specific offer.

Outcome and governance

The retailer pilots different actions in randomized holdout groups: recognition benefits for relationship customers, margin-aware bundles for value seekers, service recovery and product-quality investigation for service-intensive customers, and onboarding for emerging customers. Success is assessed using incremental retention and contribution margin, not response rate alone. The segmentation is reviewed quarterly because customer behaviour and economic conditions change.

The team refuses to use protected characteristics or sensitive inferred attributes for discriminatory pricing. It documents feature provenance, retention periods, assignment rules, model versions, and appeal or correction procedures. Monitoring checks whether segment membership and campaign outcomes differ unfairly across protected groups.

Learning points

The case demonstrates that segmentation requires temporal discipline, feature governance, model comparison, stability testing, experimental evaluation, and operational ownership. A cluster is a hypothesis about useful heterogeneity; it becomes valuable only when responsible action produces measurable improvement.

Challenges, Ethics, and Governance

Statistical and operational limitations

High dimensionality can make distances converge, a phenomenon often called the curse of dimensionality. Redundant variables can overweight one underlying concept. Missing-data imputation can create artificial similarity. Outliers may either be important strategic customers or data errors. Cluster solutions can be unstable when sample size is small or when the data-generating process changes.

Clustering is descriptive and generally non-causal. If one segment has higher churn, the clustering does not establish that its defining features caused churn. Confounding, selection bias, and measurement error remain relevant. Post-clustering significance tests should account for the fact that groups were discovered from the same data; otherwise, apparent differences may be overstated.

Ethical risks

Segmentation can enable exclusion, surveillance, discriminatory pricing, or manipulation. Sensitive traits may be inferred indirectly from location, behaviour, or social connections. Even when protected attributes are omitted, proxy variables can reproduce unequal treatment. Explainability should include the features driving membership, the uncertainty of assignment, the purpose of the segment, and the consequences for individuals.

Governance controls should include lawful and transparent data collection, purpose limitation, access control, retention policies, bias assessment, human review for consequential decisions, and monitoring for drift. Customers should not be denied essential services solely because an opaque cluster label assigns them to a supposedly undesirable group.

Contemporary Developments and Best Practice

Modern practice increasingly combines representation learning with clustering. Embeddings can represent text, products, images, or customer journeys, but their geometry reflects the training data and objective. Analysts should validate whether distances in an embedding correspond to the intended business concept. Deep clustering may improve pattern discovery but can reduce transparency and increase sensitivity to training choices.

Streaming and online clustering support rapidly changing operations, while graph-based methods represent relationships rather than only feature vectors. Dynamic segmentation treats membership as evolving over time and can distinguish stable types from temporary states. In all cases, complexity should be justified by incremental value.

A professional checklist is:

  1. Define the decision, unit, time window, and permissible features.
  2. Inspect quality, missingness, skew, outliers, leakage, and duplicates.
  3. Choose representation, scaling, and distance based on domain meaning.
  4. Compare suitable algorithms rather than relying on one default.
  5. Use repeated initialization and stability analysis.
  6. Evaluate internal quality, external relevance, and business feasibility.
  7. Profile distributions and uncertainty, not only centroids.
  8. Pilot actions with holdouts or experiments where possible.
  9. Document assumptions, limitations, governance, and model version.
  10. Monitor drift, impact, fairness, and whether actions remain differentiated.

Chapter Summary

Clustering discovers structure without labelled outcomes, while segmentation converts that structure into decision-relevant groups. k-means is efficient for scaled numerical data with compact clusters; hierarchical methods reveal nested structure; density-based methods identify irregular groups and noise; and probabilistic mixture models provide soft membership and distributional representations. Distance measures and data representation determine what “similar” means, so they must be selected deliberately.

The number of clusters should be assessed through internal metrics, stability, external outcomes, and business actionability. Cluster interpretation requires careful profiling, temporal validity, and uncertainty awareness. A responsible segmentation programme also addresses outliers, high dimensionality, drift, causal overinterpretation, privacy, fairness, and transparency. The final standard is not whether an algorithm produces groups, but whether those groups support effective, measurable, and ethically defensible action.

Key Terms

Clustering: Unsupervised grouping of observations according to similarity.

Segmentation: Use of data-derived or designed groups to support differentiated decisions.

Centroid: Mean vector representing a cluster in centroid-based methods.

Medoid: Actual observation that represents a group through low total dissimilarity.

Partition: Assignment of observations into non-overlapping groups.

Soft assignment: Probabilistic or graded membership in multiple groups.

Euclidean distance: Straight-line distance based on squared coordinate differences.

Manhattan distance: Sum of absolute coordinate-wise differences.

Cosine similarity: Similarity based on the angle between nonzero vectors.

Gower distance: A distance framework for mixed numerical, ordinal, and categorical data.

k-means: Iterative algorithm minimizing within-cluster squared Euclidean distance.

Hierarchical clustering: Method producing a nested tree of merged or split groups.

Linkage: Rule defining distance between clusters in hierarchical analysis.

Density-based clustering: Grouping based on dense regions separated by sparse regions.

Gaussian mixture model: Probabilistic model representing data as a mixture of Gaussian components.

Silhouette: Measure comparing within-cluster cohesion with nearest-cluster separation.

Stability: Persistence of a clustering under resampling, initialization, or time changes.

Cluster drift: Change in cluster structure or membership over time.

Actionability: Extent to which a segment supports feasible differentiated intervention.

Review Questions

Short Answer Questions

  1. Distinguish a data-derived cluster from a managerial segment.
  2. Why is feature scaling important for k-means?
  3. State one strength and one limitation of DBSCAN.
  4. What does a silhouette value near zero generally indicate?
  5. Why can a Gaussian mixture model provide more information than a hard partition?

Long Answer Questions

  1. Compare k-means, hierarchical clustering, density-based clustering, and Gaussian mixture models in terms of assumptions, outputs, strengths, and limitations.
  2. Explain how distance selection should be aligned with data type and business meaning. Include examples of Euclidean, Manhattan, cosine, and Gower measures.
  3. Develop a rigorous framework for selecting the number of clusters using statistical and managerial evidence.
  4. Discuss how scaling, outliers, missing values, and high dimensionality can alter a clustering solution.
  5. Explain the ethical and governance risks of customer segmentation and propose controls for responsible deployment.

Analytical / Application Questions

  1. A retailer has highly skewed spend, frequency, returns, and service-contact variables, together with a categorical preferred channel. Design a preprocessing and clustering plan, justifying representation, scaling, distance, and validation choices.
  2. A k-means solution has a higher average silhouette than a hierarchical solution, but its segments are unstable over time and cannot support different interventions. Which solution, if any, should management adopt? Defend your answer.
  3. Design an evaluation plan for a four-segment retention programme that separates descriptive cluster quality from the incremental causal effect of segment-specific actions.

Multiple Choice Questions with Answers

Question 1

Which objective does standard k-means minimize?

A. Total between-cluster covariance

B. Within-cluster sum of squared Euclidean distances

C. The number of observations labelled as noise

D. The Bayesian information criterion only

Correct Answer: B. Within-cluster sum of squared Euclidean distances

Question 2

Which distance is especially appropriate when vector direction matters more than magnitude, as in many text representations?

A. Cosine similarity

B. Squared Euclidean distance only

C. Hamming distance only

D. Ward linkage

Correct Answer: A. Cosine similarity

Question 3

What is a defining feature of DBSCAN?

A. It always requires the number of clusters in advance

B. It assumes every cluster is spherical

C. It can identify noise and irregularly shaped dense regions

D. It assigns every observation to the nearest centroid

Correct Answer: C. It can identify noise and irregularly shaped dense regions

Question 4

In a Gaussian mixture model, what does soft membership provide?

A. A probability of membership in each component

B. A guarantee that clusters are causal groups

C. A ranking based only on sample size

D. A replacement for covariance estimation

Correct Answer: A. A probability of membership in each component

Question 5

If a feature has mean 50 and standard deviation 10, what is its z-score when the observed value is 70?

A. −2

B. 0.5

C. 2

D. 20

Correct Answer: C. 2

Question 6

A silhouette value close to 1 generally indicates that an observation is:

A. Closer to its own cluster than to neighbouring clusters

B. Equally close to every cluster

C. Definitely an outlier under DBSCAN

D. Assigned using a probabilistic model

Correct Answer: A. Closer to its own cluster than to neighbouring clusters

Question 7

Which hierarchical linkage uses the farthest pair of observations between two candidate clusters?

A. Single linkage

B. Complete linkage

C. Average linkage

D. Random linkage

Correct Answer: B. Complete linkage

Question 8

Why can repeated initialization be important in k-means?

A. The algorithm’s objective is generally non-convex and different starts can yield different solutions

B. Scaling is automatically reversed after each run

C. It changes categorical variables into numerical variables

D. It guarantees the globally optimal solution immediately

Correct Answer: A. The algorithm’s objective is generally non-convex and different starts can yield different solutions

Question 9

Which statement best distinguishes internal validity from actionability?

A. Internal validity concerns cohesion and separation, whereas actionability concerns feasible differentiated decisions

B. Internal validity measures legal compliance, whereas actionability measures covariance

C. Internal validity requires labels, whereas actionability prohibits labels

D. They are identical measures with different names

Correct Answer: A. Internal validity concerns cohesion and separation, whereas actionability concerns feasible differentiated decisions

Question 10

Why should a cluster associated with higher churn not automatically be described as causing churn?

A. Clustering is descriptive and association does not establish causation

B. Churn cannot be measured numerically

C. Clusters never contain behavioural variables

D. Causation is guaranteed only by using k-means

Correct Answer: A. Clustering is descriptive and association does not establish causation

Machine Learning Algorithms
Chapter 9 of 15