.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_cost_segmentation/plot_02_segmentogram.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_02_cost_segmentation_plot_02_segmentogram.py: The merge hierarchy: segmentogram and persistence ================================================= Bottom-up segmentation produces a whole hierarchy of segmentations in one run. The *segmentogram* shows every level; change points that survive many merges are the important ones. The cost curve and its relative reduction rate (Bankó, 2015) tell where adding segments stops paying off. .. GENERATED FROM PYTHON SOURCE LINES 10-21 .. code-block:: Python import matplotlib.pyplot as plt from mvtsa import segmentation as sg from mvtsa import viz from mvtsa.datasets import make_correlation_switching X, cps, _ = make_correlation_switching( n_segments=5, length=120, random_state=7 ) .. GENERATED FROM PYTHON SOURCE LINES 22-24 One run, all levels ------------------- .. GENERATED FROM PYTHON SOURCE LINES 24-34 .. code-block:: Python bu = sg.BottomUp("pca", initial_size=10, cost_params={"n_components": 1}) tree = bu.full_tree(X) print("levels:", tree.n_levels, "| 5 segments:", tree.cut(5), "| true:", cps) pers = tree.persistence() print( "most persistent change points:", sorted(pers, key=pers.get, reverse=True)[:4], ) viz.plot_segmentogram(tree, max_segments=12) .. image-sg:: /auto_examples/02_cost_segmentation/images/sphx_glr_plot_02_segmentogram_001.png :alt: plot 02 segmentogram :srcset: /auto_examples/02_cost_segmentation/images/sphx_glr_plot_02_segmentogram_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none levels: 60 | 5 segments: [120 240 360 480] | true: [120 240 360 480] most persistent change points: [240, 480, 120, 360]
.. GENERATED FROM PYTHON SOURCE LINES 35-37 Cost curve and relative reduction rate -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 37-42 .. code-block:: Python curve = {c: v for c, v in tree.cost_curve().items() if c <= 12} rr = sg.relative_reduction(curve) print({c: round(v, 3) for c, v in rr.items()}) viz.plot_cost_curve(curve, selected=5) plt.show() .. image-sg:: /auto_examples/02_cost_segmentation/images/sphx_glr_plot_02_segmentogram_002.png :alt: plot 02 segmentogram :srcset: /auto_examples/02_cost_segmentation/images/sphx_glr_plot_02_segmentogram_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none {2: 0.14, 3: 0.415, 4: 0.493, 5: 0.942, 6: 0.004, 7: 0.008, 8: 0.008, 9: 0.008, 10: 0.007, 11: 0.005, 12: 0.007} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.203 seconds) .. _sphx_glr_download_auto_examples_02_cost_segmentation_plot_02_segmentogram.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/02_cost_segmentation/plot_02_segmentogram.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_segmentogram.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_segmentogram.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_segmentogram.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_