.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "content/user_guide/auto_examples/1single/plot_eeof.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_content_user_guide_auto_examples_1single_plot_eeof.py: Extented EOF analysis ===================== This example demonstrates Extended EOF (EEOF) analysis on ``xarray`` tutorial data. EEOF analysis, also termed as Multivariate/Multichannel Singular Spectrum Analysis, advances traditional EOF analysis to capture propagating signals or oscillations in multivariate datasets. At its core, this involves the formulation of a lagged covariance matrix that encapsulates both spatial and temporal correlations. Subsequently, this matrix is decomposed to yield its eigenvectors (components) and eigenvalues (explained variance). Let's begin by setting up the required packages and fetching the data: .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: default import matplotlib.pyplot as plt import xarray as xr import xeofs as xe xr.set_options(display_expand_data=False) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 24-25 Load the tutorial data. .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: default t2m = xr.tutorial.load_dataset("air_temperature").air .. GENERATED FROM PYTHON SOURCE LINES 29-42 Prior to conducting the EEOF analysis, it's essential to determine the structure of the lagged covariance matrix. This entails defining the time delay ``tau`` and the ``embedding`` dimension. The former signifies the interval between the original and lagged time series, while the latter dictates the number of time-lagged copies in the delay-coordinate space, representing the system's dynamics. For illustration, using ``tau=4`` and ``embedding=40``, we generate 40 delayed versions of the time series, each offset by 4 time steps, resulting in a maximum shift of ``tau x embedding = 160``. Given our dataset's 6-hour intervals, tau = 4 translates to a 24-hour shift. It's obvious that this way of constructing the lagged covariance matrix and subsequently decomposing it can be computationally expensive. For example, given our dataset's dimensions, .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: default t2m.shape .. rst-class:: sphx-glr-script-out .. code-block:: none (2920, 25, 53) .. GENERATED FROM PYTHON SOURCE LINES 46-54 the extended dataset would have 40 x 25 x 53 = 53000 features which is much larger than the original dataset's 1325 features. To mitigate this, we can first preprocess the data using PCA / EOF analysis and then perform EEOF analysis on the resulting PCA / EOF scores. Here, we'll use ``n_pca_modes=50`` to retain the first 50 PCA modes, so we end up with 40 x 50 = 200 (latent) features. With these parameters set, we proceed to instantiate the ``ExtendedEOF`` model and fit our data. .. GENERATED FROM PYTHON SOURCE LINES 54-63 .. code-block:: default model = xe.single.ExtendedEOF( n_modes=10, tau=4, embedding=40, n_pca_modes=50, use_coslat=True ) model.fit(t2m, dim="time") scores = model.scores() components = model.components() components .. raw:: html
<xarray.DataArray 'components' (mode: 10, embedding: 40, lat: 25, lon: 53)>
    0.0003856 0.0003647 0.0003572 0.0003564 ... -0.001401 -0.0009889 -0.0005825
    Coordinates:
      * lat        (lat) float32 15.0 17.5 20.0 22.5 25.0 ... 67.5 70.0 72.5 75.0
      * lon        (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
      * embedding  (embedding) int64 0 4 8 12 16 20 24 ... 136 140 144 148 152 156
      * mode       (mode) int64 1 2 3 4 5 6 7 8 9 10
    Attributes: (12/15)
        model:          Extended EOF Analysis
        software:       xeofs
        version:        1.2.0
        date:           2024-09-04 15:05:13
        n_modes:        10
        center:         True
        ...             ...
        sample_name:    sample
        feature_name:   feature
        random_state:   None
        compute:        True
        solver:         auto
        solver_kwargs:  {}


.. GENERATED FROM PYTHON SOURCE LINES 64-68 A notable distinction from standard EOF analysis is the incorporation of an extra ``embedding`` dimension in the components. Nonetheless, the overarching methodology mirrors traditional EOF practices. The results, for instance, can be assessed by examining the explained variance ratio. .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. code-block:: default model.explained_variance_ratio().plot() plt.show() .. image-sg:: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_eeof_001.png :alt: plot eeof :srcset: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_eeof_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-74 Additionally, we can look into the scores; let's spotlight mode 4. .. GENERATED FROM PYTHON SOURCE LINES 74-78 .. code-block:: default scores.sel(mode=4).plot() plt.show() .. image-sg:: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_eeof_002.png :alt: mode = 4 :srcset: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_eeof_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-82 In wrapping up, we visualize the corresponding EEOF component of mode 4. For visualization purposes, we'll focus on the component at a specific latitude, in this instance, 60 degrees north. .. GENERATED FROM PYTHON SOURCE LINES 82-85 .. code-block:: default components.sel(mode=4, lat=60).plot() plt.show() .. image-sg:: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_eeof_003.png :alt: lat = 60.0, mode = 4 :srcset: /content/user_guide/auto_examples/1single/images/sphx_glr_plot_eeof_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.568 seconds) .. _sphx_glr_download_content_user_guide_auto_examples_1single_plot_eeof.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_eeof.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_eeof.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_