.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_basics/plot_04_linear_scaling.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_01_basics_plot_04_linear_scaling.py: Linear scaling: let regression handle offset and scale ====================================================== A target such as :math:`y = 120 + 35\,x_0 x_1` has the shape of :math:`x_0 x_1`, but genetic programming has to build the constants 120 and 35 from random leaves before an otherwise perfect shape scores well. Keijzer (2003) proposed to evaluate every candidate :math:`f` as :math:`a + b f` with the least-squares :math:`a` and :math:`b`, which cost one pass over the data. The search then rewards shapes, not constants. This example compares ten runs with and without linear scaling. .. 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(0) X = rng.uniform(-1, 1, (200, 2)) y = 120 + 35 * X[:, 0] * X[:, 1] + 3 * np.sin(3 * X[:, 0]) .. GENERATED FROM PYTHON SOURCE LINES 24-26 Ten runs with and without scaling --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 26-43 .. code-block:: Python hist = {True: [], False: []} for scaling in (True, False): for seed in range(10): sr = eq.SymbolicRegressor(population_size=200, generations=20, linear_scaling=scaling, functions=("add", "sub", "mul", "sin"), random_state=seed) sr.fit(X, y) hist[scaling].append(sr.history_["best_loss"].to_numpy() / np.var(y)) print({("with" if s else "without") + " scaling": f"median final NMSE {np.median(np.array(hist[s])[:, -1]):.2e}" for s in (True, False)}) fig, ax = plt.subplots(figsize=(5.5, 3.4)) for scaling, color in ((True, "#285780"), (False, "#D55E00")): H = np.array(hist[scaling]) for h in H: ax.semilogy(h, color=color, lw=0.6, alpha=0.35) ax.semilogy(np.median(H, 0), color=color, lw=2.2, label=("with" if scaling else "without") + " linear scaling (median)") ax.set_xlabel("generation") ax.set_ylabel("normalized MSE of the best model") ax.legend(frameon=False, fontsize=8) .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_04_linear_scaling_001.png :alt: plot 04 linear scaling :srcset: /auto_examples/01_basics/images/sphx_glr_plot_04_linear_scaling_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none {'with scaling': 'median final NMSE 6.39e-03', 'without scaling': 'median final NMSE 2.60e-01'} .. GENERATED FROM PYTHON SOURCE LINES 44-48 Parameter study: the size of the offset --------------------------------------- Without scaling, the larger the offset the harder the problem; with scaling it does not matter at all. .. GENERATED FROM PYTHON SOURCE LINES 48-64 .. code-block:: Python offsets = [0, 10, 100, 1000] res = {True: [], False: []} for c in offsets: yc = c + 35 * X[:, 0] * X[:, 1] + 3 * np.sin(3 * X[:, 0]) for scaling in (True, False): r2s = [eq.SymbolicRegressor(population_size=150, generations=15, linear_scaling=scaling, functions=("add", "sub", "mul", "sin"), random_state=s).fit(X, yc).score(X, yc) for s in range(4)] res[scaling].append(np.median(r2s)) print("median R2 by offset", offsets, {("with" if s else "without"): np.round(res[s], 3).tolist() for s in (True, False)}) fig, ax = plt.subplots(figsize=(4.5, 3)) for scaling, color in ((True, "#285780"), (False, "#D55E00")): ax.plot(offsets[1:], res[scaling][1:], "o-", color=color, label="with scaling" if scaling else "without scaling") ax.set_xscale("log") ax.set_xlabel("offset of the target") ax.set_ylabel("median $R^2$ (4 runs)") ax.legend(frameon=False, fontsize=8) plt.show() .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_04_linear_scaling_002.png :alt: plot 04 linear scaling :srcset: /auto_examples/01_basics/images/sphx_glr_plot_04_linear_scaling_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none median R2 by offset [0, 10, 100, 1000] {'with': [0.992, 0.992, 0.996, 0.991], 'without': [0.991, 0.987, 0.497, 0.354]} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 22.926 seconds) .. _sphx_glr_download_auto_examples_01_basics_plot_04_linear_scaling.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/01_basics/plot_04_linear_scaling.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_linear_scaling.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_linear_scaling.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_linear_scaling.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_