.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/exploratory/plot_boxplot.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_boxplot.py: Boxplot ======= Shows the use of the functional Boxplot applied to the Canadian Weather dataset. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python # Author: Amanda Hernando Bernabé # License: MIT # sphinx_gallery_thumbnail_number = 2 .. GENERATED FROM PYTHON SOURCE LINES 15-19 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 will use the first coordinate. .. GENERATED FROM PYTHON SOURCE LINES 19-27 .. 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 36-39 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 to. .. GENERATED FROM PYTHON SOURCE LINES 39-51 .. code-block:: Python # Each climate is assigned a color. Defaults to grey. import matplotlib.pyplot as plt label_names = target.categories fd_temperatures.plot( group=target.codes, group_names=label_names, ) plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_boxplot_001.png :alt: Canadian Weather :srcset: /auto_examples/exploratory/images/sphx_glr_plot_boxplot_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 52-60 We instantiate a :class:`~skfda.exploratory.visualization.Boxplot` object with the data, and we call its :func:`~skfda.exploratory.visualization.Boxplot.plot` function to show the graph. By default, only the part of the outlier curves which falls out of the central regions is plotted. We want the entire curve to be shown, that is why the ``show_full_outliers`` parameter is set to True. .. GENERATED FROM PYTHON SOURCE LINES 60-69 .. code-block:: Python from skfda.exploratory.visualization import Boxplot boxplot = Boxplot(fd_temperatures) boxplot.show_full_outliers = True boxplot.plot() plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_boxplot_002.png :alt: Canadian Weather :srcset: /auto_examples/exploratory/images/sphx_glr_plot_boxplot_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-77 We can observe in the boxplot the median in black, the central region (where the 50% of the most centered samples reside) in pink and the envelopes and vertical lines in blue. The outliers detected, those samples with at least a point outside the outlying envelope, are represented with a red dashed line. The colors can be customized. The outliers are shown below with respect to the other samples. .. GENERATED FROM PYTHON SOURCE LINES 77-87 .. code-block:: Python color = 0.3 outliercol = 0.7 fd_temperatures.plot( group=boxplot.outliers.astype(int), group_names=["nonoutliers", "outliers"], ) plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_boxplot_003.png :alt: Canadian Weather :srcset: /auto_examples/exploratory/images/sphx_glr_plot_boxplot_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-102 The curves pointed as outliers are are those curves with significantly lower values than the rest. This is the expected result due to the depth measure used, :func:`~skfda.exploratory.depth.IntegratedDepth`, which ranks the samples according to their magnitude. The :class:`~skfda.exploratory.visualization.Boxplot` object admits any :ref:`depth measure ` defined or customized by the user. Now the call is done with the :class:`~skfda.exploratory.depth.ModifiedBandDepth` and the factor is reduced in order to designate some samples as outliers (otherwise, with this measure and the default factor, none of the curves are pointed out as outliers). We can see that the outliers detected belong to the Pacific and Arctic climates which are less common to find in Canada. As a consequence, this measure detects better shape outliers compared to the previous one. .. GENERATED FROM PYTHON SOURCE LINES 102-115 .. code-block:: Python from skfda.exploratory.depth import ModifiedBandDepth boxplot = Boxplot( fd_temperatures, depth_method=ModifiedBandDepth(), factor=0.4, ) boxplot.show_full_outliers = True boxplot.plot() plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_boxplot_004.png :alt: Canadian Weather :srcset: /auto_examples/exploratory/images/sphx_glr_plot_boxplot_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 116-123 Another functionality implemented in this object is the enhanced functional boxplot, which can include other central regions, apart from the central or 50% one. In the following instantiation, the :func:`~skfda.exploratory.depth.IntegratedDepth` is used and the 25% and 75% central regions are specified. .. GENERATED FROM PYTHON SOURCE LINES 123-134 .. code-block:: Python from skfda.exploratory.depth import IntegratedDepth boxplot = Boxplot( fd_temperatures, depth_method=IntegratedDepth(), prob=[0.75, 0.5, 0.25], ) boxplot.plot() plt.show() .. image-sg:: /auto_examples/exploratory/images/sphx_glr_plot_boxplot_005.png :alt: Canadian Weather :srcset: /auto_examples/exploratory/images/sphx_glr_plot_boxplot_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 135-138 The above two lines could be replaced just by ``boxplot`` inside a notebook since the default representation of the :class:`~skfda.exploratory.visualization.Boxplot` is the image of the plot. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.374 seconds) .. _sphx_glr_download_auto_examples_exploratory_plot_boxplot.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_boxplot.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_boxplot.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_boxplot.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_boxplot.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_