Note
Go to the end to download the full example code or to run this example in your browser via Binder.
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
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.
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)]
Two features that predict the quality#
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()

['a0 / a3 - a3 * a15', 'a3 * a15 - a13 * a1'] cost 0.0346
test R2 of the linear soft sensor on the two features: 0.9574978690948058
Parameter study: the variable penalty#
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()

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]
Total running time of the script: (0 minutes 2.619 seconds)