.. 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.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.py: Magnitude-Shape Plot ==================== Shows the use of the MS-Plot applied to the Canadian Weather dataset. .. GENERATED FROM PYTHON SOURCE LINES 7-14 .. code-block:: Python # Author: Amanda Hernando Bernabé # License: MIT # sphinx_gallery_thumbnail_number = 2 .. GENERATED FROM PYTHON SOURCE LINES 18-22 First, the Canadian Weather dataset is downloaded from the package 'fda' in CRAN. It contains a FDataGrid with daily temperatures and precipitations, that is, it has a 2-dimensional image. We are interested only in the daily average temperatures, so we extract the first coordinate. .. GENERATED FROM PYTHON SOURCE LINES 22-30 .. code-block:: Python from skfda.datasets import fetch_weather X, y = fetch_weather(return_X_y=True, as_frame=True) fd = X.iloc[:, 0].array target = y.array fd_temperatures = fd.coordinates[0] .. GENERATED FROM PYTHON SOURCE LINES 39-42 The data is plotted to show the curves we are working with. They are divided according to the target. In this case, it includes the different climates to which the weather stations belong. .. GENERATED FROM PYTHON SOURCE LINES 42-56 .. code-block:: Python # Each climate is assigned a color. Defaults to grey. import matplotlib.pyplot as plt import numpy as np group_names = target.categories fd_temperatures.plot( group=target.codes, group_names=group_names, ) plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_001.png :alt: Canadian Weather :srcset: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 57-62 The MS-Plot is generated. In order to show the results, the :func:`~skfda.exploratory.visualization.MagnitudeShapePlot.plot` method is used. Note that the colors have been specified before to distinguish between outliers or not. In particular the tones of the default colormap, (which is 'seismic' and can be customized), are assigned. .. GENERATED FROM PYTHON SOURCE LINES 62-74 .. code-block:: Python from skfda.exploratory.depth.multivariate import SimplicialDepth from skfda.exploratory.visualization import MagnitudeShapePlot msplot = MagnitudeShapePlot( fd_temperatures, multivariate_depth=SimplicialDepth(), ) msplot.plot() plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_002.png :alt: Canadian Weather :srcset: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 75-77 To show the utility of the plot, the curves are plotted according to the distinction made by the MS-Plot (outliers or not) with the same colors. .. GENERATED FROM PYTHON SOURCE LINES 77-85 .. code-block:: Python fd_temperatures.plot( group=msplot.outliers.astype(int), group_colors=["blue", "red"], group_names=["nonoutliers", "outliers"], ) plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_003.png :alt: Canadian Weather :srcset: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 86-98 We can observe that most of the curves pointed as outliers belong either to the Pacific or Arctic climates which are not the common ones found in Canada. The Pacific temperatures are much smoother and the Arctic ones much lower, differing from the rest in shape and magnitude respectively. There are two curves from the Arctic climate which are not pointed as outliers but in the MS-Plot, they appear further left from the central points. This behaviour can be modified specifying the parameter alpha. Now we use the default multivariate depth from :func:`~skfda.exploratory.depth.IntegratedDepth` in the MS-Plot. .. GENERATED FROM PYTHON SOURCE LINES 98-109 .. code-block:: Python from skfda.exploratory.depth import IntegratedDepth msplot = MagnitudeShapePlot( fd_temperatures, multivariate_depth=IntegratedDepth().multivariate_depth, ) fig = msplot.plot() plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_004.png :alt: Canadian Weather :srcset: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 110-117 We can observe that almost none of the samples are pointed as outliers. Nevertheless, if we group them in three groups according to their position in the MS-Plot, the result is the expected one. Those samples at the left (larger deviation in the mean directional outlyingness) correspond to the Arctic climate, which has lower temperatures, and those on top (larger deviation in the directional outlyingness) to the Pacific one, which has smoother curves. .. GENERATED FROM PYTHON SOURCE LINES 117-133 .. code-block:: Python colors = np.array("C") + target.codes.astype(str) ax = fig.axes[0] xlim = ax.get_xlim() ylim = ax.get_ylim() fig, ax = plt.subplots() ax.scatter(msplot.points[:, 0], msplot.points[:, 1], c=colors) ax.set_title("MS-Plot") ax.set_xlabel("magnitude outlyingness") ax.set_xlim(*xlim) ax.set_ylabel("shape outlyingness") ax.set_ylim(*ylim) plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_005.png :alt: MS-Plot :srcset: /auto_examples/exploratory/images/sphx_glr_plot_magnitude_shape_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.487 seconds) .. _sphx_glr_download_auto_examples_exploratory_plot_magnitude_shape.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.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_magnitude_shape.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_magnitude_shape.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_magnitude_shape.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_