.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_beyond/plot_27_goal_oriented_projection.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_07_beyond_plot_27_goal_oriented_projection.py: Goal-oriented projection with symbolic features =============================================== Dimension reduction optimizes a cost: variance for principal components, distances for Sammon mapping. Dörgő, Kulcsár and Abonyi (2021) proposed to define the cost by the goal of the analysis and to find the projection functions by multi-gene genetic programming, so that the map is made of a few explicit, interpretable equations. :class:`~eqdisco.SymbolicTransformer` evolves two features that separate the three cultivars of the wine data set (goal ``"fisher"``) and compares the map with the first two principal components. .. GENERATED FROM PYTHON SOURCE LINES 13-25 .. code-block:: Python import matplotlib.pyplot as plt from sklearn.datasets import load_wine from sklearn.decomposition import PCA from sklearn.preprocessing import StandardScaler import eqdisco as eq data = load_wine() X, y = data.data, data.target names = [n.replace("/", "_").replace("od280_od315_of_diluted_wines", "od280") for n in data.feature_names] .. GENERATED FROM PYTHON SOURCE LINES 26-28 Two symbolic features --------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-45 .. code-block:: Python st = eq.SymbolicTransformer(n_components=2, goal="fisher", population_size=250, generations=30, functions=("add", "sub", "mul", "div", "log"), variable_penalty=0.002, feature_names=names, random_state=0) Z = st.fit_transform(X, y) for k, e in enumerate(st.equations_): print(f"z{k} =", e) P = PCA(2).fit_transform(StandardScaler().fit_transform(X)) fig, axs = plt.subplots(1, 2, figsize=(10, 3.8)) for ax, M, title in ((axs[0], P, "principal components (13 variables)"), (axs[1], Z, "two symbolic features")): for c in range(3): ax.scatter(M[y == c, 0], M[y == c, 1], s=12, color=eq.viz.OKABE_ITO[c], label=data.target_names[c]) ax.set_title(title, fontsize=9) ax.set_xticks([]) ax.set_yticks([]) axs[1].set_xlabel("$z_0$ = " + str(st.equations_[0])[:45], fontsize=8) axs[1].set_ylabel("$z_1$ = " + str(st.equations_[1])[:45], fontsize=8) axs[0].legend(frameon=False, fontsize=8) fig.tight_layout() .. image-sg:: /auto_examples/07_beyond/images/sphx_glr_plot_27_goal_oriented_projection_001.png :alt: principal components (13 variables), two symbolic features :srcset: /auto_examples/07_beyond/images/sphx_glr_plot_27_goal_oriented_projection_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none z0 = ((log(flavanoids) + log(proline)) / flavanoids + color_intensity) * ash z1 = (3.738 - (log(flavanoids) + log(proline))) * ((flavanoids - color_intensity) / od280 / od280 + log(proline)) .. GENERATED FROM PYTHON SOURCE LINES 46-48 Parameter study: the goal ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 48-61 .. code-block:: Python goals = ["fisher", "knn", "sammon"] fig, axs = plt.subplots(1, 3, figsize=(11, 3.2)) for ax, g in zip(axs, goals): m = eq.SymbolicTransformer(n_components=2, goal=g, population_size=150, generations=20, functions=("add", "sub", "mul", "div", "log"), random_state=0) Zg = m.fit_transform(X, y if g != "sammon" else None) for c in range(3): ax.scatter(Zg[y == c, 0], Zg[y == c, 1], s=8, color=eq.viz.OKABE_ITO[c]) used = sorted({v for e in m.equations_ for v in e.variables}) print(f"goal {g}: variables used: {used}") ax.set_title(f"goal {g}: {len(used)} variables", fontsize=9) ax.set_xticks([]) ax.set_yticks([]) plt.show() .. image-sg:: /auto_examples/07_beyond/images/sphx_glr_plot_27_goal_oriented_projection_002.png :alt: goal fisher: 8 variables, goal knn: 8 variables, goal sammon: 7 variables :srcset: /auto_examples/07_beyond/images/sphx_glr_plot_27_goal_oriented_projection_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none goal fisher: variables used: [np.str_('x1'), np.str_('x10'), np.str_('x11'), np.str_('x12'), np.str_('x2'), np.str_('x5'), np.str_('x6'), np.str_('x9')] goal knn: variables used: [np.str_('x1'), np.str_('x10'), np.str_('x11'), np.str_('x12'), np.str_('x2'), np.str_('x5'), np.str_('x6'), np.str_('x9')] goal sammon: variables used: [np.str_('x0'), np.str_('x10'), np.str_('x11'), np.str_('x5'), np.str_('x6'), np.str_('x8'), np.str_('x9')] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.178 seconds) .. _sphx_glr_download_auto_examples_07_beyond_plot_27_goal_oriented_projection.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/07_beyond/plot_27_goal_oriented_projection.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_27_goal_oriented_projection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_27_goal_oriented_projection.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_27_goal_oriented_projection.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_