.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_gpols/plot_13_gpols.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_13_gpols.py: GP-OLS: pruning terms by their error reduction ratio ==================================================== GP-OLS (Madár, Abonyi and Szeifert, 2005) evolves linear-in-parameters models with the function set :math:`\{+,\times\}`, reads every individual as a sum of terms, and lets orthogonal least squares judge them: terms whose error reduction ratio is below a threshold are removed from the individual. The search is spared from carrying useless terms and returns compact, interpretable models. eqdisco adds a backward check of the unique contribution of every term, which removes collinear terms that received a high ERR only because of their position. .. 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(1) X = rng.uniform(-1, 1, (300, 4)) y = 2 * X[:, 0] * X[:, 1] - 1.5 * X[:, 2] ** 2 + 0.5 * X[:, 0] * X[:, 2] * X[:, 3] + 0.02 * rng.normal(size=300) .. GENERATED FROM PYTHON SOURCE LINES 24-26 GP-OLS and plain multi-gene GP ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 26-41 .. code-block:: Python gpols = eq.GPOLSRegressor(population_size=250, generations=30, max_genes=6, err_threshold=0.01, random_state=0).fit(X, y) mggp = eq.MultiGeneRegressor(population_size=250, generations=30, max_genes=6, functions=("add", "mul"), random_state=0).fit(X, y) print("GP-OLS:", gpols.equation_) print("MGGP: ", mggp.equation_) print(gpols.gene_table()) fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) eq.viz.plot_terms(gpols.gene_table().rename(columns={"gene": "term"}), ax=axs[0]) axs[0].set_title("terms of the GP-OLS model", fontsize=9) for k, (name, m) in enumerate((("GP-OLS", gpols), ("MGGP", mggp))): axs[1].plot(m.history_["mean_size"], color=eq.viz.OKABE_ITO[k], lw=2, label=f"{name}: final model size {m.equation_.complexity}") axs[1].set_xlabel("generation") axs[1].set_ylabel("mean individual size") axs[1].legend(frameon=False, fontsize=8) fig.tight_layout() .. image-sg:: /auto_examples/03_gpols/images/sphx_glr_plot_13_gpols_001.png :alt: terms of the GP-OLS model :srcset: /auto_examples/03_gpols/images/sphx_glr_plot_13_gpols_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none GP-OLS: 0.0004351 + 0.4999 * x0 * x3 * x2 + 1.999 * x1 * x0 - 1.5 * x2 * x2 MGGP: 0.0002613 - 0.01076 * x2 * x1 + 0.00538 * (x3 + x0) * (x0 + x2 * x1) * (x2 * x0 + x0) + 0.4999 * x0 * x3 * x2 + 2 * x1 * x0 - 1.499 * x2 * x2 + 0.01551 * x1 * (x1 + x2) * x3 * x2 gene size weight err 0 x1 * x0 3 1.998790 0.658667 1 x2 * x2 3 -1.500182 0.325860 2 x0 * x3 * x2 5 0.499935 0.014647 .. GENERATED FROM PYTHON SOURCE LINES 42-44 Parameter study: the ERR threshold ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 44-60 .. code-block:: Python thresholds = [0.001, 0.005, 0.01, 0.05, 0.1] sizes, r2s = [], [] for th in thresholds: m = eq.GPOLSRegressor(population_size=200, generations=25, err_threshold=th, random_state=0).fit(X, y) sizes.append(m.equation_.complexity) r2s.append(m.score(X, y)) print("ERR threshold", thresholds, "size", sizes, "R2", np.round(r2s, 5).tolist()) fig, ax = plt.subplots(figsize=(4.8, 3)) ax.plot(thresholds, sizes, "o-", color="#285780") ax.set_xscale("log") ax.set_xlabel("ERR threshold") ax.set_ylabel("model size", color="#285780") ax2 = ax.twinx() ax2.plot(thresholds, r2s, "s--", color="#D55E00") ax2.set_ylabel("$R^2$", color="#D55E00") plt.show() .. image-sg:: /auto_examples/03_gpols/images/sphx_glr_plot_13_gpols_002.png :alt: plot 13 gpols :srcset: /auto_examples/03_gpols/images/sphx_glr_plot_13_gpols_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ERR threshold [0.001, 0.005, 0.01, 0.05, 0.1] size [21, 21, 21, 13, 13] R2 [0.99917, 0.99917, 0.99917, 0.98453, 0.98453] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 10.056 seconds) .. _sphx_glr_download_auto_examples_03_gpols_plot_13_gpols.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_13_gpols.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_13_gpols.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_13_gpols.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_13_gpols.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_