.. 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_06_streaming.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_06_streaming.py: Segmentation of a data stream ============================= Three ways to segment data that arrive sample by sample: a multivariate CUSUM chart, recursive PCA (see the latent-variable examples) and a batch segmenter applied to a sliding buffer, which reports a change point once it is far enough from the end of the buffer to be stable. .. GENERATED FROM PYTHON SOURCE LINES 10-20 .. code-block:: Python import matplotlib.pyplot as plt import mvtsa as mv from mvtsa import segmentation as sg X, cps, _ = mv.datasets.make_piecewise_stationary( n_segments=6, length=200, n_variables=3, random_state=2 ) .. GENERATED FROM PYTHON SOURCE LINES 21-23 Windowed batch segmentation --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-32 .. code-block:: Python stream = sg.StreamSegmenter( sg.PELT("l2", jump=5), window=300, step=25, guard=20 ) reported = [] for t, x in enumerate(X): for c in stream.partial_fit(x): reported.append((t, c)) for t, c in reported: print(f"at sample {t:5d}: change point {c} (delay {t - c})") .. rst-class:: sphx-glr-script-out .. code-block:: none at sample 229: change point 200 (delay 29) at sample 429: change point 400 (delay 29) at sample 629: change point 600 (delay 29) at sample 829: change point 800 (delay 29) at sample 1029: change point 1000 (delay 29) .. GENERATED FROM PYTHON SOURCE LINES 33-35 Multivariate CUSUM ------------------ .. GENERATED FROM PYTHON SOURCE LINES 35-48 .. code-block:: Python cusum = sg.CUSUMDetector(k=0.5, h=25, warmup=50) seg = cusum.segment(X) print("CUSUM:", seg.change_points, "| true:", cps) fig, axes = plt.subplots(2, 1, figsize=(7, 4.5), sharex=True) mv.viz.plot_segmentation(X, seg, truth=cps, ax=axes[0]) mv.viz.plot_profile( cusum.statistic_, seg.change_points, ax=axes[1], label="CUSUM", threshold=cusum.h, ) plt.show() .. image-sg:: /auto_examples/04_modern_segmentation/images/sphx_glr_plot_06_streaming_001.png :alt: plot 06 streaming :srcset: /auto_examples/04_modern_segmentation/images/sphx_glr_plot_06_streaming_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none CUSUM: [200 400 644 801 988] | true: [ 200 400 600 800 1000] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.232 seconds) .. _sphx_glr_download_auto_examples_04_modern_segmentation_plot_06_streaming.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_06_streaming.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_06_streaming.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_06_streaming.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_06_streaming.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_