IB COMPUTER SCIENCE | A4.3.4 Clustering (HL)

Lesson objective

Describe how clustering uses feature similarity without known target labels, trace K-means assignment and centroid updates, and interpret customer segments cautiously.

Learn

A4.3.4 | CLUSTERING (HL ONLY)

01 | FIND GROUPS WITHOUT BEING GIVEN THE ANSWERS

An online shop has records of how often customers order and the typical value of their baskets. It does not have a reliable label saying which kind of customer each person is. Could the purchasing patterns suggest useful groups?

Clustering is an unsupervised learning approach that groups observations using similarities in their features. The algorithm is not trained to reproduce known customer-category targets. Instead, it finds structure under the representation and grouping rules we choose.

This HL lesson uses fictional purchasing data to show the process. We will work through K-means as one concrete technique, then consider alternative approaches and why discovered groups need careful interpretation.

02 | WHAT UNSUPERVISED MEANS HERE

In supervised classification, each training example has a known target label, such as Type A or Type B. The model learns or uses those examples to predict a label for a new input.

In clustering, the inputs are supplied without known target-group labels guiding the fit. The algorithm assigns group IDs such as Cluster 1 and Cluster 2 based on feature patterns. Those IDs are outputs of the grouping process, not previously verified customer types.

Unsupervised does not mean no human choices. People still define the purpose, select features, clean and scale them, choose a method and inspect whether the results are useful.

03 | CHOOSE FEATURES THAT DESCRIBE THE QUESTION

For customer segmentation, possible features include purchase frequency, average basket value and the proportions of spending on different product categories. Each feature contributes a coordinate describing one customer’s recorded behaviour.

A customer identifier is useful for joining the result back to the correct account, but a sequential account number does not normally provide a meaningful numerical distance between customers. Keep identity separate from clustering inputs.

The observation period matters too. Comparing one person’s full year of orders with another person’s first week can create misleading differences. Define consistent windows and investigate missing or incomplete histories before clustering.

04 | READ THE UNLABELLED PURCHASING DATA

Customer Frequency score x Basket-value score y Known segment label
C1 1 1 Not supplied
C2 1 2 Not supplied
C3 2 1 Not supplied
C4 4 7 Not supplied
C5 5 7 Not supplied
C6 5 8 Not supplied
C7 8 2 Not supplied
C8 8 3 Not supplied
C9 9 2 Not supplied

Both input descriptors are fictional comparable-scale scores from 0 to 10. Higher frequency scores represent more frequent purchasing; higher basket-value scores represent larger typical baskets. They are not literal numbers of orders or currency amounts.

The customer IDs identify rows only. The final column reminds us that no correct segment label is provided to the clustering procedure. A plot may suggest groups, but those visual impressions still depend on the two selected inputs.

05 | SIMILARITY DEPENDS ON THE REPRESENTATION

Two observations are similar if they are close under the chosen features and comparison rule. In a two-number representation, a common distance is Euclidean distance: square each coordinate difference, add the squares and take the square root.

C1 = (1,1) and C2 = (1,2) are one unit apart. C1 and C7 = (8,2) have distance √(7² + 1²) = √50 ≈ 7.071. Under these descriptors, C2 is much closer to C1.

This does not prove the customers are similar in every respect. They might buy different products, have different histories or behave differently outside the observation window. Similarity is always defined with respect to the supplied representation.

06 | SCALE BEFORE COMPARING UNLIKE MEASUREMENTS

Suppose raw basket value is recorded in hundreds of currency units but purchase frequency ranges only from 0 to 10. Numerical basket differences could dominate an unscaled distance simply because of magnitude.

Use appropriate training or reference-data preprocessing, such as standardization, and apply the fitted transformation consistently. The interactive dataset already uses comparable-scale descriptors to keep the calculation visible.

Scaling is itself a modelling choice. It does not make irrelevant features informative, remove every effect of an outlier or guarantee meaningful segments. If the goal is to group future customers, learn preprocessing from the fitting data rather than refitting it separately for every new batch.

07 | K-MEANS STARTS WITH A REQUESTED NUMBER OF GROUPS

K-means represents each cluster by a centroid, its mean coordinate position. The configured number k states how many clusters the procedure is asked to form.

For k = 3, begin with three initial centroid positions. Assign observations to their nearest centroid, recompute each centroid from its assigned members and repeat. The procedure aims to reduce within-cluster squared distances.

K in K-means is not k in K-nearest neighbours. K-means forms groups without target labels; K-NN consults k labelled nearby examples to predict a class.

08 | ASSIGN EACH OBSERVATION TO A CENTROID

For one worked assignment, use centroids A = (1,1), B = (2,1) and C = (4,8). These intentionally uneven starting positions are selected to make subsequent movement visible.

Customer C3 = (2,1) is distance 1 from A, distance 0 from B and distance √53 ≈ 7.280 from C. It initially belongs to B because B is nearest.

Customer C3 to centroid Squared distance Euclidean distance
A = (1,1) (2−1)² + (1−1)² = 1 1
B = (2,1) 0² + 0² = 0 0
C = (4,8) (2−4)² + (1−8)² = 53 √53 ≈ 7.280

Each point is assigned using input distances, not a majority vote of known labels. If two centroids are exactly equally near, a defined tie rule is needed; this demonstration selects the lower-numbered cluster.

09 | RECOMPUTE A CENTROID BY AVERAGING ITS MEMBERS

After the first assignment with those starting positions, Cluster B contains C3 = (2,1), C7 = (8,2), C8 = (8,3) and C9 = (9,2).

Average the x coordinates: (2 + 8 + 8 + 9) / 4 = 6.75. Average the y coordinates: (1 + 2 + 3 + 2) / 4 = 2. The updated centroid is therefore (6.75, 2).

A centroid need not coincide with an actual customer. It summarises coordinates, not a real person whose behaviour must exist. Replacing an observation with a centroid would change the original dataset; K-means instead retains observations and updates the group representation.

10 | REASSIGN AFTER THE CENTROIDS MOVE

The first update moves Cluster A’s centre to (1,1.5), B’s to (6.75,2) and C’s to approximately (4.667,7.333). Now reconsider C3 = (2,1).

Its distance to A becomes √(1² + 0.5²) ≈ 1.118. Its distance to B becomes √(4.75² + 1²) ≈ 4.854. It changes from B to A.

That is why K-means repeats the process. The closest centre can change after the previous membership assignment has moved the centres. A single assignment from initial guesses is not the completed grouping.

11 | REPEAT UNTIL A STOPPING RULE IS MET

Alternate assignment and mean updates until the group memberships or centroid positions stop changing sufficiently, or a configured iteration limit is reached. A complete cycle contains an assignment stage and an update stage.

In the three-cluster worked example, the stable memberships are C1–C3 together, C4–C6 together and C7–C9 together. Their centroids are approximately (1.333,1.333), (4.667,7.333) and (8.333,2.333).

Convergence means the procedure has reached its stopping condition. It does not prove the globally best grouping or the existence of exactly three natural customer types. Different starting positions can produce different local solutions.

12 | STEP THROUGH ASSIGNMENT AND CENTROID MOVEMENT

Choose two or three clusters and a starting layout, then reveal one stage at a time. The plot and table update from real calculations on the nine fictional observations. Cluster IDs are arbitrary; compare memberships rather than assuming an ID has a fixed meaning.

Changing a selector starts a fresh run with that setting. The lower button reveals the next stage without making you scroll back up.

For k = 3 with uneven centres, watch C3 switch clusters after the first centroid update. During an update stage, the table keeps the previous memberships until the next assignment stage. The displayed squared-error sum always refers to the memberships and centres currently shown.

13 | UNDERSTAND WHAT THE OBJECTIVE MEASURES

K-means aims to minimise the within-cluster sum of squared distances from observations to their assigned centroids. It is often called inertia. Smaller values indicate tighter groups under that representation, for a specified comparison.

Assignment chooses the nearest centre; updating to a mean minimises squared distances within each nonempty fixed group. These stages do not increase that objective under the ordinary procedure.

This value is not classification accuracy and has no verified target labels in its calculation. A low objective does not prove that a group is useful, fair or causally meaningful. Comparing values across different scaling choices is especially difficult because the distance units have changed.

14 | WHY NOT INCREASE K UNTIL EVERYONE HAS A GROUP?

More clusters generally permit a lower best within-cluster squared-distance sum. With one centroid for each of nine distinct observations, the training sum can reach zero. That does not create nine useful shared customer segments.

A small k may combine meaningfully different behaviours. A very large k can fragment the sample into groups too small to be stable or useful. The goal is a useful structure, not simply the lowest raw training sum.

Compare candidate groupings using domain relevance, stability, separation and the intended use. An elbow plot can suggest where improvements flatten, but it need not have one clear elbow. Scores such as silhouette compare cohesion and separation; neither supplies a universal correct k.

15 | STARTING CENTRES CAN CHANGE THE RESULT

K-means can settle into a local solution, so its initial centres matter. Run it from multiple suitable starting positions and compare the resulting objective and stability. More deliberate initialisation methods can help, but do not guarantee a globally optimal answer.

The two teaching layouts let you compare starting paths. They may reach the same final partition with different cluster numbers or different numbers of stages. They are not promised to produce different final partitions every time.

A fixed random seed can make an implementation’s choices reproducible. Reproducibility means you can repeat the run; it does not establish that the chosen representation or result is correct.

16 | WHAT HAPPENS IF A CLUSTER HAS NO MEMBERS?

During some runs, a centre may receive no observations. Its new mean cannot be computed from an empty set. An implementation therefore needs an empty-cluster policy, such as reinitialising that centre.

The teaching activity retains an empty centre at its previous position and reports this if it occurs. Its supplied starting layouts are chosen for clear nonempty examples. This policy is a demonstration choice, not a universal requirement for K-means.

Distance ties also need a reproducible rule. Both cases show why a description should include the algorithm’s main process while recognising that practical implementations need explicit handling of edge cases.

17 | TURN GROUPS INTO CAUTIOUS BUSINESS DESCRIPTIONS

Observed three-group pattern Possible descriptive summary What it does not establish
Lower frequency, lower basket score Less frequent smaller-basket purchasers in this window Low customer value for all time
Middle frequency, higher basket score Customers with larger typical baskets in this window A known demographic or cause
Higher frequency, lower basket score More frequent smaller-basket purchasers in this window A permanent identity or guaranteed preference

The algorithm provides memberships and feature summaries. A human can attach descriptive names afterwards, but those names are interpretations, not discovered ground truth.

A shop might investigate whether different communications or stock planning are helpful for the groups. Any intervention needs appropriate evaluation; a cluster alone does not prove an offer will cause a desired outcome. Avoid turning purchasing behaviour into unsupported assumptions about people.

18 | K-MEANS IS ONE CLUSTERING TECHNIQUE

Approach Main idea Useful distinction
K-means Alternate nearest-centroid assignment and coordinate means Requires a requested k and works naturally with numerical mean/distance structure
Hierarchical clustering Build nested groups by successively merging or splitting observations A dendrogram can show group structure at different levels
Density-based clustering Find connected dense regions under a neighbourhood rule Can identify irregular groups and leave some observations as noise

Different methods encode different ideas of a group. Standard K-means favours compact groups under Euclidean geometry and assigns every observation to a centroid, including unusual ones. Density-based approaches may leave noise unassigned.

These methods do not all use centroid means or require the same settings. K-means is the worked example here, not the definition of all clustering. No one method is best for every shape, density or feature type.

19 | OUTLIERS AND SHAPES CAN MISLEAD

A very large basket-score outlier can pull a mean centroid away from the main observations. Investigate whether it is a genuine high-spend customer, a unit error or a different population before deciding how to handle it.

Two curved or interlocking groups may not be represented well by nearest-centroid partitions. Groups with very different sizes or spreads can also create difficulties for standard K-means.

A clustering algorithm can still return a partition when the data have no clear natural grouping. Output alone is not evidence of a hidden truth. Inspect the original features, the grouping assumptions and whether a different representation or method is justified.

20 | EVALUATE CLUSTERS WITHOUT INVENTING CORRECT LABELS

Without known target-group labels, ordinary classification accuracy, precision and recall cannot simply be calculated as if a correct segment existed for every customer. Assess the structure using suitable internal measures and domain checks.

Check whether members are reasonably similar under meaningful features, whether groups are separated, whether results remain useful across resampling or time windows and whether small representation changes radically alter them.

If independent reference labels exist for a particular evaluation, external comparisons are possible. Compare memberships with measures that respect arbitrary cluster IDs; Cluster 1 and Cluster 2 can swap names without changing the partition. For a deployment task, also evaluate the downstream benefit rather than only an attractive plot.

21 | ASSIGNING A NEW CUSTOMER IS NOT THE SAME AS REFITTING

With a fitted K-means model and fixed preprocessing, a new customer can be assigned to the nearest existing centroid. That uses the learned group structure without recomputing every centre.

Refitting instead recomputes groups from a dataset, possibly changing centres and memberships. Simply assigning a new customer does not automatically update the stored centroids.

A customer far from every centre still receives a nearest-centroid assignment under the basic rule. Monitor distances and changed purchasing conditions; the assigned group does not prove the new case is well represented. Other clustering methods may handle new observations differently.

22 | COMPARE CLUSTERING, CLASSIFICATION AND ASSOCIATION

Task What is supplied or sought Purchasing example
Clustering Group observations by feature similarity without target-group labels Discover purchasing-pattern segments
Classification Use known example labels to predict a predefined category Predict whether an order will be returned
Association-rule learning Find relationships between attributes or items occurring together Explore which products appear together in baskets

These can be used in related systems, but they answer different questions. A cluster is a group of observations; an association rule describes a co-occurrence relationship; a supervised class has a target meaning defined before prediction.

The next lesson develops association rules. For now, keep the central clustering chain clear: meaningful features → similarity comparison → group formation → careful interpretation and evaluation.

23 | CHECK THE GROUPING IDEA

Choose the main concept illustrated by each scenario. Focus on what the system actually does rather than the appearance of its chart.

24 | DESCRIBE THE PROCESS AS A CONNECTED STORY

Start with the task: “A shop represents customers using comparable purchasing features without known segment labels.” Then describe the grouping: “K-means assigns customers to nearby centroids and recalculates the centroids from their members, repeating until a stopping rule is met.”

Make the relationship clear: “Customers with similar feature coordinates tend to be grouped together under the distance rule. The resulting profiles can support investigation of different purchasing patterns.”

Finish with a limit: “Feature choice, scaling, k and initialisation influence the partition. Group IDs and names are not verified customer identities, so stability and usefulness must be checked.” This addresses the syllabus’s customer-base segmentation application without claiming that every dataset contains one correct segmentation.

Further reading: K-means stages and starting positions and clustering approaches and evaluation.

MATCH THE CLUSTERING IDEA TO ITS ROLE

Terminology

Terminology

Clustering

Grouping observations according to similarities in their features.

Unsupervised learning

Learning structure without known target labels guiding the fit.

Feature

An input attribute describing an observation.

Observation

One entity or example being grouped, such as a customer.

Cluster

A group produced under the chosen similarity and method.

Cluster ID

An arbitrary identifier assigned to a group, not a verified semantic label.

Customer segmentation

Grouping customers by selected attributes or behaviour patterns.

Similarity

Closeness or resemblance under the chosen representation and comparison rule.

Euclidean distance

The square root of the sum of squared coordinate differences.

Feature scaling

Transforming input magnitudes consistently so scales do not unintentionally dominate.

K-means

Clustering through nearest-centroid assignments and mean-coordinate updates.

k in K-means

The requested number of clusters, not the number of labelled neighbours.

Centroid

The coordinate-wise mean of a cluster’s members.

Initialisation

Choosing starting centroid positions or other initial model state.

Assignment stage

Assigning each observation to its nearest current centroid.

Update stage

Recomputing each nonempty cluster’s centroid from the current members.

Iteration

A repeated cycle of grouping steps under a defined procedure.

Convergence

Reaching a specified stopping condition, not proof of a globally best grouping.

Inertia

The sum of squared distances from observations to their assigned centroids.

Local solution

A result reached from a starting configuration that may not be globally best.

Empty cluster

A centroid with no currently assigned observations, requiring a handling policy.

Outlier

An unusual observation that may be valid or erroneous and can influence mean centroids.

Cohesion

How closely members of a cluster relate under the chosen representation.

Separation

How distinct clusters are under an appropriate comparison.

Stability

How consistent useful grouping structure remains across suitable changes or repeats.

Hierarchical clustering

An approach building nested grouping relationships.

Density-based clustering

An approach finding connected dense regions and potentially identifying noise.

Dendrogram

A tree-like display of hierarchical grouping relationships.

Refitting

Recomputing a model from fitting data rather than merely assigning a new case.

Association rule

A relationship describing co-occurrence of items or attributes, rather than a cluster of observations.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Which describe unsupervised clustering?
2. Which are sensible segmentation inputs or preparations?
3. Which distinguish K-means and K-NN?
4. Which describe K-means stages?
5. For members (2,1), (8,2), (8,3), (9,2), which are correct?
6. Which are true of initialisation and convergence?
7. Which are correct objective statements?
8. Which are method-dependent?
9. Which are cautious interpretations?
10. Which distinguish assignment and refitting?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Describe how clustering can segment a customer base without labelled segments.

2. Describe how clustering differs from supervised classification.

3. Calculate the Euclidean distance between (1,1) and (8,2), and describe what it means.

4. Describe the assignment and update stages of K-means.

5. Calculate the centroid for (2,1), (8,2), (8,3) and (9,2).

6. Describe why C3 changes group after the first centre update in the worked example.

7. Describe why the smallest inertia alone does not determine the best k.

8. Describe how scaling and feature choice affect grouping.

9. Describe two limitations of standard K-means.

10. Describe how hierarchical and density-based approaches differ from K-means.

11. Describe how to evaluate clustering when no true group labels exist.

12. Describe how a new customer can be handled by a fitted K-means model.

DESCRIBE IT | FROM PURCHASES TO GROUPS

A shop records purchase frequency and typical basket value without known customer segment labels. Describe the preparation and similarity comparison, the repeated K-means stages and how the resulting groups might be interpreted. Include two limitations and a suitable way to assess usefulness without inventing true segment labels.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

Selections are kept while this page is open.

Workbook

Workbook

COMING SOON

The workbook for A4.3.4 Clustering (HL) is coming soon. Complete the matching task and written questions in your exercise book.