.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/08_evaluation/plot_04_tuning.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_08_evaluation_plot_04_tuning.py: Tuning segmenter parameters =========================== Parameters can be tuned against labelled series by grid search on a segmentation metric, or without labels by maximizing stability. The sensitivity analysis of a single parameter shows how robust the method is. .. GENERATED FROM PYTHON SOURCE LINES 9-28 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import mvtsa as mv from mvtsa import segmentation as sg from mvtsa.evaluation import ( evaluate_segmentation, grid_search_segmenter, sensitivity, ) train = [ mv.datasets.make_correlation_switching( n_segments=4, length=150, n_variables=4, random_state=s )[:2] for s in range(4) ] .. GENERATED FROM PYTHON SOURCE LINES 29-31 Supervised grid search ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-43 .. code-block:: Python best, table = grid_search_segmenter( lambda **p: sg.PCASegmenter(n_segments=4, **p), { "n_components": [1, 2, 3], "statistic": ["Q", "T2", "both"], "initial_size": [5, 10, 20], }, train, criterion="covering", ) print("best:", best) print(table.head(6).round(3).to_string(index=False)) .. rst-class:: sphx-glr-script-out .. code-block:: none best: {'n_components': 1, 'statistic': 'Q', 'initial_size': 5} n_components statistic initial_size score 1 Q 5 1.000 1 Q 10 1.000 2 Q 5 0.988 1 Q 20 0.935 3 Q 10 0.925 2 Q 10 0.914 .. GENERATED FROM PYTHON SOURCE LINES 44-46 Sensitivity of BOCPD to the hazard ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 46-70 .. code-block:: Python X, cps = train[0] res = sensitivity( lambda hazard: sg.BOCPD(hazard=hazard, model="full"), "hazard", [20, 50, 100, 200, 500, 1000], X, score=lambda s: evaluate_segmentation(cps, s, len(X), margin=10)["f1"], ) for r in res: print( f"hazard {r['hazard']:5d}: {r['n_segments']} segments, F1 {r['score']:.2f}" ) fig, ax = plt.subplots(figsize=(5, 3)) ax.semilogx( [r["hazard"] for r in res], [r["score"] for r in res], "o-", color="#285780", ) ax.set_xlabel("hazard") ax.set_ylabel("F1") ax.set_ylim(0, 1.05) print("mean time per run (s):", np.mean([r["time"] for r in res]).round(3)) plt.show() .. image-sg:: /auto_examples/08_evaluation/images/sphx_glr_plot_04_tuning_001.png :alt: plot 04 tuning :srcset: /auto_examples/08_evaluation/images/sphx_glr_plot_04_tuning_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none hazard 20: 4 segments, F1 1.00 hazard 50: 4 segments, F1 1.00 hazard 100: 4 segments, F1 1.00 hazard 200: 4 segments, F1 1.00 hazard 500: 4 segments, F1 1.00 hazard 1000: 4 segments, F1 1.00 mean time per run (s): 0.121 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.565 seconds) .. _sphx_glr_download_auto_examples_08_evaluation_plot_04_tuning.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/08_evaluation/plot_04_tuning.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_tuning.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_tuning.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_tuning.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_