.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/general_examples/plot_03_empr_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_03_empr_hdmr_2d.py: EMPR vs HDMR on a 2D Tensor =========================== Run EMPR and HDMR on the same 2D tensor, compare the reconstructions, and visualize the remaining approximation errors. .. GENERATED FROM PYTHON SOURCE LINES 8-81 .. image-sg:: /auto_examples/general_examples/images/sphx_glr_plot_03_empr_hdmr_2d_001.png :alt: Original tensor, EMPR reconstruction, HDMR reconstruction, EMPR absolute error, HDMR absolute error, Absolute difference :srcset: /auto_examples/general_examples/images/sphx_glr_plot_03_empr_hdmr_2d_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Input shape: (32, 32) EMPR mean absolute error: 4.1843427594362437e-19 HDMR mean absolute error: 2.879912020664621e-20 Mean absolute difference between reconstructions: 4.0657581468206416e-19 | .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from hdmrlib import EMPR, 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] + 0.8 * np.cos(np.pi * y)[None, :] + 0.5 * np.outer(x, y) + 0.3 * np.exp(-40.0 * ((x[:, None] - 0.7) ** 2 + (y[None, :] - 0.3) ** 2)) ) empr = EMPR(X, order=2) X_empr = np.asarray(empr.reconstruct(), dtype=np.float64) hdmr = HDMR(X, order=2) X_hdmr = np.asarray(hdmr.reconstruct(), dtype=np.float64) empr_abs_err = np.abs(X - X_empr) hdmr_abs_err = np.abs(X - X_hdmr) recon_diff = np.abs(X_empr - X_hdmr) print("Input shape:", X.shape) print("EMPR mean absolute error:", float(np.mean(empr_abs_err))) print("HDMR mean absolute error:", float(np.mean(hdmr_abs_err))) print("Mean absolute difference between reconstructions:", float(np.mean(recon_diff))) vmin = min(X.min(), X_empr.min(), X_hdmr.min()) vmax = max(X.max(), X_empr.max(), X_hdmr.max()) err_vmax = max(empr_abs_err.max(), hdmr_abs_err.max(), recon_diff.max()) fig = plt.figure(figsize=(12, 7), constrained_layout=True) gs = fig.add_gridspec(2, 4, width_ratios=[1, 1, 1, 0.05]) ax00 = fig.add_subplot(gs[0, 0]) ax01 = fig.add_subplot(gs[0, 1]) ax02 = fig.add_subplot(gs[0, 2]) cax0 = fig.add_subplot(gs[0, 3]) ax10 = fig.add_subplot(gs[1, 0]) ax11 = fig.add_subplot(gs[1, 1]) ax12 = fig.add_subplot(gs[1, 2]) cax1 = fig.add_subplot(gs[1, 3]) im0 = ax00.imshow(X, aspect="auto", vmin=vmin, vmax=vmax, interpolation="nearest") ax00.set_title("Original tensor") ax01.imshow(X_empr, aspect="auto", vmin=vmin, vmax=vmax, interpolation="nearest") ax01.set_title("EMPR reconstruction") ax02.imshow(X_hdmr, aspect="auto", vmin=vmin, vmax=vmax, interpolation="nearest") ax02.set_title("HDMR reconstruction") fig.colorbar(im0, cax=cax0) im1 = ax10.imshow(empr_abs_err, aspect="auto", vmin=0.0, vmax=err_vmax, interpolation="nearest") ax10.set_title("EMPR absolute error") ax11.imshow(hdmr_abs_err, aspect="auto", vmin=0.0, vmax=err_vmax, interpolation="nearest") ax11.set_title("HDMR absolute error") ax12.imshow(recon_diff, aspect="auto", vmin=0.0, vmax=err_vmax, interpolation="nearest") ax12.set_title("Absolute difference") fig.colorbar(im1, cax=cax1) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.618 seconds) .. _sphx_glr_download_auto_examples_general_examples_plot_03_empr_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_03_empr_hdmr_2d.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_empr_hdmr_2d.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_empr_hdmr_2d.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_