.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/decomposition_workflows/plot_compare_orders.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_compare_orders.py: Compare Decomposition Orders ============================ Compare reconstruction quality across retained decomposition orders on the same tensor. .. GENERATED FROM PYTHON SOURCE LINES 8-52 .. image-sg:: /auto_examples/decomposition_workflows/images/sphx_glr_plot_compare_orders_001.png :alt: Reconstruction error by retained order, Absolute error for order-1 reconstruction :srcset: /auto_examples/decomposition_workflows/images/sphx_glr_plot_compare_orders_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Orders: [0, 1, 2] MAE by order: [2.2834725918732106, 0.6063038502324416, 8.250100906256885e-19] RMSE by order: [2.7352060287389985, 0.7193407049152725, 5.44292167650782e-18] | .. 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) orders = [0, 1, 2] reconstructions = [np.asarray(empr.reconstruct(order=o), dtype=np.float64) for o in orders] mae_values = [float(np.mean(np.abs(X - Xr))) for Xr in reconstructions] rmse_values = [float(np.sqrt(np.mean((X - Xr) ** 2))) for Xr in reconstructions] print("Orders:", orders) print("MAE by order:", mae_values) print("RMSE by order:", rmse_values) fig, axes = plt.subplots(1, 2, figsize=(10, 4), constrained_layout=True) axes[0].plot(orders, mae_values, marker="o", label="MAE") axes[0].plot(orders, rmse_values, marker="s", label="RMSE") axes[0].set_title("Reconstruction error by retained order") axes[0].set_xlabel("Retained order") axes[0].set_ylabel("Error") axes[0].set_xticks(orders) axes[0].grid(True, alpha=0.3) axes[0].legend() order1_error = np.abs(X - reconstructions[1]) im = axes[1].imshow(order1_error, aspect="auto", interpolation="nearest") axes[1].set_title("Absolute error for order-1 reconstruction") fig.colorbar(im, ax=axes[1], fraction=0.046, pad=0.04) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.264 seconds) .. _sphx_glr_download_auto_examples_decomposition_workflows_plot_compare_orders.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_compare_orders.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_compare_orders.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_compare_orders.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_