Sub topic 5
Turning behaviour into features
This chapter assumes you can read basic Python and pandas, know what recency, frequency and conversion mean in marketing, and have seen the customer data foundation from earlier chapters. Here you will turn raw events into the numeric columns that clustering and prediction models need.
Why features matter
A feature is a numeric column that describes one customer in a way a model can use. Raw data is usually a log of events: orders, page views, email opens. A model cannot learn from a log directly. It needs one row per customer, with one column per measurable trait.
The quality of your segments depends more on these columns than on the choice of algorithm. A good algorithm run on weak features gives groups nobody can act on.
RFM: the standard starting point
RFM summarises purchase behaviour with three features:
- Recency: days since the last purchase. Lower means more engaged.
- Frequency: number of purchases in a time window.
- Monetary: total spend in the same window.
You compute them from an order table, using a fixed reference date so the numbers are reproducible.
import pandas as pd
orders = pd.DataFrame({
"customer_id": [1, 1, 1, 2, 2, 3],
"order_date": pd.to_datetime([
"2026-11-20", "2026-11-01", "2026-09-15",
"2026-10-02", "2026-06-10", "2026-03-05",
]),
"amount": [60.0, 45.0, 80.0, 30.0, 25.0, 120.0],
})
as_of = pd.Timestamp("2026-11-26")
window_start = as_of - pd.Timedelta(days=180)
recent = orders[(orders["order_date"] >= window_start) & (orders["order_date"] <= as_of)]
rfm = recent.groupby("customer_id").agg(
last_order=("order_date", "max"),
frequency=("order_date", "count"),
monetary=("amount", "sum"),
)
rfm["recency_days"] = (as_of - rfm["last_order"]).dt.days
rfm = rfm.drop(columns="last_order")
print(rfm)Customer 3 bought only once, 266 days before the reference date. That order falls outside the 180-day window, so the customer does not appear in rfm. You must decide how to handle such customers, for example by adding them back with zero frequency and a capped recency.
Beyond RFM: behavioural features
RFM ignores browsing and engagement. Common additions include:
- Sessions in the last 30 days.
- Share of orders that used a discount.
- Number of distinct product categories bought.
- Email click rate over the last 90 days.
- Average days between orders.
Each one should answer a question a marketer could act on. Discount share, for instance, tells you who needs an incentive and who does not.
Preparing features for a model
Raw features need cleaning before modelling.
- Fix the time window. Compute every feature as of one date. Mixing windows makes columns incomparable.
- Handle missing values. A customer with no email history has no click rate. Fill with 0 only if "no activity" is the true meaning. Otherwise add a flag column such as
has_email_history. - Tame skew. Monetary values often have a long tail. A log transform, such as
np.log1p, stops a few big spenders dominating. - Scale. Distance-based methods like k-means treat a column measured in hundreds of pounds as more important than one measured in single digits. Standardise so each column has mean 0 and standard deviation 1.
Common errors
Other frequent problems:
- Correlated features. Spend and order count often move together. Including both double-counts one trait in distance calculations.
- Too many features. Twenty columns make clusters hard to interpret. Start with five to eight.
- Unstable definitions. If "active" means 30 days in one pipeline and 60 in another, segments will not match across tools. Record each definition in one shared place.
- Using identifiers as features. A customer ID has no behavioural meaning.
Exercise
You compute monetary value as total spend over each customer's whole lifetime, but recency over the last 90 days only. Name two problems this causes and how to fix them.
Show answer
The windows differ, so the columns describe different periods and are not comparable. Long-standing customers also get higher monetary values simply because they have had more time to spend. Fix both by computing every feature over the same fixed window, as of the same reference date.