A customer’s purchases can reveal more than which items occur together: their order may matter. Sequence mining looks for patterns across ordered events, such as customers viewing one kind of movie and then another the following month. Unlike ordinary association rules built from items in the same transaction, it examines consecutive events. It is also distinct from time-series analysis. section 4.7
An event is a collection of items in one transaction; a sequence is events in order. For example, (Milk, Cheese) → (Bread, Eggs) records two purchases, not one four-item basket. A k-item sequence contains k items across its events. SPADE, or Sequential Pattern Discovery Using Equivalence classes, is an algorithm for finding sequential patterns. section 4.7.1
To follow a SPADE-style comparison of purchase sequences:
- Keep a sequence ID for each customer history and an ordered element ID for each event. Events may contain several items. section 4.7.1
- Compare occurrences within the same sequence ID. Milk → Eggs requires an Eggs element after a Milk element; reversing their order tests a different pattern. section 4.7.1
- Join compatible ordered occurrences to examine longer patterns, such as Milk → Eggs → Milk. Set a support threshold when running SPADE to select reported patterns. section 4.7.1 section 4.11
Order changes the result
In the four-sequence grocery dataset, Milk → Eggs occurs in all four sequence IDs, while Eggs → Milk occurs in two. Joining ordered occurrences yields Milk → Eggs → Milk in sequence IDs 1001 and 1002. These counts describe the supplied histories; they do not make the two directions interchangeable. section 4.7.1
Table 4.19 Final table after we have analyzed all the combinations for the items
|
Milk, Eggs -> Milk
|
|||
|---|---|---|---|
|
Sequence ID
|
Element ID (Milk)
|
Element ID (Eggs)
|
Element ID (Milk)
|
|
1001
|
1
|
2
|
3
|
|
1002
|
1
|
3
|
4
|
Inspect the element IDs to see the increasing event order required by the three-item pattern. section 4.7.1
Run SPADE in Python
With the sequence data in SPADE_dataset.txt, the supplied pycspade invocation is `from pycspade.helpers import spade, print_result; spade_result = spade(filename='SPADE_dataset.txt', support=0.6, parse=True); print_result(spade_result)`. Check that the file represents ordered events, then inspect the printed patterns against the support setting. section 4.7.1
Patterns can inform recommendations, marketing, biological sequence analysis, or predictions about online searches. Treat them as dependent on the data used: seasonal customer behavior may change, so refresh analyses rather than assuming an old pattern still applies. section 4.7 section 4.9
