.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/ml/plot_neighbors_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_neighbors_functional_regression.py: Neighbors Functional Regression =============================== Shows the usage of the nearest neighbors regressor with functional response. .. GENERATED FROM PYTHON SOURCE LINES 7-13 .. code-block:: Python # Author: Pablo Marcos Manchón # License: MIT # sphinx_gallery_thumbnail_number = 4 .. GENERATED FROM PYTHON SOURCE LINES 14-26 In this example we are going to show the usage of the nearest neighbors regressors with functional response. There is available a K-nn version, :class:`~skfda.ml.regression.KNeighborsRegressor`, and other one based in the radius, :class:`~skfda.ml.regression.RadiusNeighborsRegressor`. As in the :ref:`scalar response example `, we will fetch the Canadian weather dataset, which contains the daily temperature and precipitation at 35 different locations in Canada averaged over 1960 to 1994. The following figure shows the different temperature and precipitation curves. .. GENERATED FROM PYTHON SOURCE LINES 27-36 .. code-block:: Python from skfda.datasets import fetch_weather data = fetch_weather() fd = data["data"] # Split dataset, temperatures and curves of precipitation X, y_grid = fd.coordinates .. GENERATED FROM PYTHON SOURCE LINES 42-43 Temperatures .. GENERATED FROM PYTHON SOURCE LINES 43-49 .. code-block:: Python import matplotlib.pyplot as plt X.plot() plt.show() .. image-sg:: /auto_examples/ml/images/sphx_glr_plot_neighbors_functional_regression_001.png :alt: Canadian Weather :srcset: /auto_examples/ml/images/sphx_glr_plot_neighbors_functional_regression_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 50-51 Precipitation .. GENERATED FROM PYTHON SOURCE LINES 51-55 .. code-block:: Python y_grid.plot() plt.show() .. image-sg:: /auto_examples/ml/images/sphx_glr_plot_neighbors_functional_regression_002.png :alt: Canadian Weather :srcset: /auto_examples/ml/images/sphx_glr_plot_neighbors_functional_regression_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 56-59 We will try to predict the precipitation curves. First of all we are going to make a smoothing of the precipitation curves using a basis representation, employing for it a fourier basis with 5 elements. .. GENERATED FROM PYTHON SOURCE LINES 60-68 .. code-block:: Python from skfda.representation.basis import FourierBasis y = y_grid.to_basis(FourierBasis(n_basis=5)) y.plot() plt.show() .. image-sg:: /auto_examples/ml/images/sphx_glr_plot_neighbors_functional_regression_003.png :alt: Canadian Weather :srcset: /auto_examples/ml/images/sphx_glr_plot_neighbors_functional_regression_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 69-72 We will split the dataset in two partitions, for training and test, using the sklearn function :func:`~sklearn.model_selection.train_test_split`. .. GENERATED FROM PYTHON SOURCE LINES 73-83 .. code-block:: Python from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.1, random_state=28, ) .. GENERATED FROM PYTHON SOURCE LINES 84-88 We will try make a prediction using 5 neighbors and the :math:`\mathbb{L}^2` distance. In this case, to calculate the response we will use a mean of the response, weighted by their distance to the test sample. .. GENERATED FROM PYTHON SOURCE LINES 89-95 .. code-block:: Python from skfda.ml.regression import KNeighborsRegressor knn = KNeighborsRegressor(n_neighbors=5, weights="distance") knn.fit(X_train, y_train) .. raw:: html
KNeighborsRegressor(weights='distance')
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 102-106 We can predict values for the test partition using :meth:`~skfda.ml.regression.KNeighborsRegressor.predict`. The following figure shows the real precipitation curves, in dashed line, and the predicted ones. .. GENERATED FROM PYTHON SOURCE LINES 107-116 .. code-block:: Python y_pred = knn.predict(X_test) # Plot prediction fig = y_pred.plot() fig.axes[0].set_prop_cycle(None) # Reset colors y_test.plot(fig=fig, linestyle="--") plt.show() .. image-sg:: /auto_examples/ml/images/sphx_glr_plot_neighbors_functional_regression_004.png :alt: Canadian Weather :srcset: /auto_examples/ml/images/sphx_glr_plot_neighbors_functional_regression_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 117-128 We can quantify how much variability it is explained by the model using the :meth:`~skfda.ml.regression.KNeighborsRegressor.score` method, which computes the value .. math:: 1 - \frac{\sum_{i=1}^{n}\int (y_i(t) - \hat{y}_i(t))^2dt} {\sum_{i=1}^{n} \int (y_i(t)- \frac{1}{n}\sum_{i=1}^{n}y_i(t))^2dt} where :math:`y_i` are the real responses and :math:`\hat{y}_i` the predicted ones. .. GENERATED FROM PYTHON SOURCE LINES 129-133 .. code-block:: Python score = knn.score(X_test, y_test) print(score) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.9149923776656348 .. GENERATED FROM PYTHON SOURCE LINES 134-142 More detailed information about the canadian weather dataset can be obtained in the following references. * Ramsay, James O., and Silverman, Bernard W. (2006). Functional Data Analysis, 2nd ed. , Springer, New York. * Ramsay, James O., and Silverman, Bernard W. (2002). Applied Functional Data Analysis, Springer, New York\n' .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.343 seconds) .. _sphx_glr_download_auto_examples_ml_plot_neighbors_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_neighbors_functional_regression.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_neighbors_functional_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_neighbors_functional_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_neighbors_functional_regression.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_