.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/smoothing/plot_smooth_data_1d.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_smoothing_plot_smooth_data_1d.py: Smoothing of dense one-dimensional functional data ================================================== .. GENERATED FROM PYTHON SOURCE LINES 6-18 .. code-block:: Python # Author: Steven Golovkine # License: MIT # Load packages import matplotlib.pyplot as plt import numpy as np from FDApy.simulation import KarhunenLoeve from FDApy.representation import DenseArgvals from FDApy.visualization import plot .. GENERATED FROM PYTHON SOURCE LINES 19-20 The package includes different smoothing methods to smooth functional data. In this section, we are showing the building blocks of the smoothing of dense unidimensional functional data. First, we simulate functional data using the Karhunen-Loève decomposition using B-splines basis functions. We then add some noise to the simulation. .. GENERATED FROM PYTHON SOURCE LINES 20-44 .. code-block:: Python # Set general parameters rng = 42 n_obs = 4 # Parameters of the basis name = "bsplines" n_functions = 5 points = DenseArgvals({"input_dim_0": np.linspace(0, 1, 101)}) # Simulate data kl = KarhunenLoeve( basis_name=name, n_functions=n_functions, argvals=points, random_state=rng ) kl.new(n_obs=n_obs) # Add some noise to the simulation. kl.add_noise(0.05) .. GENERATED FROM PYTHON SOURCE LINES 45-46 We will smooth the noisy data using the :func:`~FDApy.representation.DenseFunctionalData.smooth` function. This function allows to smooth the data using different methods such as local polynomials and P-splines. In this example, we will use the local polynomials smoothing method with an Epanechnikov kernel and a bandwidth of :math:`0.1`. In the plot, the red line represents the true data, the grey line represents the noisy data and the blue line represents the smoothed data. .. GENERATED FROM PYTHON SOURCE LINES 46-69 .. code-block:: Python # Smooth the data kernel_name = "epanechnikov" bandwidth = 0.1 degree = 1 fdata_smooth = kl.noisy_data.smooth( points=points, method="LP", kernel_name=kernel_name, bandwidth=bandwidth, degree=degree, ) # Plot results fig, axes = plt.subplots(2, 2, figsize=(10, 8)) for idx, ax in enumerate(axes.flat): plot(kl.noisy_data[idx], colors="k", alpha=0.2, ax=ax) plot(kl.data[idx], colors="r", ax=ax) plot(fdata_smooth[idx], colors="b", ax=ax) ax.set_title(f"Observation {idx + 1}") plt.show() .. image-sg:: /auto_examples/smoothing/images/sphx_glr_plot_smooth_data_1d_001.png :alt: Observation 1, Observation 2, Observation 3, Observation 4 :srcset: /auto_examples/smoothing/images/sphx_glr_plot_smooth_data_1d_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.278 seconds) .. _sphx_glr_download_auto_examples_smoothing_plot_smooth_data_1d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_smooth_data_1d.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_smooth_data_1d.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_smooth_data_1d.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_