.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/expand_skfda/plot_basis_subclass.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_basis_subclass.py: Creating a new basis ==================== Shows how to add new bases for FDataBasis by creating subclasses. .. Disable isort isort:skip_file .. GENERATED FROM PYTHON SOURCE LINES 13-20 .. code-block:: Python # Author: Carlos Ramos CarreƱo # License: MIT import matplotlib.pyplot as plt import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 21-31 In this example, we want to showcase how it is possible to make new functional bases compatible with :class:`~skfda.representation.basis.FDataBasis`, by subclassing the :class:`~skfda.representation.basis.Basis` class. Suppose that we already know that our data belongs to (or can be reasonably approximated in) the functional space spanned by the basis :math:`\{f(t) = \sin(6t), g(t) = t^2\}`. We can define these two functions in Python as follows (remember than the input and output are both NumPy arrays). .. GENERATED FROM PYTHON SOURCE LINES 31-41 .. code-block:: Python def f(t): return np.sin(6 * t) def g(t): return t**2 .. GENERATED FROM PYTHON SOURCE LINES 42-54 Lets now define the functional basis. We create a subclass of :class:`~skfda.representation.basis.Basis` containing the definition of our desired basis. We need to overload the ``__init__`` method in order to add the necessary parameters for the creation of the basis. :class:`~skfda.representation.basis.Basis` requires both the domain range and the number of elements in the basis in order to work. As this particular basis has fixed size, we only expose the ``domain_range`` parameter in the constructor, but we still pass the fixed size to the parent class constructor. It is also necessary to override the protected ``_evaluate`` method, that defines the evaluation of the basis elements. .. GENERATED FROM PYTHON SOURCE LINES 54-78 .. code-block:: Python from skfda.representation.basis import Basis class MyBasis(Basis): """Basis of f and g.""" def __init__( self, *, domain_range=None, ): super().__init__(domain_range=domain_range, n_basis=2) def _evaluate( self, eval_points, ): return np.vstack([ f(eval_points), g(eval_points), ]) .. GENERATED FROM PYTHON SOURCE LINES 79-80 We can now create an instance of this basis and plot it. .. GENERATED FROM PYTHON SOURCE LINES 80-85 .. code-block:: Python basis = MyBasis() basis.plot() plt.show() .. image-sg:: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_001.png :alt: plot basis subclass :srcset: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 86-96 This simple definition already allows to represent functions and work with them, but it does not allow advanced functionality such as taking derivatives (because it does not know how to derive the basis elements!). In order to add support for that we would need to override the appropriate methods (in this case, ``_derivative_basis_and_coefs``). In this particular case, we are not interested in the derivatives, only in correct representation and evaluation. We can now test the conversion from :class:`~skfda.representation.grid.FDataGrid` to :class:`~skfda.representation.basis.FDataBasis` for elements in this space. .. GENERATED FROM PYTHON SOURCE LINES 98-100 We first define a (discretized) function in the space spanned by :math:`f` and :math:`g`. .. GENERATED FROM PYTHON SOURCE LINES 100-113 .. code-block:: Python from skfda.representation import FDataGrid t = np.linspace(0, 1, 100) data_matrix = [ 2 * f(t) + 3 * g(t), 5 * f(t) - 2 * g(t), ] X = FDataGrid(data_matrix, grid_points=t) X.plot() plt.show() .. image-sg:: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_002.png :alt: plot basis subclass :srcset: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 114-116 We can now convert it to our basis, and visually check that the representation is exact. .. GENERATED FROM PYTHON SOURCE LINES 116-121 .. code-block:: Python X_basis = X.to_basis(basis) X_basis.plot() plt.show() .. image-sg:: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_003.png :alt: plot basis subclass :srcset: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 122-124 If we inspect the coefficients, we can finally guarantee that they are the ones used in the initial definition. .. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: Python X_basis.coefficients .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 2., 3.], [ 5., -2.]]) .. GENERATED FROM PYTHON SOURCE LINES 127-135 Lets consider a more complex example. Suppose that we want to create a basis adapted to the data using the principal components. One approach could be creating a basis that computes FPCA and uses the components obtained. Note that equality should be overriden as it is no longer true that two instances of this basis with the same domain and number of elements are equal. .. GENERATED FROM PYTHON SOURCE LINES 135-166 .. code-block:: Python from skfda.preprocessing.dim_reduction import FPCA class FPCABasis(Basis): """Basis of principal components.""" def __init__( self, *, X, n_basis=1, ): super().__init__(domain_range=X.domain_range, n_basis=n_basis) self._fpca = FPCA(n_components=n_basis) self._fpca.fit(X) def _evaluate( self, eval_points, ): return self._fpca.components_(eval_points) def __eq__(self, other): return ( super().__eq__(self, other) and self._fpca.components_ == other._fpca.components_ ) .. GENERATED FROM PYTHON SOURCE LINES 167-169 We now load the temperatures from the Canadian Weather dataset and plot them. .. GENERATED FROM PYTHON SOURCE LINES 169-177 .. code-block:: Python from skfda.datasets import fetch_weather X, y = fetch_weather(return_X_y=True) X = X.coordinates[0] X.plot() plt.show() .. image-sg:: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_004.png :alt: Canadian Weather :srcset: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 178-181 We construct the new FPCA basis from the data, using 4 basis elements. We plot the basis elements, which correspond with the first 4 principal components of the data. .. GENERATED FROM PYTHON SOURCE LINES 181-186 .. code-block:: Python basis = FPCABasis(X=X, n_basis=4) basis.plot() plt.show() .. image-sg:: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_005.png :alt: plot basis subclass :srcset: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 187-188 We can now represent the original data using this basis. .. GENERATED FROM PYTHON SOURCE LINES 188-192 .. code-block:: Python X_basis = X.to_basis(basis) X_basis.plot() plt.show() .. image-sg:: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_006.png :alt: Canadian Weather :srcset: /auto_examples/expand_skfda/images/sphx_glr_plot_basis_subclass_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.906 seconds) .. _sphx_glr_download_auto_examples_expand_skfda_plot_basis_subclass.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_basis_subclass.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_basis_subclass.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_basis_subclass.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_