.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_explore.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_explore.py: Exploring data ============== Explores the Tecator data set by plotting the functional data and calculating means and derivatives. .. GENERATED FROM PYTHON SOURCE LINES 8-16 .. code-block:: Python # Author: Miguel Carbajo Berrocal # License: MIT import numpy as np import skfda .. GENERATED FROM PYTHON SOURCE LINES 17-24 In this example we are going to explore the functional properties of the :func:`Tecator ` dataset. This dataset measures the infrared absorbance spectrum of meat samples. The objective is to predict the fat, water, and protein content of the samples. In this example we only want to discriminate between meat with less than 20% of fat, and meat with a higher fat content. .. GENERATED FROM PYTHON SOURCE LINES 24-28 .. code-block:: Python X, y = skfda.datasets.fetch_tecator(return_X_y=True, as_frame=True) fd = X.iloc[:, 0].values fat = y['fat'].values .. GENERATED FROM PYTHON SOURCE LINES 29-31 We will now plot in red samples containing less than 20% of fat and in blue the rest. .. GENERATED FROM PYTHON SOURCE LINES 31-48 .. code-block:: Python low_fat = fat < 20 labels = np.full(fd.n_samples, 'high fat') labels[low_fat] = 'low fat' colors = { 'high fat': 'red', 'low fat': 'blue', } fig = fd.plot( group=labels, group_colors=colors, linewidth=0.5, alpha=0.7, legend=True, ) .. image-sg:: /auto_examples/images/sphx_glr_plot_explore_001.png :alt: Spectrometric curves :srcset: /auto_examples/images/sphx_glr_plot_explore_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 49-50 The means of each group are the following ones. .. GENERATED FROM PYTHON SOURCE LINES 50-64 .. code-block:: Python mean_low = skfda.exploratory.stats.mean(fd[low_fat]) mean_high = skfda.exploratory.stats.mean(fd[~low_fat]) means = mean_high.concatenate(mean_low) means.dataset_name = f"{fd.dataset_name} - means" means.plot( group=['high fat', 'low fat'], group_colors=colors, linewidth=0.5, legend=True, ) .. image-sg:: /auto_examples/images/sphx_glr_plot_explore_002.png :alt: Spectrometric curves - means :srcset: /auto_examples/images/sphx_glr_plot_explore_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 65-71 In this dataset, the vertical shift in the original trajectories is not very significative for predicting the fat content. However, the shape of the curve is very relevant. We can observe that looking at the first and second derivatives. The first derivative is shown below: .. GENERATED FROM PYTHON SOURCE LINES 71-82 .. code-block:: Python fdd = fd.derivative() fdd.dataset_name = f"{fd.dataset_name} - derivative" fig = fdd.plot( group=labels, group_colors=colors, linewidth=0.5, alpha=0.7, legend=True, ) .. image-sg:: /auto_examples/images/sphx_glr_plot_explore_003.png :alt: Spectrometric curves - derivative :srcset: /auto_examples/images/sphx_glr_plot_explore_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-84 We now show the second derivative: .. GENERATED FROM PYTHON SOURCE LINES 84-93 .. code-block:: Python fdd = fd.derivative(order=2) fdd.dataset_name = f"{fd.dataset_name} - second derivative" fig = fdd.plot( group=labels, group_colors=colors, linewidth=0.5, alpha=0.7, legend=True, ) .. image-sg:: /auto_examples/images/sphx_glr_plot_explore_004.png :alt: Spectrometric curves - second derivative :srcset: /auto_examples/images/sphx_glr_plot_explore_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.418 seconds) .. _sphx_glr_download_auto_examples_plot_explore.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_explore.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_explore.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_explore.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_