Note
Go to the end to download the full example code or to run this example in your browser via Binder.
A one-page report of a search#
plot_dashboard() summarizes a fitted search on one page: the
selected equation, its tree, the Pareto front on test data, a parity plot and
the convergence of the run. Together with the model card it documents a model
for colleagues who did not run the search. The data are Feynman I.12.2,
Coulomb’s law \(F=q_1q_2/(4\pi\varepsilon r^2)\), with 0.5 % noise.
import matplotlib.pyplot as plt
import eqdisco as eq
prob = eq.datasets.get_problem("feynman-I.12.2", noise=0.005, n_train=300)
Search and report#
sr = eq.SymbolicRegressor(population_size=400, generations=30, functions=("add", "sub", "mul", "div", "sqrt", "square"), optimize="lm", feature_names=prob.feature_names, random_state=0)
sr.fit(prob.X_train, prob.y_train)
print("selected: ", sr.equation_)
print("simplified:", eq.simplify.simplify(sr.equation_, prob.X_train, prob.y_train, sympy=True, refit=True, round_digits=True))
print("law recovered (affine, tol 1e-2):", eq.benchmarks.is_symbolic_solution(sr.equation_, prob.truth, X=prob.X_test, affine=True, tol=1e-2))
fig = eq.viz.plot_dashboard(sr, prob.X_train, prob.y_train, prob.X_test, prob.y_test)

selected: -5.628e-05 + 1.254 * (q2 - 0.002241) * q1 / r / (-1.984)^2 / (sqrt(r^2) / ((r / (r + r))^2 / ((epsilon * epsilon)^2 * (-2.414e-05) + epsilon)))
simplified: 0.0796 * (q2 - 0.00224) * q1 / r / abs(r) / (-2.41e-05 * (epsilon^2)^2 + epsilon) - 5.63e-05
law recovered (affine, tol 1e-2): True
Parameter study: population size and recovery#
sizes = [100, 200, 400]
hits = []
for mu in sizes:
h = 0
for s in range(3):
m = eq.SymbolicRegressor(population_size=mu, generations=20, functions=("add", "sub", "mul", "div", "sqrt", "square"), optimize="lm", feature_names=prob.feature_names, random_state=s).fit(prob.X_train, prob.y_train)
h += eq.benchmarks.is_symbolic_solution(m.equation_, prob.truth, X=prob.X_test, affine=True, tol=1e-2)
hits.append(h)
print("population", sizes, "recoveries of 3", hits)
fig, ax = plt.subplots(figsize=(4, 3))
ax.bar([str(s) for s in sizes], hits, color="#285780")
ax.set_xlabel("population size")
ax.set_ylabel("recoveries (of 3 runs)")
plt.show()

population [100, 200, 400] recoveries of 3 [2, 1, 3]
Total running time of the script: (0 minutes 46.066 seconds)