.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/ml/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_ml_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-13 .. code-block:: Python # Author: Rafael Hidalgo Alejo # License: MIT .. GENERATED FROM PYTHON SOURCE LINES 14-20 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 20-28 .. code-block:: Python from skfda.datasets import fetch_weather X_weather, y_weather = fetch_weather( return_X_y=True, as_frame=True, ) fd = X_weather.iloc[:, 0].array .. GENERATED FROM PYTHON SOURCE LINES 34-41 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 41-51 .. code-block:: Python import numpy as np from sklearn.preprocessing import OneHotEncoder # 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 52-54 Then, we construct a dataframe with each covariate in a different column and the temperature curves (responses). .. GENERATED FROM PYTHON SOURCE LINES 54-63 .. code-block:: Python import pandas as pd from skfda.representation.basis import FourierBasis 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 64-66 An intercept term is incorporated. All functional coefficients will have the same basis as the response. .. GENERATED FROM PYTHON SOURCE LINES 66-72 .. code-block:: Python from skfda.ml.regression import LinearRegression 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 79-81 The regression coefficients are shown below. The first one is the intercept coefficient, corresponding to Canadian mean temperature. .. GENERATED FROM PYTHON SOURCE LINES 81-91 .. code-block:: Python import matplotlib.pyplot as plt funct_reg.intercept_.plot() funct_reg.coef_[0].plot() funct_reg.coef_[1].plot() funct_reg.coef_[2].plot() plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/ml/images/sphx_glr_plot_functional_regression_001.png :alt: plot functional regression :srcset: /auto_examples/ml/images/sphx_glr_plot_functional_regression_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/ml/images/sphx_glr_plot_functional_regression_002.png :alt: plot functional regression :srcset: /auto_examples/ml/images/sphx_glr_plot_functional_regression_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/ml/images/sphx_glr_plot_functional_regression_003.png :alt: plot functional regression :srcset: /auto_examples/ml/images/sphx_glr_plot_functional_regression_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/ml/images/sphx_glr_plot_functional_regression_004.png :alt: plot functional regression :srcset: /auto_examples/ml/images/sphx_glr_plot_functional_regression_004.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 92-93 Finally, it is shown a panel with the prediction for all climate zones. .. GENERATED FROM PYTHON SOURCE LINES 93-104 .. code-block:: Python X_test = pd.DataFrame( [ [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], ], ) predictions = funct_reg.predict(X_test) predictions.plot() plt.show() .. image-sg:: /auto_examples/ml/images/sphx_glr_plot_functional_regression_005.png :alt: plot functional regression :srcset: /auto_examples/ml/images/sphx_glr_plot_functional_regression_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.990 seconds) .. _sphx_glr_download_auto_examples_ml_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/ml/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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_functional_regression.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_