.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/10_mlops/plot_02_drift_monitoring.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_10_mlops_plot_02_drift_monitoring.py: Monitoring a deployed model for drift ===================================== A model learned on reference data is only valid while the process behaves like the reference. The :class:`~mvtsa.mlops.DriftMonitor` checks every new batch: the exceedance rates of the :math:`T^2` and :math:`Q` limits of the reference PCA model, the Krzanowski similarity of the batch model, the distribution shift of every variable (Kolmogorov--Smirnov, population stability index) and, optionally, the distribution of segment durations. A data contract catches schema and range violations before any of this. .. GENERATED FROM PYTHON SOURCE LINES 13-25 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import mvtsa as mv from mvtsa import mlops X, cps, _ = mv.datasets.make_correlation_switching( n_segments=3, length=400, n_variables=4, random_state=1 ) reference = X[:300] .. GENERATED FROM PYTHON SOURCE LINES 26-28 Data contract ------------- .. GENERATED FROM PYTHON SOURCE LINES 28-32 .. code-block:: Python contract = mlops.DataContract.infer(reference, names=["a", "b", "c", "d"]) bad = X[300:350].copy() bad[5, 2] = np.nan print(contract.check(bad, names=["a", "b", "c", "d"])) .. rst-class:: sphx-glr-script-out .. code-block:: none {'valid': False, 'violations': ['1 missing values']} .. GENERATED FROM PYTHON SOURCE LINES 33-35 Batch-wise drift monitoring --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 35-68 .. code-block:: Python mon = mlops.DriftMonitor(n_components=1, similarity_threshold=0.8).fit( reference ) for a in range(300, len(X), 100): r = mon.update(X[a : a + 100]) print( f"batch {a:4d}-{a + 100:4d}: Q exceed {r['q_exceedance']:.2f} " f"similarity {r['similarity']:.2f} " f"alerts {r['alerts']}" ) rep = mon.report() fig, ax = plt.subplots(figsize=(6, 3)) start = np.arange(300, len(X), 100) ax.plot( start, rep["similarity"], "o-", color="#285780", label="Krzanowski similarity", ) ax.plot( start, rep["q_exceedance"], "s-", color="#D55E00", label="Q exceedance rate", ) ax.axhline(0.8, color="#285780", ls=":", lw=1) for c in cps: ax.axvline(c, color="gray", ls="--", lw=0.8) ax.set_xlabel("batch start") ax.legend(frameon=False) plt.show() .. image-sg:: /auto_examples/10_mlops/images/sphx_glr_plot_02_drift_monitoring_001.png :alt: plot 02 drift monitoring :srcset: /auto_examples/10_mlops/images/sphx_glr_plot_02_drift_monitoring_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none batch 300- 400: Q exceed 0.01 similarity 1.00 alerts [] batch 400- 500: Q exceed 0.81 similarity 0.25 alerts ['Q exceedance (correlation structure changed)', 'PCA model similarity below threshold'] batch 500- 600: Q exceed 0.75 similarity 0.25 alerts ['Q exceedance (correlation structure changed)', 'PCA model similarity below threshold'] batch 600- 700: Q exceed 0.81 similarity 0.25 alerts ['Q exceedance (correlation structure changed)', 'PCA model similarity below threshold'] batch 700- 800: Q exceed 0.77 similarity 0.25 alerts ['Q exceedance (correlation structure changed)', 'PCA model similarity below threshold'] batch 800- 900: Q exceed 0.84 similarity 0.00 alerts ['Q exceedance (correlation structure changed)', 'PCA model similarity below threshold'] batch 900-1000: Q exceed 0.84 similarity 0.00 alerts ['Q exceedance (correlation structure changed)', 'PCA model similarity below threshold', 'distribution shift of variable 1'] batch 1000-1100: Q exceed 0.77 similarity 0.00 alerts ['Q exceedance (correlation structure changed)', 'PCA model similarity below threshold'] batch 1100-1200: Q exceed 0.79 similarity 0.00 alerts ['Q exceedance (correlation structure changed)', 'PCA model similarity below threshold'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.066 seconds) .. _sphx_glr_download_auto_examples_10_mlops_plot_02_drift_monitoring.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/10_mlops/plot_02_drift_monitoring.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_drift_monitoring.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_drift_monitoring.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_drift_monitoring.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_