.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/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_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-19 .. code-block:: Python # Author: Pablo Marcos Manchón # License: MIT # sphinx_gallery_thumbnail_number = 4 from sklearn.model_selection import train_test_split import skfda from skfda.ml.regression import KNeighborsRegressor from skfda.representation.basis import FourierBasis .. GENERATED FROM PYTHON SOURCE LINES 20-32 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 33-41 .. code-block:: Python data = skfda.datasets.fetch_weather() fd = data['data'] # Split dataset, temperatures and curves of precipitation X, y = fd.coordinates .. GENERATED FROM PYTHON SOURCE LINES 42-43 Temperatures .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: Python X.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_neighbors_functional_regression_001.png :alt: Canadian Weather :srcset: /auto_examples/images/sphx_glr_plot_neighbors_functional_regression_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 47-48 Precipitation .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: Python y.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_neighbors_functional_regression_002.png :alt: Canadian Weather :srcset: /auto_examples/images/sphx_glr_plot_neighbors_functional_regression_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 52-55 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 56-61 .. code-block:: Python y = y.to_basis(FourierBasis(n_basis=5)) y.plot() .. image-sg:: /auto_examples/images/sphx_glr_plot_neighbors_functional_regression_003.png :alt: Canadian Weather :srcset: /auto_examples/images/sphx_glr_plot_neighbors_functional_regression_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 62-65 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 66-74 .. code-block:: Python 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 75-79 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 80-84 .. code-block:: Python 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 85-89 We can predict values for the test partition using :meth:`~skfda.ml.regression.KNeighborsFunctionalRegressor.predict`. The following figure shows the real precipitation curves, in dashed line, and the predicted ones. .. GENERATED FROM PYTHON SOURCE LINES 90-99 .. 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='--') .. image-sg:: /auto_examples/images/sphx_glr_plot_neighbors_functional_regression_004.png :alt: Canadian Weather :srcset: /auto_examples/images/sphx_glr_plot_neighbors_functional_regression_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 100-111 We can quantify how much variability it is explained by the model using the :meth:`~skfda.ml.regression.KNeighborsFunctionalRegressor.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 112-116 .. 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 117-125 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.804 seconds) .. _sphx_glr_download_auto_examples_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/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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_