.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/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_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-19 .. code-block:: Python # Author: Carlos Ramos CarreƱo # License: MIT # sphinx_gallery_thumbnail_number = 3 import matplotlib.pyplot as plt import numpy as np import skfda from skfda.exploratory.visualization import MagnitudeShapePlot .. GENERATED FROM PYTHON SOURCE LINES 20-21 First, we generate a synthetic dataset following [DaWe18]_ .. GENERATED FROM PYTHON SOURCE LINES 21-33 .. code-block:: Python random_state = np.random.RandomState(0) n_samples = 200 fd = skfda.datasets.make_gaussian_process( n_samples=n_samples, n_features=100, cov=skfda.misc.covariances.Exponential(), mean=lambda t: 4 * t, random_state=random_state, ) .. GENERATED FROM PYTHON SOURCE LINES 34-35 We now add the outliers .. GENERATED FROM PYTHON SOURCE LINES 35-94 .. code-block:: Python magnitude_outlier = skfda.datasets.make_gaussian_process( n_samples=1, n_features=100, cov=skfda.misc.covariances.Exponential(), mean=lambda t: 4 * t + 20, random_state=random_state, ) shape_outlier_shift = skfda.datasets.make_gaussian_process( n_samples=1, n_features=100, cov=skfda.misc.covariances.Exponential(), mean=lambda t: 4 * t + 10 * (t > 0.4), random_state=random_state, ) shape_outlier_peak = skfda.datasets.make_gaussian_process( n_samples=1, n_features=100, cov=skfda.misc.covariances.Exponential(), mean=lambda t: 4 * t - 10 * ((0.25 < t) & (t < 0.3)), random_state=random_state, ) shape_outlier_sin = skfda.datasets.make_gaussian_process( n_samples=1, n_features=100, cov=skfda.misc.covariances.Exponential(), mean=lambda t: 4 * t + 2 * np.sin(18 * t), random_state=random_state, ) shape_outlier_slope = skfda.datasets.make_gaussian_process( n_samples=1, n_features=100, cov=skfda.misc.covariances.Exponential(), mean=lambda t: 10 * t, random_state=random_state, ) magnitude_shape_outlier = skfda.datasets.make_gaussian_process( n_samples=1, n_features=100, cov=skfda.misc.covariances.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 95-96 The data is plotted to show the curves we are working with. .. GENERATED FROM PYTHON SOURCE LINES 96-103 .. code-block:: Python labels = [0] * n_samples + [1] * 6 fd.plot( group=labels, group_colors=['lightgrey', 'black'], ) .. image-sg:: /auto_examples/images/sphx_glr_plot_magnitude_shape_synthetic_001.png :alt: plot magnitude shape synthetic :srcset: /auto_examples/images/sphx_glr_plot_magnitude_shape_synthetic_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 104-107 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 107-112 .. code-block:: Python msplot = MagnitudeShapePlot(fd) msplot.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_magnitude_shape_synthetic_002.png :alt: plot magnitude shape synthetic :srcset: /auto_examples/images/sphx_glr_plot_magnitude_shape_synthetic_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 113-115 To show the utility of the plot, the curves are plotted showing each outlier in a different color .. GENERATED FROM PYTHON SOURCE LINES 115-132 .. 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, ) .. image-sg:: /auto_examples/images/sphx_glr_plot_magnitude_shape_synthetic_003.png :alt: plot magnitude shape synthetic :srcset: /auto_examples/images/sphx_glr_plot_magnitude_shape_synthetic_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 133-134 We now show the points in the MS-plot using the same colors .. GENERATED FROM PYTHON SOURCE LINES 134-146 .. code-block:: Python fig = plt.figure() ax = fig.add_subplot(1, 1, 1) 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") .. image-sg:: /auto_examples/images/sphx_glr_plot_magnitude_shape_synthetic_004.png :alt: MS-Plot :srcset: /auto_examples/images/sphx_glr_plot_magnitude_shape_synthetic_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(47.097222222222214, 0.5, 'shape outlyingness') .. GENERATED FROM PYTHON SOURCE LINES 147-151 .. 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 1.872 seconds) .. _sphx_glr_download_auto_examples_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/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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_