.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/general_examples/plot_01_hdmr_2d.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_general_examples_plot_01_hdmr_2d.py: HDMR on a 2D Tensor =================== Run HDMR on a small 2D tensor, reconstruct the approximation, and compare the reconstructed tensor with the original input. .. GENERATED FROM PYTHON SOURCE LINES 8-44 .. image-sg:: /auto_examples/general_examples/images/sphx_glr_plot_01_hdmr_2d_001.png :alt: Original tensor, HDMR reconstruction :srcset: /auto_examples/general_examples/images/sphx_glr_plot_01_hdmr_2d_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Input shape: (32, 32) Reconstructed shape: (32, 32) Available component keys: ['g_0', 'g_1', 'g_2', 'g_1,2'] | .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from hdmrlib import HDMR x = np.linspace(0.0, 1.0, 32) y = np.linspace(0.0, 1.0, 32) X = ( 0.5 + np.sin(np.pi * x)[:, None] + np.cos(np.pi * y)[None, :] + 0.25 * np.outer(x, y) ) hdmr = HDMR(X, order=2) X_reconstructed = hdmr.reconstruct() components = hdmr.components() print("Input shape:", X.shape) print("Reconstructed shape:", X_reconstructed.shape) print("Available component keys:", list(components.keys())) fig, axes = plt.subplots(1, 2, figsize=(9, 4)) im0 = axes[0].imshow(X, aspect="auto") axes[0].set_title("Original tensor") plt.colorbar(im0, ax=axes[0], fraction=0.046, pad=0.04) im1 = axes[1].imshow(X_reconstructed, aspect="auto") axes[1].set_title("HDMR reconstruction") plt.colorbar(im1, ax=axes[1], fraction=0.046, pad=0.04) plt.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.239 seconds) .. _sphx_glr_download_auto_examples_general_examples_plot_01_hdmr_2d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_hdmr_2d.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_hdmr_2d.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_hdmr_2d.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_