.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_beyond/plot_28_soft_sensor_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_28_soft_sensor_projection.py: Symbolic features for a soft sensor =================================== In soft sensing, a hard-to-measure quality is estimated from easily measured process variables. With goal ``"target"`` the :class:`~eqdisco.SymbolicTransformer` evolves features that make the quality predictable by a linear model -- a goal-oriented projection in the sense of Dörgő, Kulcsár and Abonyi (2021). The data imitate a spectral analyzer: twenty correlated absorbances, of which the quality depends nonlinearly on three. .. GENERATED FROM PYTHON SOURCE LINES 12-27 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq rng = np.random.default_rng(8) n = 400 latent = rng.normal(size=(n, 3)) A = rng.normal(size=(3, 20)) X = latent @ A + 0.05 * rng.normal(size=(n, 20)) X = X - X.min(0) + 0.1 quality = np.log(X[:, 3]) * X[:, 7] - 0.5 * X[:, 12] + 0.02 * rng.normal(size=n) names = [f"a{j}" for j in range(20)] .. GENERATED FROM PYTHON SOURCE LINES 28-30 Two features that predict the quality ------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-45 .. code-block:: Python st = eq.SymbolicTransformer(n_components=2, goal="target", 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[:300], quality[:300]) print([str(e) for e in st.equations_], "cost", round(st.cost_, 4)) from sklearn.linear_model import LinearRegression # noqa: E402 lin = LinearRegression().fit(Z, quality[:300]) print("test R2 of the linear soft sensor on the two features:", lin.score(st.transform(X[300:]), quality[300:])) fig, axs = plt.subplots(1, 2, figsize=(10, 3.6)) sc = axs[0].scatter(Z[:, 0], Z[:, 1], c=quality[:300], cmap="viridis", s=12) fig.colorbar(sc, ax=axs[0], label="quality") axs[0].set_xlabel("$z_0$") axs[0].set_ylabel("$z_1$") eq.viz.plot_parity(quality[300:], lin.predict(st.transform(X[300:])), ax=axs[1], label="test") fig.tight_layout() .. image-sg:: /auto_examples/07_beyond/images/sphx_glr_plot_28_soft_sensor_projection_001.png :alt: plot 28 soft sensor projection :srcset: /auto_examples/07_beyond/images/sphx_glr_plot_28_soft_sensor_projection_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ['a0 / a3 - a3 * a15', 'a3 * a15 - a13 * a1'] cost 0.0346 test R2 of the linear soft sensor on the two features: 0.9574978690948058 .. GENERATED FROM PYTHON SOURCE LINES 46-48 Parameter study: the variable penalty ------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 48-64 .. code-block:: Python pens = [0.0, 0.002, 0.01, 0.05] nv, r2 = [], [] for p in pens: m = eq.SymbolicTransformer(n_components=2, goal="target", population_size=150, generations=20, functions=("add", "sub", "mul", "div", "log"), variable_penalty=p, random_state=0).fit(X[:300], quality[:300]) nv.append(len({v for e in m.equations_ for v in e.variables})) r2.append(LinearRegression().fit(m.transform(X[:300]), quality[:300]).score(m.transform(X[300:]), quality[300:])) print("variable penalty", pens, "variables used", nv, "test R2", np.round(r2, 3).tolist()) fig, ax = plt.subplots(figsize=(4.8, 3)) ax.plot(pens, nv, "o-", color="#285780") ax.set_xscale("symlog", linthresh=1e-3) ax.set_xlabel("penalty per variable") ax.set_ylabel("variables used", color="#285780") ax2 = ax.twinx() ax2.plot(pens, r2, "s--", color="#D55E00") ax2.set_ylabel("test $R^2$", color="#D55E00") plt.show() .. image-sg:: /auto_examples/07_beyond/images/sphx_glr_plot_28_soft_sensor_projection_002.png :alt: plot 28 soft sensor projection :srcset: /auto_examples/07_beyond/images/sphx_glr_plot_28_soft_sensor_projection_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none variable penalty [0.0, 0.002, 0.01, 0.05] variables used [5, 4, 3, 3] test R2 [0.972, 0.942, 0.929, 0.928] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.619 seconds) .. _sphx_glr_download_auto_examples_07_beyond_plot_28_soft_sensor_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_28_soft_sensor_projection.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_28_soft_sensor_projection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_28_soft_sensor_projection.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_28_soft_sensor_projection.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_