.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/data_analysis/plot_canadian_weather.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_data_analysis_plot_canadian_weather.py: Canadian weather dataset ========================= .. GENERATED FROM PYTHON SOURCE LINES 6-20 .. code-block:: Python # Author: Steven Golovkine # License: MIT # Load packages import matplotlib.pyplot as plt import numpy as np from FDApy.representation import DenseArgvals from FDApy.preprocessing import UFPCA from FDApy import read_csv from FDApy.visualization import plot .. GENERATED FROM PYTHON SOURCE LINES 21-22 In this section, we will use the Canadian weather dataset to illustrate the use of the package. We will first load the data and plot it. Then, we will smooth the data using local polynomial regression. Finally, we will perform UFPCA on the smoothed data and plot the eigenfunctions. .. GENERATED FROM PYTHON SOURCE LINES 24-25 First, we load the data. The dataset contains daily temperature data for :math:`35` Canadian cities. The dataset can be downloaded from the `here `_. This is an example of a :class:`~FDApy.representation.DenseFunctionalData` object. It also shows how the CSV file should be formatted and can be read using the :func:`~FDApy.read_csv` function. .. GENERATED FROM PYTHON SOURCE LINES 25-33 .. code-block:: Python # Load data temp_data = read_csv("../data/canadian_temperature_daily.csv", index_col=0) _ = plot(temp_data) plt.show() .. image-sg:: /auto_examples/data_analysis/images/sphx_glr_plot_canadian_weather_001.png :alt: plot canadian weather :srcset: /auto_examples/data_analysis/images/sphx_glr_plot_canadian_weather_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 34-35 We will now smooth the data using local polynomial regression on the grid :math:`\{1, 2, 3, \dots, 365\}`. We will use the Epanechnikov kernel with a bandwidth of :math:`30` and a degree of :math:`1`. The smoothing is performed using the :func:`~FDApy.representation.DenseFunctionalData.smooth` method. We will then plot the smoothed data. .. GENERATED FROM PYTHON SOURCE LINES 35-57 .. code-block:: Python points = DenseArgvals({"input_dim_0": np.linspace(1, 365, 365)}) kernel_name = "epanechnikov" bandwidth = 30 degree = 1 temp_smooth = temp_data.smooth( points=points, method="LP", kernel_name=kernel_name, bandwidth=bandwidth, degree=degree, ) fig, axes = plt.subplots(2, 2, figsize=(10, 8)) for idx, ax in enumerate(axes.flat): plot(temp_data[idx], colors="k", alpha=0.2, ax=ax) plot(temp_smooth[idx], colors="r", ax=ax) ax.set_title(f"Observation {idx + 1}") plt.show() .. image-sg:: /auto_examples/data_analysis/images/sphx_glr_plot_canadian_weather_002.png :alt: Observation 1, Observation 2, Observation 3, Observation 4 :srcset: /auto_examples/data_analysis/images/sphx_glr_plot_canadian_weather_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 58-59 We will now perform UFPCA on the smoothed data. We will use the inner product method and keep the principal components that explain 99% of the variance. We will then plot the eigenfunctions. The scores are then computed using the inner-product matrix. .. GENERATED FROM PYTHON SOURCE LINES 59-68 .. code-block:: Python ufpca = UFPCA(n_components=0.99, method="inner-product") ufpca.fit(temp_smooth) scores = ufpca.transform(method="InnPro") _ = plot(ufpca.eigenfunctions) plt.show() .. image-sg:: /auto_examples/data_analysis/images/sphx_glr_plot_canadian_weather_003.png :alt: plot canadian weather :srcset: /auto_examples/data_analysis/images/sphx_glr_plot_canadian_weather_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 69-70 Finally, the data can be reconstructed using the scores. We plot the reconstruction of the first 10 observations. .. GENERATED FROM PYTHON SOURCE LINES 70-80 .. code-block:: Python data_recons = ufpca.inverse_transform(scores) fig, axes = plt.subplots(nrows=5, ncols=2, figsize=(16, 16)) for idx_plot, idx in enumerate(np.random.choice(temp_data.n_obs, 10)): temp_ax = axes.flatten()[idx_plot] temp_ax = plot(temp_data[idx], colors="k", alpha=0.2, ax=temp_ax, label="Data") plot(temp_smooth[idx], colors="r", ax=temp_ax, label="Smooth") plot(data_recons[idx], colors="b", ax=temp_ax, label="Reconstruction") temp_ax.legend() plt.show() .. image-sg:: /auto_examples/data_analysis/images/sphx_glr_plot_canadian_weather_004.png :alt: plot canadian weather :srcset: /auto_examples/data_analysis/images/sphx_glr_plot_canadian_weather_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.675 seconds) .. _sphx_glr_download_auto_examples_data_analysis_plot_canadian_weather.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_canadian_weather.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_canadian_weather.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_canadian_weather.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_