.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_sparse/plot_18_exhaustive.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_18_exhaustive.py: Exhaustive search with a description length =========================================== For small formulas the whole space can be searched. :class:`~eqdisco.ExhaustiveRegressor` enumerates every canonical expression up to a given size, fits its parameters, removes functions that compute the same values, and ranks the rest by a description length -- the cost of the data given the model plus the cost of the model -- following the minimum description length approach of exhaustive symbolic regression (Bartlett, Desmond and Ferreira, 2024). The data are the energy of a spring, :math:`E=\tfrac12 kx^2`, with 1 % noise. .. GENERATED FROM PYTHON SOURCE LINES 13-21 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq prob = eq.datasets.get_problem("feynman-I.14.4", noise=0.01, n_train=100) .. GENERATED FROM PYTHON SOURCE LINES 22-24 Enumeration and ranking ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 24-43 .. code-block:: Python ex = eq.ExhaustiveRegressor(functions=("add", "sub", "mul", "div", "square"), max_size=6, max_params=2, feature_names=prob.feature_names) ex.fit(prob.X_train, prob.y_train) print(ex.n_candidates_, "expressions,", len(ex.results_), "distinct functions") print(ex.results_.head(6)[["equation", "size", "n_params", "mse", "bic", "mdl"]]) fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) r = ex.results_ axs[0].scatter(r["size"] + np.random.default_rng(0).uniform(-0.2, 0.2, len(r)), r["mdl"], s=8, color="#6B7785", alpha=0.5) best = r.iloc[0] axs[0].scatter([best["size"]], [best["mdl"]], s=80, facecolor="none", edgecolor="#D55E00", lw=2, label=f"best: {best['equation']}") axs[0].set_xlabel("size (nodes)") axs[0].set_ylabel("description length (nats)") axs[0].legend(frameon=False, fontsize=8) top = r.head(10).iloc[::-1] axs[1].barh(top["equation"], top["mdl"] - r["mdl"].min() + 1, color="#285780") axs[1].set_xscale("log") axs[1].set_xlabel("description length above the best (+1)") axs[1].tick_params(axis="y", labelsize=7) fig.tight_layout() .. image-sg:: /auto_examples/04_sparse/images/sphx_glr_plot_18_exhaustive_001.png :alt: plot 18 exhaustive :srcset: /auto_examples/04_sparse/images/sphx_glr_plot_18_exhaustive_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2461 expressions, 914 distinct functions equation size n_params mse bic mdl 0 0.5005 * x^2 * k_spring 6 1 0.023324 -82.829798 -31.240835 1 (-1.219 + x)^2 * k_spring 6 1 14.655978 561.482875 290.915502 2 (-2.414 + k_spring + x)^2 6 1 18.439259 584.446249 302.397189 3 (k_spring + k_spring) * x 5 0 30.254857 629.358551 322.773898 4 1.963 * k_spring * x 5 1 30.088725 633.413099 324.801172 5 (0.3498 * k_spring + x)^2 6 1 29.391810 631.069652 325.708890 .. GENERATED FROM PYTHON SOURCE LINES 44-46 Parameter study: size limit and cost ------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 46-61 .. code-block:: Python import time # noqa: E402 sizes, counts, times = [3, 4, 5, 6], [], [] for s in sizes: t0 = time.perf_counter() m = eq.ExhaustiveRegressor(functions=("add", "sub", "mul", "div", "square"), max_size=s, max_params=2).fit(prob.X_train, prob.y_train) counts.append(m.n_candidates_) times.append(time.perf_counter() - t0) print("max size", sizes, "candidates", counts, "seconds", np.round(times, 2).tolist()) fig, ax = plt.subplots(figsize=(4.5, 3)) ax.semilogy(sizes, counts, "o-", color="#285780", label="candidates") ax.semilogy(sizes, times, "s--", color="#D55E00", label="seconds") ax.set_xlabel("maximum size") ax.legend(frameon=False, fontsize=8) plt.show() .. image-sg:: /auto_examples/04_sparse/images/sphx_glr_plot_18_exhaustive_002.png :alt: plot 18 exhaustive :srcset: /auto_examples/04_sparse/images/sphx_glr_plot_18_exhaustive_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none max size [3, 4, 5, 6] candidates [29, 89, 591, 2461] seconds [0.01, 0.03, 0.44, 1.99] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.736 seconds) .. _sphx_glr_download_auto_examples_04_sparse_plot_18_exhaustive.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_18_exhaustive.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_18_exhaustive.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_18_exhaustive.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_18_exhaustive.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_