.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/09_analysis/plot_35_building_blocks.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_35_building_blocks.py: Building blocks and variable interactions ========================================= Which subtrees does the search keep reusing, and which variables does it combine nonlinearly? The frequent subtrees of a final population form a co-occurrence network (:func:`~eqdisco.analysis.building_block_network`), and the joint occurrence of variables under nonlinear functions a variable interaction network in the spirit of Kronberger et al. (2011). The target is Vladislavleva-7, :math:`(x-3)(y-3)+2\sin((x-4)(y-4))`. .. GENERATED FROM PYTHON SOURCE LINES 12-25 .. code-block:: Python import matplotlib.pyplot as plt import eqdisco as eq from eqdisco.analysis import building_block_network, frequent_building_blocks, variable_interaction_network prob = eq.datasets.get_problem("vladislavleva-7") X = prob.X_train import numpy as np # noqa: E402 X3 = np.column_stack([X, np.random.default_rng(0).uniform(0, 6, len(X))]) # an irrelevant third input z sr = eq.SymbolicRegressor(population_size=400, generations=30, functions=("add", "sub", "mul", "sin"), feature_names=["x", "y", "z"], random_state=0).fit(X3, prob.y_train) .. GENERATED FROM PYTHON SOURCE LINES 26-28 Frequent blocks of the final population --------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-40 .. code-block:: Python pop = sr.population_[:200] print(frequent_building_blocks(pop, min_support=0.25).head(8)) G = building_block_network(pop, min_support=0.2, min_lift=1.05) V = variable_interaction_network(pop) print({e: round(d["weight"], 2) for e, d in ((tuple(sorted(e)), V.edges[e]) for e in V.edges)}) fig, axs = plt.subplots(1, 2, figsize=(11, 4.4), gridspec_kw={"width_ratios": [1.5, 1]}) eq.viz.plot_building_blocks(G, ax=axs[0]) axs[0].set_title("frequent building blocks (area: support; edges: lift)", fontsize=9) eq.viz.plot_variable_interactions(V, ax=axs[1]) axs[1].set_title("variable interaction network", fontsize=9) fig.tight_layout() .. image-sg:: /auto_examples/09_analysis/images/sphx_glr_plot_35_building_blocks_001.png :alt: frequent building blocks (area: support; edges: lift), variable interaction network :srcset: /auto_examples/09_analysis/images/sphx_glr_plot_35_building_blocks_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none block size support rel_support 0 sin(y) 2 200 1.000 1 sin(x) 2 200 1.000 2 sub(c,y) 3 200 1.000 3 sub(y,x) 3 200 1.000 4 mul(sin(y),sub(y,x)) 6 196 0.980 5 mul(sin(x),sub(c,y)) 6 194 0.970 6 sin(sin(x)) 3 121 0.605 7 sin(sub(y,x)) 4 79 0.395 {(np.str_('x'), np.str_('y')): 1.0, (np.str_('x'), np.str_('z')): 0.1, (np.str_('y'), np.str_('z')): 0.1} .. GENERATED FROM PYTHON SOURCE LINES 41-43 Parameter study: minimum support -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 43-50 .. code-block:: Python sups = [0.05, 0.1, 0.2, 0.4] print("min support", sups, "blocks", [building_block_network(pop, min_support=s).number_of_nodes() for s in sups]) fig, ax = plt.subplots(figsize=(4.5, 3)) ax.plot(sups, [building_block_network(pop, min_support=s).number_of_nodes() for s in sups], "o-", color="#285780") ax.set_xlabel("minimum support") ax.set_ylabel("building blocks") plt.show() .. image-sg:: /auto_examples/09_analysis/images/sphx_glr_plot_35_building_blocks_002.png :alt: plot 35 building blocks :srcset: /auto_examples/09_analysis/images/sphx_glr_plot_35_building_blocks_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none min support [0.05, 0.1, 0.2, 0.4] blocks [30, 24, 12, 7] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.905 seconds) .. _sphx_glr_download_auto_examples_09_analysis_plot_35_building_blocks.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_35_building_blocks.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_35_building_blocks.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_35_building_blocks.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_35_building_blocks.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_