.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/05_probabilistic/plot_20_grammar_ce.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_05_probabilistic_plot_20_grammar_ce.py: Learning a grammar of good expressions (new in eqdisco) ======================================================= :class:`~eqdisco.GrammarCERegressor` keeps a probability table over the symbols of an expression, conditioned on the parent symbol and the argument position. Each iteration samples a batch of expressions, keeps only the best few percent -- the *risk-seeking* elite of deep symbolic regression (Petersen et al., 2021) -- and moves the table toward their symbol frequencies, as the cross-entropy method does. The search distribution concentrates on the useful structure. The target is Nguyen-10, :math:`2\sin x\cos y`. .. 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("nguyen-10") .. GENERATED FROM PYTHON SOURCE LINES 22-24 Learning curve -------------- .. GENERATED FROM PYTHON SOURCE LINES 24-44 .. code-block:: Python gce = eq.GrammarCERegressor(functions=("add", "sub", "mul", "sin", "cos"), n_iterations=40, batch_size=300, elite_fraction=0.05, random_state=0, feature_names=["x", "y"]) gce.fit(prob.X_train, prob.y_train) print(gce.equation_) root = gce.probabilities_[(-1, 0)] print("root symbol probabilities:", {k: round(p, 2) for k, p in sorted(root.items(), key=lambda kv: -kv[1])[:4]}) fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) h = gce.history_ axs[0].plot(h["iteration"], h["best_reward"], color="#285780", lw=2, label="best reward") axs[0].plot(h["iteration"], h["elite_reward"], color="#285780", lw=1, ls="--", label="mean elite reward") ax2 = axs[0].twinx() ax2.plot(h["iteration"], h["entropy"], color="#D55E00", lw=1.2) ax2.set_ylabel("mean entropy of the table", color="#D55E00") axs[0].set_xlabel("iteration") axs[0].set_ylabel("reward 1/(1+NRMSE)") axs[0].legend(frameon=False, fontsize=8, loc="lower right") syms = sorted(root, key=lambda k: -root[k]) axs[1].bar(syms, [root[s] for s in syms], color="#285780") axs[1].set_ylabel("probability at the root") fig.tight_layout() .. image-sg:: /auto_examples/05_probabilistic/images/sphx_glr_plot_20_grammar_ce_001.png :alt: plot 20 grammar ce :srcset: /auto_examples/05_probabilistic/images/sphx_glr_plot_20_grammar_ce_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2 * sin(x) * cos(y) root symbol probabilities: {'mul': 0.97, 'add': 0.0, 'sub': 0.0, 'sin': 0.0} .. GENERATED FROM PYTHON SOURCE LINES 45-47 Parameter study: how risk-seeking? ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 47-57 .. code-block:: Python fracs = [0.01, 0.05, 0.2, 0.5] fig, ax = plt.subplots(figsize=(4.5, 3)) for k, f in enumerate(fracs): H = np.mean([eq.GrammarCERegressor(functions=("add", "sub", "mul", "sin", "cos"), n_iterations=25, batch_size=200, elite_fraction=f, random_state=s).fit(prob.X_train, prob.y_train).history_["best_reward"] for s in range(3)], axis=0) print(f"elite {int(100 * f)} %: mean best reward at iteration 25: {H[-1]:.3f}") ax.plot(H, color=eq.viz.OKABE_ITO[k], label=f"elite {int(100 * f)} %") ax.set_xlabel("iteration") ax.set_ylabel("mean best reward (3 runs)") ax.legend(frameon=False, fontsize=8) plt.show() .. image-sg:: /auto_examples/05_probabilistic/images/sphx_glr_plot_20_grammar_ce_002.png :alt: plot 20 grammar ce :srcset: /auto_examples/05_probabilistic/images/sphx_glr_plot_20_grammar_ce_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none elite 1 %: mean best reward at iteration 25: 0.949 elite 5 %: mean best reward at iteration 25: 0.996 elite 20 %: mean best reward at iteration 25: 0.949 elite 50 %: mean best reward at iteration 25: 0.983 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.885 seconds) .. _sphx_glr_download_auto_examples_05_probabilistic_plot_20_grammar_ce.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/05_probabilistic/plot_20_grammar_ce.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_20_grammar_ce.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_20_grammar_ce.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_20_grammar_ce.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_