.. 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_04_predictive.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_04_predictive.py: Predictive homogeneity with any scikit-learn model ================================================== Two neighbouring segments belong together if no model can tell their samples apart. :class:`~mvtsa.segmentation.PredictiveSegmenter` turns this idea -- the classifier two-sample test (Lopez-Paz and Oquab, 2017) -- into a bottom-up merging criterion: the cross-validated accuracy of a classifier that predicts the side of every sample, rescaled so that chance level is zero. With ``kind="regression"`` the criterion is the loss of predicting the variables of one segment with a model trained on the other. Any scikit-learn estimator can be plugged in. .. GENERATED FROM PYTHON SOURCE LINES 14-29 .. code-block:: Python import matplotlib.pyplot as plt import pandas as pd from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis from sklearn.ensemble import RandomForestClassifier from sklearn.linear_model import LogisticRegression import mvtsa as mv from mvtsa import segmentation as sg from mvtsa.evaluation import evaluate_segmentation X, cps, _ = mv.datasets.make_piecewise_stationary( n_segments=4, length=150, n_variables=3, change="all", random_state=12 ) .. GENERATED FROM PYTHON SOURCE LINES 30-34 Three classifiers ----------------- A linear classifier sees mean shifts only; quadratic discriminant analysis and random forests also see changes of the covariance. .. GENERATED FROM PYTHON SOURCE LINES 34-57 .. code-block:: Python rows = [] for name, est in [ ("logistic", LogisticRegression(max_iter=500)), ("QDA", QuadraticDiscriminantAnalysis()), ("random forest", RandomForestClassifier(n_estimators=60, random_state=0)), ]: s = sg.PredictiveSegmenter( "classifier", estimator=est, n_segments=4, initial_size=30 ).segment(X) rows.append( {"estimator": name, **evaluate_segmentation(cps, s, len(X), margin=15)} ) print(pd.DataFrame(rows)[["estimator", "f1", "covering"]].round(3)) seg = sg.PredictiveSegmenter( "classifier", estimator=QuadraticDiscriminantAnalysis(), n_segments=4, initial_size=30, ).segment(X) mv.viz.plot_segmentation( X, seg, truth=cps, title="Predictive homogeneity (QDA)" ) plt.show() .. image-sg:: /auto_examples/04_modern_segmentation/images/sphx_glr_plot_04_predictive_001.png :alt: Predictive homogeneity (QDA) :srcset: /auto_examples/04_modern_segmentation/images/sphx_glr_plot_04_predictive_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none estimator f1 covering 0 logistic 1.0 1.0 1 QDA 1.0 1.0 2 random forest 1.0 1.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.668 seconds) .. _sphx_glr_download_auto_examples_04_modern_segmentation_plot_04_predictive.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_04_predictive.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_predictive.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_predictive.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_predictive.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_