.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_gpols/plot_15_cooperative.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_03_gpols_plot_15_cooperative.py: Cooperative GP with OLS credit assignment (new in eqdisco) ========================================================== In :class:`~eqdisco.CooperativeOLSRegressor` the population is a shared library of basis functions. Each generation, forward orthogonal least squares selects the subset that best explains the data; selected individuals earn their ERR, the others the share of the residual they could explain, so the population keeps searching for what the model still misses. The selection path gives nested models of every size, a Pareto front at no extra cost. The target mixes a periodic, a multiplicative and a rational term. .. GENERATED FROM PYTHON SOURCE LINES 13-23 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq rng = np.random.default_rng(4) X = rng.uniform(-2, 2, (300, 3)) y = 2 * np.sin(X[:, 0]) + X[:, 1] * X[:, 2] + 1 / (1 + X[:, 2] ** 2) + 0.01 * rng.normal(size=300) .. GENERATED FROM PYTHON SOURCE LINES 24-26 A cooperative run ----------------- .. GENERATED FROM PYTHON SOURCE LINES 26-40 .. code-block:: Python coop = eq.CooperativeOLSRegressor(population_size=150, generations=30, max_terms=6, random_state=0).fit(X, y) print(coop.equation_) print(coop.history_.tail(3)) fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) h = coop.history_ axs[0].semilogy(h["generation"], 1 - h["r2"], color="#285780", lw=2, label="$1-R^2$ of the current model") ax2 = axs[0].twinx() ax2.step(h["generation"], h["n_terms"], color="#D55E00", where="post") ax2.set_ylabel("terms in the model", color="#D55E00") axs[0].set_xlabel("generation") axs[0].legend(frameon=False, fontsize=8) eq.viz.plot_pareto(coop, ax=axs[1], annotate=True, max_labels=4) fig.tight_layout() .. image-sg:: /auto_examples/03_gpols/images/sphx_glr_plot_15_cooperative_001.png :alt: plot 15 cooperative :srcset: /auto_examples/03_gpols/images/sphx_glr_plot_15_cooperative_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none -2.695 + 2 * sin(x0) + 1 * x1 * x2 + 4.35 * sin(cos(sin(sqrt(x2)))) + 0.5849 * sin(sqrt(x2)) + 0.03112 * sin(cos(log(x2))) - 0.02022 * cos(x2^2) generation r2 bic n_terms best_bic library 28 28 0.999712 -1122.677185 6 -1836.698882 75 29 29 0.999712 -1122.677185 6 -1836.698882 88 30 30 0.999712 -1122.677185 6 -1836.698882 85 .. GENERATED FROM PYTHON SOURCE LINES 41-43 Parameter study: library size ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 43-52 .. code-block:: Python sizes = [40, 80, 150, 300] med = [np.median([1 - eq.CooperativeOLSRegressor(population_size=s, generations=20, random_state=k).fit(X, y).score(X, y) for k in range(3)]) for s in sizes] print("population", sizes, "median 1-R2", [f"{v:.1e}" for v in med]) fig, ax = plt.subplots(figsize=(4.5, 3)) ax.plot(sizes, med, "o-", color="#285780") ax.set_yscale("log") ax.set_xlabel("population (library) size") ax.set_ylabel("median $1-R^2$") plt.show() .. image-sg:: /auto_examples/03_gpols/images/sphx_glr_plot_15_cooperative_002.png :alt: plot 15 cooperative :srcset: /auto_examples/03_gpols/images/sphx_glr_plot_15_cooperative_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none population [40, 80, 150, 300] median 1-R2 ['2.6e-05', '2.6e-05', '2.5e-05', '2.5e-05'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.461 seconds) .. _sphx_glr_download_auto_examples_03_gpols_plot_15_cooperative.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/03_gpols/plot_15_cooperative.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_15_cooperative.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_15_cooperative.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_15_cooperative.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_