Sub topic 4
Rule-based, clustered and predicted segments
This chapter assumes you know what a customer segment is and can read basic pandas code. It also assumes you are comfortable with terms such as conversion rate and churn (customers who stop buying). Later chapters cover consent, feature building, clustering and predictive models in detail. Here you learn how the three segment types differ, so you can pick the right one for a given job.
What a segment is, in practice
A segment is a named group of customers that receives the same treatment. The treatment might be a message, an offer or a page layout. A segment is only useful if three things are true:
- Membership is well defined, so you can say why a customer is in it.
- The group is large enough to act on and to measure.
- Someone has decided how the treatment should differ from the default.
The three approaches differ in how membership is decided. Rules are written by people. Clusters are discovered by an algorithm. Predicted segments are defined by a model's estimate of a future outcome.
Rule-based segments
A rule-based segment uses explicit conditions on known attributes. Examples are "ordered in the last 30 days", "country is Germany" and "cart value above 100 with no purchase".
Rules are easy to explain, audit and change. A marketer can read the rule and know who receives the message. Rules also work when you have little data, because they encode business knowledge instead of learning it.
Rules have limits:
- They only capture patterns someone thought of in advance.
- Thresholds are arbitrary. A customer 91 days after their last order is "lapsed", but one at 89 days is not, though they behave almost the same.
- Rule sets grow. After a year you may have 80 overlapping rules that nobody fully understands.
- Overlap needs an explicit policy. A customer can match several rules at once.
The next example shows the overlap problem. np.select returns the label of the first condition that matches, so the order of the conditions is the priority order.
import numpy as np
import pandas as pd
customers = pd.DataFrame({
"customer_id": [1, 2, 3, 4, 5],
"orders_12m": [0, 12, 3, 4, 11],
"days_since_last_order": [400, 5, 120, 20, 100],
"revenue_12m": [0.0, 640.0, 150.0, 90.0, 700.0],
})
conditions = [
customers["orders_12m"] == 0,
customers["days_since_last_order"] > 90,
(customers["orders_12m"] >= 10) & (customers["revenue_12m"] >= 500),
]
labels = ["prospect", "lapsed", "vip"]
customers["segment"] = np.select(conditions, labels, default="active")
print(customers[["customer_id", "segment"]])Customer 5 meets the VIP rule, with 11 orders and 700 in revenue. The customer is labelled lapsed, because the lapsed rule comes first and 100 days have passed since their last order. If you want high-value customers to get a win-back message instead of a generic one, you need a separate "lapsed VIP" rule placed above both. Priority order is a business decision. Write it down.
Clustered segments
A clustering algorithm groups customers who look similar across many attributes, without being told what the groups are. This is called unsupervised learning, because there is no target column to predict. The usual inputs are behaviour features such as recency, frequency and monetary value, which the next chapters build. A common algorithm is k-means, which assigns each customer to the nearest of k group centres.
Clusters can reveal groups nobody wrote a rule for. Examples are customers who buy rarely but spend heavily, or customers who browse a lot and never buy. They also adapt to the data: when behaviour shifts, re-running the algorithm moves the boundaries.
Clusters have their own failure modes:
- No names. The algorithm returns numbers such as 0, 1 and 2. A person must read each cluster's averages and give it a business meaning.
- Instability. A different random seed, a different date or a new feature can change the groups. Cluster 2 last month may not be cluster 2 this month.
- Choice of k. You must choose the number of clusters. There is rarely one correct answer.
- Scaling sensitivity. Features on large scales, such as revenue in the thousands, dominate features on small scales, such as order counts. You must standardise features first.
- No link to outcomes. Customers can be similar in behaviour and still respond differently to an offer.
Treat a cluster as a hypothesis about a useful group. You confirm it by testing a treatment on that group.
Predicted segments
A predicted segment is defined by a model's estimate of a future outcome for each customer. This is supervised learning, because the model trains on historical examples where the outcome is known. Typical outcomes are:
- Propensity: the probability the customer will buy, click or upgrade.
- Churn risk: the probability the customer will stop buying within a period.
- Expected value: the predicted revenue over the next 12 months.
The model produces a score per customer. You turn the score into a segment by cutting it, for example "top 10% churn risk" or "propensity above 0.3". The segment is then tied directly to an outcome you care about.
The costs are higher too:
- You need a clear outcome definition and enough history, including enough positive cases.
- Features must not leak the answer. A feature that is only recorded after the outcome happens makes the model look great in training and useless in production.
- A score tells you who is likely to act. It does not tell you who will act because of your message. Someone likely to buy anyway does not need a discount.
- Models need monitoring, because the relationship between features and outcomes changes over time.
Comparing the three
| Property | Rule-based | Clustered | Predicted |
|---|---|---|---|
| Who defines membership | A person | An algorithm, then named by a person | A model plus a score cut-off |
| Data needed | Little | Moderate, many customers and features | Large, with labelled outcomes |
| Explainability | Highest | Medium, via cluster averages | Lowest, often needs feature importance |
| Stability over time | Fixed until edited | Can shift between runs | Drifts as behaviour changes |
| Main risk | Arbitrary thresholds, rule sprawl | Meaningless or unstable groups | Leakage, and targeting people who would act anyway |
| Best for | Compliance, lifecycle stages, clear business logic | Discovering structure, shaping a strategy | Prioritising action toward one outcome |
Choosing an approach
The decision follows from your question and your data, not from which method is newest.
In practice, mature programmes combine all three:
- Rules handle fixed requirements. Examples are excluding customers who withdrew consent, or treating new customers as their own stage.
- Clusters describe the broad customer base and guide content strategy.
- Predicted scores rank customers inside a segment, for example by churn risk.
A rule that suppresses contact for people who opted out always wins over any score. Rules are the safety layer around the models.
Common mistakes
- Starting with a model when rules would do. If the question is "who has not ordered in 90 days", a one-line rule is cheaper, clearer and easier to defend than a model.
- Leaving overlap undefined. Without a priority order, a customer can receive two conflicting messages, or the result can depend on query order.
- Too many segments. Every segment needs its own content and its own measurement. Five segments with real differences beat fifty that nobody can staff or test.
- Treating cluster labels as permanent. Store the cluster definition, such as the scaler and centres, and track membership changes between runs.
- Judging a segment by how it looks, not by what it does. A segment is only worthwhile if different treatments produce different outcomes. That is tested with experiments, not with a nicer chart.
- Letting scores override rules. Consent, legal and brand-safety rules should be applied first, and models should only rank customers who remain eligible.
Moving between approaches
Segments often change method as a programme matures. A common path starts with rules, because they are fast and need no modelling. Clustering then shows which customers rules misclassify, for example "active" customers who behave like lapsed ones. Finally, a predictive model replaces the arbitrary 90-day threshold with a learned churn probability. When you make such a switch, keep the old rule running on a small share of traffic as a baseline. Without it you cannot tell whether the model beat the rule or only felt more sophisticated.
Exercise
A retailer wants a win-back email for customers likely to stop buying. It has three years of order history and knows which customers stopped buying in the past. A manager suggests k-means clustering on recency, frequency and monetary value. Which approach fits the goal better, and why? Name one rule that should still apply regardless of your choice.
Show answer
A predicted (churn) segment fits better. The goal is a specific future outcome, and there is labelled history showing who churned, so a supervised model can learn it directly. Clustering would only group similar customers and would not say who is likely to leave. A rule should still apply that excludes customers who withdrew marketing consent, applied before any score is used. Rules for priority, such as handling high-value customers separately, are also reasonable. Test the model's targeting against a holdout to check that the email changes behaviour, because a churn score alone does not show who the email will persuade.
Exercise
In the pandas example, change the rules so that customers with 10 or more orders and at least 500 revenue who are also past 90 days since their last order become "lapsed_vip". Where must the new condition go in the list, and what happens to customer 5?
Show answer
The new condition must come before the plain lapsed rule, because np.select uses the first match. It can be (orders_12m >= 10) & (revenue_12m >= 500) & (days_since_last_order > 90) with the label "lapsedvip", placed after the prospect rule and before the lapsed rule. Customer 5 has 11 orders, 700 revenue and 100 days since the last order, so the customer becomes `lapsedvip instead of lapsed. Customer 2 stays vip` because the customer ordered 5 days ago.