.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_modern_segmentation/plot_03_clasp_fluss.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_04_modern_segmentation_plot_03_clasp_fluss.py: Shape changes: ClaSP and FLUSS ============================== When the segments differ in the *shape* of repeating patterns rather than in their statistics, subsequence methods work best. FLUSS (Gharghabi et al., 2017) counts the nearest-neighbour arcs of the matrix profile that cross every position: few crossings mean a boundary. ClaSP (Ermshaus et al., 2023) scores every split by how well a k-nearest-neighbour classifier can tell the subsequences of the two sides apart. .. GENERATED FROM PYTHON SOURCE LINES 12-31 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mvtsa import segmentation as sg from mvtsa import viz rng = np.random.default_rng(3) t = np.arange(1200) x = ( np.r_[ np.sin(2 * np.pi * t[:400] / 40), # sine 2 * (t[400:800] % 40) / 40 - 1, # sawtooth np.sign(np.sin(2 * np.pi * t[800:] / 40)), ] + 0.1 * rng.normal(size=1200) ) # square cps = [400, 800] .. GENERATED FROM PYTHON SOURCE LINES 32-34 ClaSP profile ------------- .. GENERATED FROM PYTHON SOURCE LINES 34-43 .. code-block:: Python clasp = sg.ClaSPSegmenter(width=40, n_segments=3) seg = clasp.segment(x) print("ClaSP:", seg.change_points, "| width", clasp.width_) fig, axes = plt.subplots(3, 1, figsize=(7, 5.5), sharex=True) viz.plot_segmentation(x, seg, truth=cps, ax=axes[0]) viz.plot_profile( clasp.profile_, seg.change_points, ax=axes[1], label="ClaSP score" ) .. image-sg:: /auto_examples/04_modern_segmentation/images/sphx_glr_plot_03_clasp_fluss_001.png :alt: plot 03 clasp fluss :srcset: /auto_examples/04_modern_segmentation/images/sphx_glr_plot_03_clasp_fluss_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ClaSP: [397 816] | width 40 .. GENERATED FROM PYTHON SOURCE LINES 44-46 FLUSS corrected arc curve ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 46-52 .. code-block:: Python fluss = sg.FLUSSSegmenter(width=40, n_segments=3) print("FLUSS:", fluss.segment(x).change_points) viz.plot_profile( fluss.cac_, fluss.segmentation_.change_points, ax=axes[2], label="CAC" ) .. rst-class:: sphx-glr-script-out .. code-block:: none FLUSS: [396 816] .. GENERATED FROM PYTHON SOURCE LINES 53-56 Parameter study: the subsequence width -------------------------------------- ``width="auto"`` estimates the period from the autocorrelation. .. GENERATED FROM PYTHON SOURCE LINES 56-60 .. code-block:: Python for w in (10, 20, 40, 80, "auto"): c = sg.ClaSPSegmenter(width=w, n_segments=3) print(f"width {w!s:5s}: {c.segment(x).change_points}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none width 10 : [390 811] width 20 : [392 794] width 40 : [397 816] width 80 : [814] width auto : [397 816] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.420 seconds) .. _sphx_glr_download_auto_examples_04_modern_segmentation_plot_03_clasp_fluss.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/mvtsa/main?urlpath=lab/tree/notebooks/auto_examples/04_modern_segmentation/plot_03_clasp_fluss.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_clasp_fluss.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_clasp_fluss.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_clasp_fluss.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_