.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_basics/plot_01_first_steps.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_01_first_steps.py: First steps: segment, look, evaluate ==================================== A multivariate series of three variables whose correlation structure switches three times -- the means and variances stay the same, so no variable shows the changes on its own. A PCA-based bottom-up segmentation (Abonyi et al., 2005) finds them, and the result is compared with the truth. .. GENERATED FROM PYTHON SOURCE LINES 12-14 Data and segmentation --------------------- .. GENERATED FROM PYTHON SOURCE LINES 14-26 .. code-block:: Python import matplotlib.pyplot as plt import mvtsa as mv from mvtsa.evaluation import evaluate_segmentation X, true_cps, states = mv.datasets.make_correlation_switching( n_segments=4, length=150, random_state=0 ) seg = mv.segment( X, "pca", n_segments=4, n_components=1 ) # Q-statistic cost, bottom-up search print(seg) print(evaluate_segmentation(true_cps, seg, len(X), margin=5)) .. rst-class:: sphx-glr-script-out .. code-block:: none Segmentation(n_segments=4, n_samples=600, change_points=[150, 300, 450], method='PCASegmenter') {'precision': 1.0, 'recall': 1.0, 'f1': 1.0, 'tp': 3, 'covering': 1.0, 'hausdorff': 0.0, 'annotation_error': 0, 'n_true': 3, 'n_pred': 3, 'margin': 5, 'segment_ari': 1.0} .. GENERATED FROM PYTHON SOURCE LINES 27-31 The segmented series -------------------- Segments are shaded, detected change points are blue lines, the true ones orange marks at the top. .. GENERATED FROM PYTHON SOURCE LINES 31-33 .. code-block:: Python mv.viz.plot_segmentation(X, seg, truth=true_cps, names=["x1", "x2", "x3"]) .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_01_first_steps_001.png :alt: plot 01 first steps :srcset: /auto_examples/01_basics/images/sphx_glr_plot_01_first_steps_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 34-37 The same series segmented by the means only ------------------------------------------- A piecewise-constant (L2) cost cannot see correlation changes. .. GENERATED FROM PYTHON SOURCE LINES 37-46 .. code-block:: Python seg_l2 = mv.segment(X, "bottomup", cost="l2", n_segments=4) print( "L2 change points:", seg_l2.change_points, " PCA change points:", seg.change_points, ) mv.viz.plot_segmentation(X, seg_l2, truth=true_cps, names=["x1", "x2", "x3"]) plt.show() .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_01_first_steps_002.png :alt: plot 01 first steps :srcset: /auto_examples/01_basics/images/sphx_glr_plot_01_first_steps_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none L2 change points: [129 496 510] PCA change points: [150 300 450] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.727 seconds) .. _sphx_glr_download_auto_examples_01_basics_plot_01_first_steps.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/01_basics/plot_01_first_steps.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_first_steps.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_first_steps.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_first_steps.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_