.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_basics/plot_03_algorithms.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_01_basics_plot_03_algorithms.py: Five algorithms, one answer =========================== The package implements PrefixSpan (pattern growth), SPAM and CM-SPAM (vertical bitmaps, the algorithm used by Bántay & Abonyi, 2024), SPADE (vertical id-lists) and GSP (level-wise). They differ in speed, never in the result: the same patterns with the same supports and supporting sequences. External miners (the ``prefixspan`` package, ``seq2pat``, ``gsppy``, SPMF) return the same :class:`~spmviz.PatternSet` and can be compared in the same way. .. GENERATED FROM PYTHON SOURCE LINES 12-22 .. code-block:: Python import time import matplotlib.pyplot as plt import numpy as np import spmviz as sv db = sv.datasets.make_customer_journeys(1000, random_state=1) .. GENERATED FROM PYTHON SOURCE LINES 23-25 Run all built-in miners ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-34 .. code-block:: Python results, times = {}, {} for alg in ["prefixspan", "cmspam", "spam", "spade", "gsp"]: t0 = time.perf_counter() results[alg] = sv.mine(db, min_support=0.03, algorithm=alg, max_length=6) times[alg] = time.perf_counter() - t0 ref = results["prefixspan"] for alg, ps in results.items(): same = ps.patterns == ref.patterns and np.array_equal(ps.support, ref.support) print(f"{alg:10s} {len(ps):5d} patterns {times[alg]:6.3f} s identical={same}") .. rst-class:: sphx-glr-script-out .. code-block:: none prefixspan 890 patterns 0.069 s identical=True cmspam 890 patterns 0.041 s identical=True spam 890 patterns 0.033 s identical=True spade 890 patterns 0.101 s identical=True gsp 890 patterns 0.637 s identical=True .. GENERATED FROM PYTHON SOURCE LINES 35-38 An external back end -------------------- The ``prefixspan`` package (PyPI) is used when installed. .. GENERATED FROM PYTHON SOURCE LINES 38-44 .. code-block:: Python try: ext = sv.mine(db, 0.03, algorithm="prefixspan-py", max_length=6) print("prefixspan-py:", len(ext), "patterns, identical:", dict(zip(ext.patterns, ext.support)) == dict(zip(ref.patterns, ref.support))) except ImportError: print("the prefixspan package is not installed") .. rst-class:: sphx-glr-script-out .. code-block:: none the prefixspan package is not installed .. GENERATED FROM PYTHON SOURCE LINES 45-47 Run times --------- .. GENERATED FROM PYTHON SOURCE LINES 47-54 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 3)) ax.barh(list(times), list(times.values()), color="#285780") ax.set_xlabel("seconds") ax.set_title("same patterns, different algorithms", loc="left", fontsize=10) for s in ("top", "right"): ax.spines[s].set_visible(False) .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_03_algorithms_001.png :alt: same patterns, different algorithms :srcset: /auto_examples/01_basics/images/sphx_glr_plot_03_algorithms_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 55-58 Parameter study: top-k instead of a threshold --------------------------------------------- When no sensible threshold is known, ask for the k most frequent patterns. .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. code-block:: Python top = sv.topk(db, k=10, max_length=6) print(top.to_frame()[["pattern", "support"]]) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none pattern support 0 category 443 1 exit 841 2 home 1000 3 product 617 4 search 473 5 home → category 443 6 home → exit 841 7 home → product 617 8 home → search 473 9 product → exit 458 10 home → product → exit 458 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.976 seconds) .. _sphx_glr_download_auto_examples_01_basics_plot_03_algorithms.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/spmviz/main?urlpath=lab/tree/notebooks/auto_examples/01_basics/plot_03_algorithms.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_algorithms.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_algorithms.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_algorithms.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_