.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/representation/plot_interpolation.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_representation_plot_interpolation.py: Interpolation ===================== This example shows the types of interpolation used in the evaluation of FDataGrids. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python # Author: Pablo Marcos Manchón # License: MIT # sphinx_gallery_thumbnail_number = 3 .. GENERATED FROM PYTHON SOURCE LINES 15-21 The :class:`~skfda.representation.grid.FDataGrid` class is used for datasets containing discretized functions. For the evaluation between the points of discretization, or sample points, is necessary to interpolate. We will construct an example dataset with two curves with 6 points of discretization. .. GENERATED FROM PYTHON SOURCE LINES 21-35 .. code-block:: Python import matplotlib.pyplot as plt import skfda fd = skfda.datasets.make_sinusoidal_process( n_samples=2, n_features=6, random_state=1, ) fig = fd.scatter() fig.legend(["Sample 1", "Sample 2"]) plt.show() .. image-sg:: /auto_examples/representation/images/sphx_glr_plot_interpolation_001.png :alt: plot interpolation :srcset: /auto_examples/representation/images/sphx_glr_plot_interpolation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 36-40 By default it is used linear interpolation, which is one of the simplest methods of interpolation and therefore one of the least computationally expensive, but has the disadvantage that the interpolant is not differentiable at the points of discretization. .. GENERATED FROM PYTHON SOURCE LINES 40-45 .. code-block:: Python fig = fd.plot() fd.scatter(fig=fig) plt.show() .. image-sg:: /auto_examples/representation/images/sphx_glr_plot_interpolation_002.png :alt: plot interpolation :srcset: /auto_examples/representation/images/sphx_glr_plot_interpolation_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-53 The interpolation method of the FDataGrid could be changed setting the attribute ``interpolation``. Once we have set an interpolation it is used for the evaluation of the object. Polynomial spline interpolation could be performed using the interpolation :class:`~skfda.representation.interpolation.SplineInterpolation`. In the following example a cubic interpolation is set. .. GENERATED FROM PYTHON SOURCE LINES 53-62 .. code-block:: Python from skfda.representation.interpolation import SplineInterpolation fd.interpolation = SplineInterpolation(interpolation_order=3) fig = fd.plot() fd.scatter(fig=fig) plt.show() .. image-sg:: /auto_examples/representation/images/sphx_glr_plot_interpolation_003.png :alt: plot interpolation :srcset: /auto_examples/representation/images/sphx_glr_plot_interpolation_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 63-67 Sometimes our samples are required to be monotone, in these cases it is possible to use monotone cubic interpolation with the attribute ``monotone``. A piecewise cubic hermite interpolating polynomial (PCHIP) will be used. .. GENERATED FROM PYTHON SOURCE LINES 67-88 .. code-block:: Python import numpy as np fd = fd[1] fd_monotone = fd.copy(data_matrix=np.sort(fd.data_matrix, axis=1)) fig = fd_monotone.plot(linestyle="--", label="cubic") fd_monotone.interpolation = SplineInterpolation( interpolation_order=3, monotone=True, ) fd_monotone.plot(fig=fig, label="PCHIP") fd_monotone.scatter(fig=fig, c="C1") fig.legend() plt.show() .. image-sg:: /auto_examples/representation/images/sphx_glr_plot_interpolation_004.png :alt: plot interpolation :srcset: /auto_examples/representation/images/sphx_glr_plot_interpolation_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 89-95 All the interpolations will work regardless of the dimension of the image, but depending on the domain dimension some methods will not be available. For the next examples it is constructed a surface, :math:`x_i: \mathbb{R}^2 \longmapsto \mathbb{R}`. By default, as in unidimensional samples, it is used linear interpolation. .. GENERATED FROM PYTHON SOURCE LINES 95-109 .. code-block:: Python from mpl_toolkits.mplot3d import axes3d X, Y, Z = axes3d.get_test_data(1.2) data_matrix = [Z.T] grid_points = [X[0, :], Y[:, 0]] fd = skfda.FDataGrid(data_matrix, grid_points) fig = fd.plot() fd.scatter(fig=fig) plt.show() .. image-sg:: /auto_examples/representation/images/sphx_glr_plot_interpolation_005.png :alt: plot interpolation :srcset: /auto_examples/representation/images/sphx_glr_plot_interpolation_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 110-112 In the following figure it is shown the result of the constant interpolation applied to the surface. .. GENERATED FROM PYTHON SOURCE LINES 112-118 .. code-block:: Python fd.interpolation = SplineInterpolation(interpolation_order=0) fig = fd.plot() fd.scatter(fig=fig) plt.show() .. image-sg:: /auto_examples/representation/images/sphx_glr_plot_interpolation_006.png :alt: plot interpolation :srcset: /auto_examples/representation/images/sphx_glr_plot_interpolation_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.333 seconds) .. _sphx_glr_download_auto_examples_representation_plot_interpolation.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/representation/plot_interpolation.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_interpolation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_interpolation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_interpolation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_