An everyday example: daily routines#

Three hundred days of three kinds of people — early birds, commuters and remote workers — each a sequence of activities from waking up to going to sleep. The raw sequences are summarized by a sunburst (rings = steps, angle = number of days), the frequent sequences by a prefix tree, and the most typical patterns by a glyph table. Patterns may skip activities (max_gap=2 allows one activity in between).

Look at the data first#

import matplotlib.pyplot as plt

import spmviz as sv
from spmviz.viz import plot_patterns, plot_prefix_tree, plot_sunburst
db = sv.datasets.make_daily_routines(300, random_state=0)
print(db.stats())
print(sv.format_pattern(db[0]))
plot_sunburst(db, max_depth=4, title="how the days begin")
how the days begin
n_sequences            300.000000
n_items                 19.000000
n_elements            3853.000000
avg_length              12.843333
min_length               9.000000
max_length              16.000000
avg_itemset_size         1.000000
avg_distinct_items      10.953333
density                  0.576491
dtype: float64
wake up → shower → commute → work → meeting → lunch → work → commute → gym → tv → sleep

<Axes: title={'left': 'how the days begin'}>

Frequent sequences with a small gap#

ps = sv.mine(db, min_support=0.3, max_gap=2, max_length=6)
print(ps)
plot_prefix_tree(ps, max_depth=4, max_children=3, title="frequent sequences as a tree (three most frequent continuations per node)")
frequent sequences as a tree (three most frequent continuations per node)
PatternSet(n_patterns=96, algorithm='cmspam', min_support=0.3, n_sequences=300)

<Axes: title={'left': 'frequent sequences as a tree (three most frequent continuations per node)'}>

The ten best patterns of length at least three#

plot_patterns(ps.filter(min_length=3), top=10)
top 10 patterns by support, rel support, confidence
<Figure size 975x480 with 3 Axes>

Parameter study: the gap#

Without a gap constraint, patterns may join activities hours apart and their number grows; with max_gap=1 only directly consecutive activities count.

for gap in (1, 2, 3, None):
    n = len(sv.mine(db, 0.3, max_gap=gap, max_length=6))
    print(f"max_gap={gap}: {n} patterns")
plt.show()
max_gap=1: 25 patterns
max_gap=2: 96 patterns
max_gap=3: 346 patterns
max_gap=None: 1425 patterns

Total running time of the script: (0 minutes 0.597 seconds)

Gallery generated by Sphinx-Gallery