.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/expand_skfda/plot_new_evaluator.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_expand_skfda_plot_new_evaluator.py: Creating a new interpolation or extrapolation strategy ====================================================== Shows how to add new interpolation and extrapolation strategies. .. GENERATED FROM PYTHON SOURCE LINES 8-18 .. code-block:: Python # Author: Carlos Ramos Carreño # License: MIT import matplotlib.pyplot as plt import numpy as np from scipy.interpolate import lagrange from skfda.representation import FDataGrid .. GENERATED FROM PYTHON SOURCE LINES 19-22 In this example, we want to showcase how it is possible to make new interpolation and extrapolation strategies. These are Python callables with the following prototype: .. GENERATED FROM PYTHON SOURCE LINES 22-28 .. code-block:: Python def evaluator_prototype(fdata, eval_points, *, aligned): """Prototype of a extrapolation/interpolation strategy.""" pass .. GENERATED FROM PYTHON SOURCE LINES 29-33 Here, ``fdata`` is a :class:`~skfda.representation.FData` object, ``èval_points`` is a NumPy array with the points at which this object will be evaluated, and ``aligned`` is a boolean parameter indicating if the points are common for all samples or different for each. .. GENERATED FROM PYTHON SOURCE LINES 35-42 For example, lets consider for illustration purposes only an interpolation/extrapolation strategy that uses the `Lagrange polynomial `_ between the points of a :class:`~skfda.representation.grid.FDataGrid`. This is in general a bad idea, as when the number of points is high this polynomial has rapid oscillations between the measured points. Moreover, the implementation here is not vectorized and has low performance. .. GENERATED FROM PYTHON SOURCE LINES 42-63 .. code-block:: Python def evaluator_lagrange(fdata, eval_points, *, aligned): """Lagrange interpolation, for 1D FDataGrid only.""" grid_points = fdata.grid_points[0] result = [] for i, data in enumerate(fdata.data_matrix): polynomial = lagrange(grid_points, data) if aligned: # Same points for all observations. # The dimension is n_points x dim_domain (1 in this case). result.append(polynomial(eval_points)) else: # Different points for every observation. # The dimension is n_samples x n_points x dim_domain. result.append(polynomial(eval_points[i])) return np.array(result) .. GENERATED FROM PYTHON SOURCE LINES 64-70 We can now create a new :class:`~skfda.representation.grid.FDataGrid` and plot it. Note that the plot uses the specified interpolation between the measured points. Note also that is not necessary to specify the extrapolation, as by default for :class:`~skfda.representation.grid.FDataGrid` it already calls the interpolation if no extrapolation is defined. .. GENERATED FROM PYTHON SOURCE LINES 70-81 .. code-block:: Python X = FDataGrid( [[0, 1, 2], [0, 1, 8], [0, 0, 0]], grid_points=[0, 1, 2], interpolation=evaluator_lagrange, ) X.plot() plt.show() .. image-sg:: /auto_examples/expand_skfda/images/sphx_glr_plot_new_evaluator_001.png :alt: plot new evaluator :srcset: /auto_examples/expand_skfda/images/sphx_glr_plot_new_evaluator_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 82-85 We can try to evaluate the function at different points, including some between measurements or outside the original range, to test the interpolation and extrapolation. .. GENERATED FROM PYTHON SOURCE LINES 85-88 .. code-block:: Python X([-1, 0, 0.5, 1, 2, 3]) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[[-1. ], [ 0. ], [ 0.5 ], [ 1. ], [ 2. ], [ 3. ]], [[ 5. ], [ 0. ], [-0.25], [ 1. ], [ 8. ], [21. ]], [[ 0. ], [ 0. ], [ 0. ], [ 0. ], [ 0. ], [ 0. ]]]) .. GENERATED FROM PYTHON SOURCE LINES 89-91 We can also try to evaluate each observation at different points to test this behavior. .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: Python X([[-1, 0], [0.5, 1], [2, 3]], aligned=False) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[[-1. ], [ 0. ]], [[-0.25], [ 1. ]], [[ 0. ], [ 0. ]]]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.115 seconds) .. _sphx_glr_download_auto_examples_expand_skfda_plot_new_evaluator.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/expand_skfda/plot_new_evaluator.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_new_evaluator.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_new_evaluator.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_