eqdisco.models.SymbolicRegressor#

class eqdisco.models.SymbolicRegressor(population_size=500, generations=50, functions=('add', 'sub', 'mul', 'div', 'sin', 'cos', 'exp', 'log', 'sqrt', 'square'), selection='tournament', tournament_size=5, parsimony=0.0, complexity='size', init_depth=(2, 5), max_depth=10, max_size=60, p_crossover=0.7, crossover='subtree', p_subtree_mutation=0.4, p_point_mutation=0.25, p_hoist_mutation=0.1, p_constant_mutation=0.15, p_shrink_mutation=0.05, p_insert_mutation=0.05, p_constant=0.2, const_range=(-5.0, 5.0), linear_scaling=True, loss='mse', optimize='none', optimize_fraction=0.1, optimize_iterations=10, n_islands=1, migration_interval=10, migration_size=2, elitism=1, lexicase_epsilon='mad', constraints=None, units=None, constraint_weight=10.0, seed_equations=None, library=None, p_library=0.05, library_learning=False, library_interval=5, llm_proposer=None, llm_interval=5, llm_n_proposals=5, validation_fraction=0.0, model_selection='bic', selection_tolerance=0.01, refit=True, early_stopping_loss=None, patience=None, time_limit=None, max_evaluations=None, feature_names=None, random_state=None, verbose=0)[source]#

Bases: _TreeGP, RegressorMixin

Genetic programming for symbolic regression.

Parameters:
population_sizeint, default=500

Number of individuals \(\mu\) (split equally among the islands).

generationsint, default=50

Number of generations \(G\).

functionssequence of str, default=DEFAULT_FUNCTIONS

Function set \(\mathcal{F}\) (names of eqdisco.expr.PRIMITIVES).

selection{“tournament”, “double_tournament”, “lexicase”, “nsga2”, “afp”}

Selection scheme.

tournament_sizeint, default=5
parsimonyfloat, default=0.0

Penalty per unit of complexity added to the loss (parsimony pressure).

complexitystr, default=”size”

Complexity measure (eqdisco.fitness.complexity()).

init_depthtuple of int, default=(2, 5)

Depth range of ramped half-and-half initialization.

max_depth, max_sizeint

Limits of the offspring (an offspring that violates them is retried, then replaced by its parent).

p_crossoverfloat, default=0.7

Probability of crossover; otherwise one of the mutations is applied, chosen with probabilities proportional to the p_*_mutation weights.

crossover{“subtree”, “semantic”}
p_constantfloat, default=0.2

Probability that a new leaf is a constant.

const_rangetuple of float, default=(-5, 5)
linear_scalingbool, default=True

Evaluate \(a + b f\) with optimal \(a, b\) (Keijzer, 2003).

lossstr or callable(), default=”mse”
optimize{“none”, “lm”}, default=”none”

Local parameter optimization of the best optimize_fraction of the offspring by Levenberg–Marquardt (Lamarckian: the tuned parameters are inherited).

optimize_fractionfloat, default=0.1
optimize_iterationsint, default=10
n_islandsint, default=1
migration_interval, migration_sizeint

Ring migration of the migration_size best individuals every migration_interval generations.

elitismint, default=1

Number of best individuals copied unchanged (generational schemes).

constraintslist of ShapeConstraint, optional

Prior knowledge on the shape of the model (monotonicity, convexity, bounds).

unitsdict, optional

Physical units of the variables and of the target for dimensional consistency ({"x0": "m", "x1": "s", "__target__": "m/s"}).

constraint_weightfloat, default=10.0

Weight of the constraint and unit penalties (relative to the variance of the target).

seed_equationslist, optional

Equations (strings, Equation or Expression) placed in the initial population.

librarylist, optional

Building blocks inserted into offspring with rate p_library.

library_learningbool, default=False

Mine the library from the frequent subtrees of the best individuals every library_interval generations (new in eqdisco).

llm_proposercallable(), optional

A proposer (prompt -> text) whose suggestions are added every llm_interval generations (see eqdisco.llm).

validation_fractionfloat, default=0.0

Share of the data held out to choose the final model.

model_selection{“bic”, “aic”, “best”, “knee”, “tolerance”, “score”, “validation”}

Rule that picks equation_ from the Pareto front (eqdisco.analysis.pareto.select_model()); the default is the lowest Bayesian information criterion, with exact fits tied and ties broken by size, which guards against selecting bloated models on small data.

selection_tolerancefloat, default=0.01
refitbool, default=True

Re-estimate the parameters of the front by Levenberg–Marquardt at the end.

early_stopping_lossfloat, optional

Stop when the best loss is at most this value.

patienceint, optional

Stop after patience generations without improvement.

time_limitfloat, optional

Wall-clock limit in seconds.

random_stateint, optional
Attributes:
equation_Equation

The selected model.

pareto_front_list of Equation

Non-dominated models (training error vs size), by increasing size.

history_pandas.DataFrame

One row per generation.

n_evaluations_int

Number of distinct fitness evaluations.

Examples

>>> import numpy as np
>>> from eqdisco import SymbolicRegressor
>>> rng = np.random.default_rng(0)
>>> X = rng.uniform(-2, 2, size=(200, 2))
>>> y = X[:, 0] ** 2 + 0.5 * X[:, 1]
>>> sr = SymbolicRegressor(population_size=200, generations=15, random_state=1,
...                        functions=("add", "sub", "mul")).fit(X, y)
>>> sr.score(X, y) > 0.99
True
fit(X, y)[source]#

Run the search on (X, y).

predict(X)[source]#

Output of equation_.

sympy()[source]#

SymPy form of equation_.