.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/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_plot_interpolation.py: Interpolation ===================== This example shows the types of interpolation used in the evaluation of FDataGrids. .. GENERATED FROM PYTHON SOURCE LINES 8-21 .. code-block:: Python # Author: Pablo Marcos Manchón # License: MIT # sphinx_gallery_thumbnail_number = 3 import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import axes3d import skfda from skfda.representation.interpolation import SplineInterpolation .. GENERATED FROM PYTHON SOURCE LINES 22-28 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 28-38 .. code-block:: Python 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/images/sphx_glr_plot_interpolation_001.png :alt: plot interpolation :srcset: /auto_examples/images/sphx_glr_plot_interpolation_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 39-43 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 43-48 .. code-block:: Python fig = fd.plot() fd.scatter(fig=fig) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_interpolation_002.png :alt: plot interpolation :srcset: /auto_examples/images/sphx_glr_plot_interpolation_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 49-56 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 56-63 .. code-block:: Python fd.interpolation = SplineInterpolation(interpolation_order=3) fig = fd.plot() fd.scatter(fig=fig) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_interpolation_003.png :alt: plot interpolation :srcset: /auto_examples/images/sphx_glr_plot_interpolation_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-68 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 68-87 .. code-block:: Python 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/images/sphx_glr_plot_interpolation_004.png :alt: plot interpolation :srcset: /auto_examples/images/sphx_glr_plot_interpolation_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-94 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 94-106 .. code-block:: Python 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/images/sphx_glr_plot_interpolation_005.png :alt: plot interpolation :srcset: /auto_examples/images/sphx_glr_plot_interpolation_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 107-109 In the following figure it is shown the result of the constant interpolation applied to the surface. .. GENERATED FROM PYTHON SOURCE LINES 109-115 .. code-block:: Python fd.interpolation = SplineInterpolation(interpolation_order=0) fig = fd.plot() fd.scatter(fig=fig) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_interpolation_006.png :alt: plot interpolation :srcset: /auto_examples/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.978 seconds) .. _sphx_glr_download_auto_examples_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/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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_