.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/decomposition_workflows/plot_lower_order_reconstruction.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_decomposition_workflows_plot_lower_order_reconstruction.py: Lower-Order Reconstruction ========================== Compare full and lower-order reconstructions and relate the approximation quality to the retained component terms. .. GENERATED FROM PYTHON SOURCE LINES 8-78 .. image-sg:: /auto_examples/decomposition_workflows/images/sphx_glr_plot_lower_order_reconstruction_001.png :alt: Reconstruction error, Component magnitudes, Order-1 absolute error :srcset: /auto_examples/decomposition_workflows/images/sphx_glr_plot_lower_order_reconstruction_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Available component keys: ['g_0', 'g_1', 'g_2', 'g_1,2'] Full reconstruction MAE: 8.250100906256885e-19 Order-1 reconstruction MAE: 0.6063038502324416 | .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from hdmrlib import EMPR 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.35 * np.outer(x, y) ) empr = EMPR(X, order=2) components = empr.components() X_full = np.asarray(empr.reconstruct(order=2), dtype=np.float64) X_order1 = np.asarray(empr.reconstruct(order=1), dtype=np.float64) full_mae = float(np.mean(np.abs(X - X_full))) order1_mae = float(np.mean(np.abs(X - X_order1))) keys = list(components.keys()) norms = [float(np.linalg.norm(np.asarray(components[key]))) for key in keys] pairs = sorted(zip(keys, norms), key=lambda t: t[1], reverse=True) sorted_keys = [str(k) for k, _ in pairs] sorted_norms = [v for _, v in pairs] print("Available component keys:", keys) print("Full reconstruction MAE:", full_mae) print("Order-1 reconstruction MAE:", order1_mae) fig, axes = plt.subplots(1, 3, figsize=(12, 4), constrained_layout=True) # Panel 1: reconstruction error comparison axes[0].bar( ["Order 2", "Order 1"], [full_mae, order1_mae], ) axes[0].set_title("Reconstruction error") axes[0].set_ylabel("Mean absolute error") # Panel 2: retained component magnitudes bars = axes[1].bar(sorted_keys, sorted_norms) axes[1].set_title("Component magnitudes") axes[1].set_xlabel("Component key") axes[1].set_ylabel("Frobenius norm") axes[1].tick_params(axis="x", rotation=30) for bar, value in zip(bars, sorted_norms): axes[1].text( bar.get_x() + bar.get_width() / 2, bar.get_height(), f"{value:.2f}", ha="center", va="bottom", fontsize=9, ) # Panel 3: spatial error for lower-order reconstruction order1_error = np.abs(X - X_order1) im = axes[2].imshow(order1_error, aspect="auto", interpolation="nearest") axes[2].set_title("Order-1 absolute error") fig.colorbar(im, ax=axes[2], fraction=0.046, pad=0.04) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.292 seconds) .. _sphx_glr_download_auto_examples_decomposition_workflows_plot_lower_order_reconstruction.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_lower_order_reconstruction.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_lower_order_reconstruction.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_lower_order_reconstruction.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_