.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_sparse/plot_17_ffx.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_04_sparse_plot_17_ffx.py: Fast Function Extraction (FFX) ============================== FFX (McConaghy, 2011) avoids evolution altogether: it generates thousands of basis functions -- powers, absolute values, logarithms, hinge functions and their pairwise products -- and runs the elastic-net regularization path. Every point of the path is a model with a different number of bases; the non-dominated ones form a front of accuracy versus size, computed in a fraction of a second. The test problem is Vladislavleva-1, a ratio of a Gaussian bump and a quadratic. .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: Python import matplotlib.pyplot as plt import eqdisco as eq prob = eq.datasets.get_problem("vladislavleva-1") .. GENERATED FROM PYTHON SOURCE LINES 20-22 The FFX front ------------- .. GENERATED FROM PYTHON SOURCE LINES 22-39 .. code-block:: Python ffx = eq.FFXRegressor(n_knots=3, max_bases=15).fit(prob.X_train, prob.y_train) print(len(ffx.bases_), "candidate bases;", len(ffx.pareto_front_), "models on the front") print(ffx.equation_) fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) nb = [e.meta["n_bases"] for e in ffx.pareto_front_] tr = [1 - e.score(prob.X_train, prob.y_train) for e in ffx.pareto_front_] te = [1 - e.score(prob.X_test, prob.y_test) for e in ffx.pareto_front_] print("test 1-R2 of the model with the most bases:", f"{te[-1]:.4f}", "| best test 1-R2 on the front:", f"{min(te):.4f}", "with", nb[te.index(min(te))], "bases") axs[0].step(nb, tr, where="post", color="#285780", label="train") axs[0].step(nb, te, where="post", color="#009E73", label="test") axs[0].set_yscale("log") axs[0].set_xlabel("number of bases") axs[0].set_ylabel("$1-R^2$") axs[0].legend(frameon=False, fontsize=8) eq.viz.plot_parity(prob.y_test, ffx.predict(prob.X_test), ax=axs[1], label="test") fig.tight_layout() .. image-sg:: /auto_examples/04_sparse/images/sphx_glr_plot_17_ffx_001.png :alt: plot 17 ffx :srcset: /auto_examples/04_sparse/images/sphx_glr_plot_17_ffx_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 36 candidate bases; 14 models on the front 1.251 + 0.2901 * log(x0) - 0.05735 * max(0, 1.408 - x0) + 0.3784 * max(0, x0 - 2.361) + 0.1073 * max(0, 2.361 - x0) + 0.07957 * max(0, x0 - 3.311) + 0.05209 * max(0, 1.277 - x1) + 0.02544 * max(0, x1 - 2.377) - 0.5452 * max(0, 2.377 - x1) - 0.2035 * max(0, x1 - 3.343) - 0.2542 * x0 * x1 - 0.05366 * x0^2 * x1 + 0.01372 * x0^2 * x1^2 + 0.03916 * x0^2 * log(x1) + 0.0438 * log(x0) * x1^2 test 1-R2 of the model with the most bases: 0.0344 | best test 1-R2 on the front: 0.0344 with 14 bases .. GENERATED FROM PYTHON SOURCE LINES 40-42 Parameter study: interactions and hinge knots --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 42-53 .. code-block:: Python settings = {"univariate": dict(interactions=False, n_knots=0), "+ hinges": dict(interactions=False, n_knots=3), "+ interactions": dict(interactions=True, n_knots=0), "all": dict(interactions=True, n_knots=3)} fig, ax = plt.subplots(figsize=(4.8, 3.2)) for k, (name, kw) in enumerate(settings.items()): f = eq.FFXRegressor(max_bases=15, **kw).fit(prob.X_train, prob.y_train) print(f"{name:15s} best test 1-R2 per setting: {min(1 - e.score(prob.X_test, prob.y_test) for e in f.pareto_front_):.4f}") ax.step([e.meta["n_bases"] for e in f.pareto_front_], [1 - e.score(prob.X_test, prob.y_test) for e in f.pareto_front_], where="post", color=eq.viz.OKABE_ITO[k], label=name) ax.set_yscale("log") ax.set_xlabel("number of bases") ax.set_ylabel("$1-R^2$ (test)") ax.legend(frameon=False, fontsize=8) plt.show() .. image-sg:: /auto_examples/04_sparse/images/sphx_glr_plot_17_ffx_002.png :alt: plot 17 ffx :srcset: /auto_examples/04_sparse/images/sphx_glr_plot_17_ffx_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none univariate best test 1-R2 per setting: 0.1601 + hinges best test 1-R2 per setting: 0.1383 + interactions best test 1-R2 per setting: 0.0351 all best test 1-R2 per setting: 0.0344 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.704 seconds) .. _sphx_glr_download_auto_examples_04_sparse_plot_17_ffx.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/04_sparse/plot_17_ffx.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_17_ffx.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_17_ffx.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_17_ffx.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_