.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_sparse/plot_16_sparse_library.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 or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_04_sparse_plot_16_sparse_library.py: Sparse regression over a library of terms ========================================= If the unknown law is a sparse combination of candidate terms, regression can replace search. :class:`~eqdisco.SparseRegressor` builds a library -- here all monomials up to degree 3 in three variables, 19 terms -- and selects a few by sequentially thresholded least squares (STLSQ; Brunton, Proctor and Kutz, 2016), the cross-validated Lasso, forward orthogonal least squares (FROLS) or best-subset search. The target is a polynomial heat-release model with three terms. .. GENERATED FROM PYTHON SOURCE LINES 12-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq rng = np.random.default_rng(5) X = rng.uniform(-1, 1, (200, 3)) y = 1.5 * X[:, 0] ** 2 * X[:, 1] - 0.8 * X[:, 2] + 0.6 * X[:, 0] * X[:, 2] + 0.02 * rng.normal(size=200) .. GENERATED FROM PYTHON SOURCE LINES 23-25 Four solvers on the same library -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-40 .. code-block:: Python models = {m: eq.SparseRegressor(degree=3, method=m, threshold=0.05, max_terms=4, feature_names=["T", "p", "c"]).fit(X, y) for m in ("stlsq", "lasso", "frols", "best_subset")} for m, s in models.items(): print(f"{m:12s} {s.support_.sum():2d} terms R2 {s.score(X, y):.5f} {s.equation_}") terms = [t.to_string() for t in models["stlsq"].terms_] fig, ax = plt.subplots(figsize=(10, 3.3)) w = 0.2 for k, (m, s) in enumerate(models.items()): ax.bar(np.arange(len(terms)) + (k - 1.5) * w, s.coef_, width=w, color=eq.viz.OKABE_ITO[k], label=m) ax.axhline(0, color="#6B7785", lw=0.8) ax.set_xticks(range(len(terms))) ax.set_xticklabels(terms, rotation=60, ha="right", fontsize=7) ax.set_ylabel("coefficient") ax.legend(frameon=False, fontsize=8, ncol=4) fig.tight_layout() .. image-sg:: /auto_examples/04_sparse/images/sphx_glr_plot_16_sparse_library_001.png :alt: plot 16 sparse library :srcset: /auto_examples/04_sparse/images/sphx_glr_plot_16_sparse_library_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none stlsq 3 terms R2 0.99898 0.001385 - 0.7988 * c + 0.6024 * T * c + 1.501 * T^2 * p lasso 12 terms R2 0.99904 0.003245 - 0.001809 * T - 0.7913 * c - 0.006153 * T^2 + 0.005957 * T * p + 0.6013 * T * c - 0.004042 * T^3 + 1.502 * T^2 * p - 0.007765 * T^2 * c + 0.002921 * T * p^2 - 0.009571 * T * p * c - 0.01192 * p^2 * c - 0.00214 * c^3 frols 3 terms R2 0.99898 0.001385 - 0.7988 * c + 0.6024 * T * c + 1.501 * T^2 * p best_subset 3 terms R2 0.99898 0.001385 - 0.7988 * c + 0.6024 * T * c + 1.501 * T^2 * p .. GENERATED FROM PYTHON SOURCE LINES 41-43 Parameter study: the STLSQ threshold and noise ---------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 43-55 .. code-block:: Python ths = np.logspace(-3, 0, 12) fig, ax = plt.subplots(figsize=(4.8, 3.2)) for k, nz in enumerate((0.02, 0.1, 0.3)): yn = y + nz * rng.normal(size=len(y)) n_by_threshold = [int(eq.SparseRegressor(degree=3, threshold=t).fit(X, yn).support_.sum()) for t in ths] print(f"noise {nz}: terms kept at thresholds {np.round(ths[::3], 3).tolist()}: {n_by_threshold[::3]}") ax.semilogx(ths, n_by_threshold, "o-", color=eq.viz.OKABE_ITO[k], ms=3, label=f"noise {nz}") ax.axhline(3, color="#6B7785", ls="--", lw=1) ax.set_xlabel("STLSQ threshold (relative)") ax.set_ylabel("selected terms") ax.legend(frameon=False, fontsize=8) plt.show() .. image-sg:: /auto_examples/04_sparse/images/sphx_glr_plot_16_sparse_library_002.png :alt: plot 16 sparse library :srcset: /auto_examples/04_sparse/images/sphx_glr_plot_16_sparse_library_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none noise 0.02: terms kept at thresholds [0.001, 0.007, 0.043, 0.285]: [16, 5, 3, 3] noise 0.1: terms kept at thresholds [0.001, 0.007, 0.043, 0.285]: [19, 14, 3, 3] noise 0.3: terms kept at thresholds [0.001, 0.007, 0.043, 0.285]: [18, 16, 6, 3] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.500 seconds) .. _sphx_glr_download_auto_examples_04_sparse_plot_16_sparse_library.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/eqdisco/main?urlpath=lab/tree/notebooks/auto_examples/04_sparse/plot_16_sparse_library.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_16_sparse_library.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_16_sparse_library.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_16_sparse_library.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_