.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_functional_regression.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_functional_regression.py: Functional Linear Regression with multivariate covariates. ========================================================== This example explores the use of the linear regression with multivariate (scalar) covariates and functional response. .. GENERATED FROM PYTHON SOURCE LINES 9-21 .. code-block:: Python # Author: Rafael Hidalgo Alejo # License: MIT import numpy as np import pandas as pd from sklearn.preprocessing import OneHotEncoder import skfda from skfda.ml.regression import LinearRegression from skfda.representation.basis import FDataBasis, FourierBasis .. GENERATED FROM PYTHON SOURCE LINES 22-28 In this example, we will demonstrate the use of the Linear Regression with functional response and multivariate covariates using the :func:`weather ` dataset. It is possible to divide the weather stations into four groups: Atlantic, Pacific, Continental and Artic. There are a total of 35 stations in this dataset. .. GENERATED FROM PYTHON SOURCE LINES 28-34 .. code-block:: Python X_weather, y_weather = skfda.datasets.fetch_weather( return_X_y=True, as_frame=True, ) fd = X_weather.iloc[:, 0].values .. GENERATED FROM PYTHON SOURCE LINES 35-42 The main goal is knowing about the effect of stations' geographic location on the shape of the temperature curves. So we will have a model with a functional response, the temperature curve, and five covariates. The first one is the intercept (all entries equal to 1) and it shows the contribution of the Canadian mean temperature. The remaining covariates use one-hot encoding, with 1 if that weather station is in the corresponding climate zone and 0 otherwise. .. GENERATED FROM PYTHON SOURCE LINES 42-50 .. code-block:: Python # We first create the one-hot encoding of the climates. enc = OneHotEncoder(handle_unknown='ignore') enc.fit([['Atlantic'], ['Continental'], ['Pacific']]) X = np.array(y_weather).reshape(-1, 1) X = enc.transform(X).toarray() .. GENERATED FROM PYTHON SOURCE LINES 51-53 Then, we construct a dataframe with each covariate in a different column and the temperature curves (responses). .. GENERATED FROM PYTHON SOURCE LINES 53-59 .. code-block:: Python X_df = pd.DataFrame(X) y_basis = FourierBasis(n_basis=65) y_fd = fd.coordinates[0].to_basis(y_basis) .. GENERATED FROM PYTHON SOURCE LINES 60-62 An intercept term is incorporated. All functional coefficients will have the same basis as the response. .. GENERATED FROM PYTHON SOURCE LINES 62-66 .. code-block:: Python funct_reg = LinearRegression(fit_intercept=True) funct_reg.fit(X_df, y_fd) .. raw:: html
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 67-69 The regression coefficients are shown below. The first one is the intercept coefficient, corresponding to Canadian mean temperature. .. GENERATED FROM PYTHON SOURCE LINES 69-75 .. code-block:: Python funct_reg.intercept_.plot() funct_reg.coef_[0].plot() funct_reg.coef_[1].plot() funct_reg.coef_[2].plot() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_functional_regression_001.png :alt: plot functional regression :srcset: /auto_examples/images/sphx_glr_plot_functional_regression_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_functional_regression_002.png :alt: plot functional regression :srcset: /auto_examples/images/sphx_glr_plot_functional_regression_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_functional_regression_003.png :alt: plot functional regression :srcset: /auto_examples/images/sphx_glr_plot_functional_regression_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_functional_regression_004.png :alt: plot functional regression :srcset: /auto_examples/images/sphx_glr_plot_functional_regression_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 76-77 Finally, it is shown a panel with the prediction for all climate zones. .. GENERATED FROM PYTHON SOURCE LINES 77-90 .. code-block:: Python predictions = [] predictions.append(funct_reg.predict([[0, 1, 0, 0]])[0]) predictions.append(funct_reg.predict([[0, 0, 1, 0]])[0]) predictions.append(funct_reg.predict([[0, 0, 0, 1]])[0]) predictions_conc = FDataBasis.concatenate(*predictions) predictions_conc.argument_names = ('day',) predictions_conc.coordinate_names = ('temperature (ºC)',) predictions_conc.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_functional_regression_005.png :alt: plot functional regression :srcset: /auto_examples/images/sphx_glr_plot_functional_regression_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.894 seconds) .. _sphx_glr_download_auto_examples_plot_functional_regression.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_functional_regression.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_functional_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_functional_regression.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_