.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/09_analysis/plot_37_ensemble.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_09_analysis_plot_37_ensemble.py: Bootstrap ensembles: model uncertainty and stable structure =========================================================== Refitting the search on bootstrap samples of the data (Breiman, 1996) shows which parts of the model the data really support. :class:`~eqdisco.SymbolicEnsemble` aggregates the members' predictions, reports their spread -- largest where the data are sparse -- and counts how often each variable and building block appears. .. GENERATED FROM PYTHON SOURCE LINES 10-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq rng = np.random.default_rng(2) x = np.concatenate([rng.uniform(0, 2, 50), rng.uniform(3.5, 4, 8)]) # a gap in the data z = rng.uniform(0, 1, len(x)) # irrelevant input y = x * np.exp(-0.5 * x) + 0.03 * rng.normal(size=len(x)) X = np.column_stack([x, z]) .. GENERATED FROM PYTHON SOURCE LINES 23-25 Ten bootstrap members --------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-45 .. code-block:: Python ens = eq.SymbolicEnsemble(eq.SymbolicRegressor(population_size=200, generations=20, functions=("add", "sub", "mul", "exp")), n_estimators=10, feature_names=["x", "z"], random_state=0) ens.fit(X, y) print(ens.variable_frequency()) print(ens.structure_frequency(min_support=0.4).head()) xs = np.linspace(0, 5, 300) Xs = np.column_stack([xs, np.full_like(xs, 0.5)]) lo, hi = ens.predict_interval(Xs, coverage=0.9) fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) for e in ens.equations_: axs[0].plot(xs, e.predict(Xs), color="#0072B2", lw=0.6, alpha=0.4) axs[0].fill_between(xs, lo, hi, color="#0072B2", alpha=0.15, label="90 % of the members") axs[0].plot(xs, ens.predict(Xs), color="#285780", lw=2, label="median of the ensemble") axs[0].scatter(x, y, s=10, color="#6B7785", label="data") axs[0].set_xlabel("x") axs[0].legend(frameon=False, fontsize=8) vf = ens.variable_frequency() axs[1].bar(vf.index, vf.to_numpy(), color="#285780") axs[1].set_ylabel("share of members using the variable") fig.tight_layout() .. image-sg:: /auto_examples/09_analysis/images/sphx_glr_plot_37_ensemble_001.png :alt: plot 37 ensemble :srcset: /auto_examples/09_analysis/images/sphx_glr_plot_37_ensemble_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none x 1.0 z 0.4 Name: frequency, dtype: float64 block size support rel_support 0 add(x,x) 3 6 0.6 1 mul(c,x) 3 6 0.6 2 add(c,x) 3 4 0.4 .. GENERATED FROM PYTHON SOURCE LINES 46-48 Parameter study: number of members ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 48-60 .. code-block:: Python ns = [3, 5, 10, 20] width = [] for n in ns: e = eq.SymbolicEnsemble(eq.SymbolicRegressor(population_size=120, generations=12, functions=("add", "sub", "mul", "exp")), n_estimators=n, random_state=1).fit(X, y) a, b = e.predict_interval(Xs) width.append(float(np.mean(b[(xs > 2) & (xs < 3.5)] - a[(xs > 2) & (xs < 3.5)]))) print("members", ns, "mean interval width in 2 < x < 3.5", np.round(width, 3).tolist()) fig, ax = plt.subplots(figsize=(4.5, 3)) ax.plot(ns, width, "o-", color="#285780") ax.set_xlabel("ensemble members") ax.set_ylabel("mean band width in the gap") plt.show() .. image-sg:: /auto_examples/09_analysis/images/sphx_glr_plot_37_ensemble_002.png :alt: plot 37 ensemble :srcset: /auto_examples/09_analysis/images/sphx_glr_plot_37_ensemble_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none members [3, 5, 10, 20] mean interval width in 2 < x < 3.5 [0.075, 0.078, 0.076, 0.092] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 13.766 seconds) .. _sphx_glr_download_auto_examples_09_analysis_plot_37_ensemble.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/09_analysis/plot_37_ensemble.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_37_ensemble.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_37_ensemble.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_37_ensemble.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_