.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/decomposition_workflows/plot_inspect_components.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_inspect_components.py: Inspect Component Terms ======================= Inspect the available component keys and visualize the magnitude of each component term. .. GENERATED FROM PYTHON SOURCE LINES 8-56 .. image-sg:: /auto_examples/decomposition_workflows/images/sphx_glr_plot_inspect_components_001.png :alt: Component magnitudes :srcset: /auto_examples/decomposition_workflows/images/sphx_glr_plot_inspect_components_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'] | .. 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.25 * np.outer(x, y) ) empr = EMPR(X, order=2) components = empr.components() keys = list(components.keys()) norms = [float(np.linalg.norm(np.asarray(components[key]))) for key in keys] # Sort terms by magnitude for a clearer comparison. 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) fig, ax = plt.subplots(figsize=(7, 4), constrained_layout=True) bars = ax.bar(sorted_keys, sorted_norms) ax.set_title("Component magnitudes") ax.set_xlabel("Component key") ax.set_ylabel("Frobenius norm") ax.tick_params(axis="x", rotation=30) for bar, value in zip(bars, sorted_norms): ax.text( bar.get_x() + bar.get_width() / 2, bar.get_height(), f"{value:.2f}", ha="center", va="bottom", fontsize=9, ) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.100 seconds) .. _sphx_glr_download_auto_examples_decomposition_workflows_plot_inspect_components.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_inspect_components.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_inspect_components.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_inspect_components.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_