.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/exploratory/plot_magnitude_shape_synthetic.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_exploratory_plot_magnitude_shape_synthetic.py: Magnitude-Shape Plot synthetic example ====================================== Shows the use of the MS-Plot applied to a synthetic dataset. .. GENERATED FROM PYTHON SOURCE LINES 7-14 .. code-block:: Python # Author: Carlos Ramos CarreƱo # License: MIT # sphinx_gallery_thumbnail_number = 3 .. GENERATED FROM PYTHON SOURCE LINES 19-20 First, we generate a synthetic dataset following [DaWe18]_ .. GENERATED FROM PYTHON SOURCE LINES 20-37 .. code-block:: Python import numpy as np from skfda.datasets import make_gaussian_process from skfda.misc.covariances import Exponential random_state = np.random.RandomState(0) n_samples = 200 fd = make_gaussian_process( n_samples=n_samples, n_features=100, cov=Exponential(), mean=lambda t: 4 * t, random_state=random_state, ) .. GENERATED FROM PYTHON SOURCE LINES 38-39 We now add the outliers .. GENERATED FROM PYTHON SOURCE LINES 39-98 .. code-block:: Python magnitude_outlier = make_gaussian_process( n_samples=1, n_features=100, cov=Exponential(), mean=lambda t: 4 * t + 20, random_state=random_state, ) shape_outlier_shift = make_gaussian_process( n_samples=1, n_features=100, cov=Exponential(), mean=lambda t: 4 * t + 10 * (t > 0.4), random_state=random_state, ) shape_outlier_peak = make_gaussian_process( n_samples=1, n_features=100, cov=Exponential(), mean=lambda t: 4 * t - 10 * ((0.25 < t) & (t < 0.3)), random_state=random_state, ) shape_outlier_sin = make_gaussian_process( n_samples=1, n_features=100, cov=Exponential(), mean=lambda t: 4 * t + 2 * np.sin(18 * t), random_state=random_state, ) shape_outlier_slope = make_gaussian_process( n_samples=1, n_features=100, cov=Exponential(), mean=lambda t: 10 * t, random_state=random_state, ) magnitude_shape_outlier = make_gaussian_process( n_samples=1, n_features=100, cov=Exponential(), mean=lambda t: 4 * t + 2 * np.sin(18 * t) - 20, random_state=random_state, ) fd = fd.concatenate( magnitude_outlier, shape_outlier_shift, shape_outlier_peak, shape_outlier_sin, shape_outlier_slope, magnitude_shape_outlier, ) .. GENERATED FROM PYTHON SOURCE LINES 99-100 The data is plotted to show the curves we are working with. .. GENERATED FROM PYTHON SOURCE LINES 100-111 .. code-block:: Python import matplotlib.pyplot as plt labels = [0] * n_samples + [1] * 6 fd.plot( group=labels, group_colors=["lightgrey", "black"], ) plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_synthetic_001.png :alt: plot magnitude shape synthetic :srcset: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_synthetic_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 112-115 The MS-Plot is generated. In order to show the results, the :func:`~skfda.exploratory.visualization.MagnitudeShapePlot.plot` method is used. .. GENERATED FROM PYTHON SOURCE LINES 115-123 .. code-block:: Python from skfda.exploratory.visualization import MagnitudeShapePlot msplot = MagnitudeShapePlot(fd) msplot.plot() plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_synthetic_002.png :alt: plot magnitude shape synthetic :srcset: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_synthetic_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 124-126 To show the utility of the plot, the curves are plotted showing each outlier in a different color .. GENERATED FROM PYTHON SOURCE LINES 126-144 .. code-block:: Python labels = [0] * n_samples + [1, 2, 3, 4, 5, 6] colors = [ "lightgrey", "orange", "blue", "black", "green", "brown", "lightblue", ] fd.plot( group=labels, group_colors=colors, ) plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_synthetic_003.png :alt: plot magnitude shape synthetic :srcset: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_synthetic_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 145-146 We now show the points in the MS-plot using the same colors .. GENERATED FROM PYTHON SOURCE LINES 146-158 .. code-block:: Python fig, ax = plt.subplots() ax.scatter( msplot.points[:, 0].ravel(), msplot.points[:, 1].ravel(), c=colors[:1] * n_samples + colors[1:], ) ax.set_title("MS-Plot") ax.set_xlabel("magnitude outlyingness") ax.set_ylabel("shape outlyingness") plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_synthetic_004.png :alt: MS-Plot :srcset: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_synthetic_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 159-163 .. rubric:: References .. [DaWe18] Dai, Wenlin, and Genton, Marc G. "Multivariate functional data visualization and outlier detection." Journal of Computational and Graphical Statistics 27.4 (2018): 923-934. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.761 seconds) .. _sphx_glr_download_auto_examples_exploratory_plot_magnitude_shape_synthetic.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/GAA-UAM/scikit-fda/develop?filepath=examples/exploratory/plot_magnitude_shape_synthetic.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_magnitude_shape_synthetic.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_magnitude_shape_synthetic.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_magnitude_shape_synthetic.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_