A basket tells you what goes together; a sequence tells you what comes next. Sequence-aware recommendations use the order of views or purchases to suggest the next item. This guide shows how the counting works, how to read the probabilities, and when a simpler method is enough.
Worked Example (Simulated)
Question: Which item comes next in each session?
Inputs: The six made-up sessions shown in the script, not data from a real site.
Method: Count each pair of items that sit side by side, then divide each count by the number of moves from the first item.
from collections import Counter, defaultdict
sessions = [
['A', 'B', 'C'],
['A', 'B', 'D'],
['A', 'B', 'C'],
['B', 'C', 'D'],
['A', 'C', 'D'],
['B', 'C', 'D'],
]
counts = defaultdict(Counter)
for session in sessions:
for current, following in zip(session, session[1:]):
counts[current][following] += 1
print('Sessions:', len(sessions))
for current in sorted(counts):
total = sum(counts[current].values())
details = ', '.join(
f'{item} {count} ({count / total:.2f})'
for item, count in sorted(counts[current].items())
)
print(f'From {current}: {details} ({total} transitions)')
Printed output:
Sessions: 6
From A: B 3 (0.75), C 1 (0.25) (4 transitions)
From B: C 4 (0.80), D 1 (0.20) (5 transitions)
From C: D 3 (1.00) (3 transitions)
Result: From B, C follows four times in five moves, so its chance is 0.80. From C, D follows three times in three moves, so its chance is 1.00, but that rests on only three observations. Item D has no next item in these sessions, so it has no row.
Limit: This count does not show if these picks beat a rule that picks the most common item in a later test.
Introduction
A basket of items tells you what goes together. A sequence tells you what comes next. Someone who views a camera and then a lens is in a different place from someone who views a lens and then a camera, and a recommender that ignores order treats them the same.
What is Sequence-Aware Recommendations?
A sequence-aware recommender uses the ordered history of a user or session, such as pages viewed or items purchased, to predict the next item. The simplest form counts how often item B follows item A. More flexible forms use longer histories, from Markov chains of higher order to neural sequence models.
When to Use This Technique
Use it when order carries information: next-purchase suggestions, next-page suggestions, replenishment, or onboarding steps. When items are bought together without a meaningful order, association rules are simpler. When you have little history per user, collaborative filtering may be more stable.
How It Works
A first-order approach looks only at the last item. For each item A, it counts how many times each item B followed it, then divides by the total number of times A was followed by anything. That gives an estimated probability of B next, given A. Higher-order versions condition on the last two or three items and need much more data, because the number of possible histories grows quickly.
Step-by-Step Process
- Order events by time within each user or session.
- Decide a session boundary, for example a gap in activity.
- Count transitions between consecutive items.
- Convert counts to probabilities, and require a minimum count before using a transition.
- Rank candidate next items by probability, and hold out the last item of each session to test the ranking.
Interpreting Results
A transition probability is only as reliable as the count behind it. A probability of 1.0 from three observations is much weaker than 0.6 from three thousand. Report the count next to the probability, and measure quality on held-out sessions, for example how often the true next item appears in the top three suggestions, compared with a plain popularity baseline.
Best Practices
- Start with a first-order model and a popularity baseline. Move to longer histories only if they beat the baseline on held-out data.
- Set a minimum count for any transition used in a suggestion.
- Split train and test by time, not at random, so the test sessions come after the training ones.
- Check that suggestions are not just the most popular items repeated.
Related Techniques
Association rules find items that appear together regardless of order. Market basket analysis applies the same idea to purchases. Collaborative filtering uses similar users rather than order.
Conclusion
Order is extra information, and it is worth using when the sequence matters. Start by counting transitions, report the count with every probability, and test against a popularity baseline before adding complexity.
Key Takeaway
Count what follows what, report the count with each probability, and beat a popularity baseline on held-out sessions before adding complexity.
Your turn
Bring your own data and the question you actually need answered.
MCP AnalyticsData science by email Send us your data and your question, and we will reply. ds@mcpanalytics.ai