.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/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_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-21 .. code-block:: Python # Author: Amanda Hernando Bernabé # License: MIT # sphinx_gallery_thumbnail_number = 2 import matplotlib.pyplot as plt import numpy as np from skfda import datasets from skfda.exploratory.depth import IntegratedDepth from skfda.exploratory.depth.multivariate import SimplicialDepth from skfda.exploratory.visualization import MagnitudeShapePlot .. GENERATED FROM PYTHON SOURCE LINES 22-26 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 26-31 .. code-block:: Python X, y = datasets.fetch_weather(return_X_y=True, as_frame=True) fd = X.iloc[:, 0].values fd_temperatures = fd.coordinates[0] target = y.values .. GENERATED FROM PYTHON SOURCE LINES 32-35 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 35-48 .. code-block:: Python # Each climate is assigned a color. Defaults to grey. colormap = plt.cm.get_cmap('seismic') label_names = target.categories nlabels = len(label_names) label_colors = colormap(np.arange(nlabels) / (nlabels - 1)) fd_temperatures.plot( group=target.codes, group_colors=label_colors, group_names=label_names, ) .. image-sg:: /auto_examples/images/sphx_glr_plot_magnitude_shape_001.png :alt: Canadian Weather :srcset: /auto_examples/images/sphx_glr_plot_magnitude_shape_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/fda/checkouts/stable/examples/plot_magnitude_shape.py:37: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap(obj)`` instead. colormap = plt.cm.get_cmap('seismic')
.. GENERATED FROM PYTHON SOURCE LINES 49-54 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 54-67 .. code-block:: Python msplot = MagnitudeShapePlot( fd_temperatures, multivariate_depth=SimplicialDepth(), ) color = 0.3 outliercol = 0.7 msplot.color = color msplot.outliercol = outliercol msplot.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_magnitude_shape_002.png :alt: Canadian Weather :srcset: /auto_examples/images/sphx_glr_plot_magnitude_shape_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 68-70 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 70-77 .. code-block:: Python fd_temperatures.plot( group=msplot.outliers.astype(int), group_colors=msplot.colormap([color, outliercol]), group_names=['nonoutliers', 'outliers'], ) .. image-sg:: /auto_examples/images/sphx_glr_plot_magnitude_shape_003.png :alt: Canadian Weather :srcset: /auto_examples/images/sphx_glr_plot_magnitude_shape_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 78-90 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 90-100 .. code-block:: Python msplot = MagnitudeShapePlot( fd_temperatures, multivariate_depth=IntegratedDepth().multivariate_depth, ) msplot.color = color msplot.outliercol = outliercol msplot.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_magnitude_shape_004.png :alt: Canadian Weather :srcset: /auto_examples/images/sphx_glr_plot_magnitude_shape_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 101-108 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 108-128 .. code-block:: Python group1 = np.where(msplot.points[:, 0] < -0.6) group2 = np.where(msplot.points[:, 1] > 0.12) colors = np.copy(msplot.outliers).astype(float) colors[:] = color colors[group1] = outliercol colors[group2] = 0.9 fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.scatter(msplot.points[:, 0], msplot.points[:, 1], c=colormap(colors)) ax.set_title("MS-Plot") ax.set_xlabel("magnitude outlyingness") ax.set_ylabel("shape outlyingness") labels = np.copy(msplot.outliers.astype(int)) labels[group1] = 1 labels[group2] = 2 .. image-sg:: /auto_examples/images/sphx_glr_plot_magnitude_shape_005.png :alt: MS-Plot :srcset: /auto_examples/images/sphx_glr_plot_magnitude_shape_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 129-130 We now plot the curves with their corresponding color: .. GENERATED FROM PYTHON SOURCE LINES 130-135 .. code-block:: Python fd_temperatures.plot( group=labels, group_colors=colormap([color, outliercol, 0.9]), ) .. image-sg:: /auto_examples/images/sphx_glr_plot_magnitude_shape_006.png :alt: Canadian Weather :srcset: /auto_examples/images/sphx_glr_plot_magnitude_shape_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.530 seconds) .. _sphx_glr_download_auto_examples_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/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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_