lawsynth/python/lawsynth-sklearn
caelum0x 2b734f3a20 feat(v2-B): scikit-learn adapter (LawSynthDynamics/Regressor/Transformer)
The #1 adoption lever from the competitive analysis: a sklearn-compatible package
(python/lawsynth-sklearn) so the scikit-learn ecosystem adopts LawSynth with
near-zero switching cost (why gplearn/PySINDy win mindshare).
- LawSynthDynamics: fit(X, t=)/predict/simulate/score/equations over the real
  Study discovery; full get_params/set_params/__sklearn_tags__/NotFittedError.
- LawSynthRegressor (RegressorMixin), LawSynthTransformer (feature library +
  deterministic decorrelation prune), parsimony='auto' (lambda = Cov/Var).
- Inherits sklearn mixins when present; faithful standalone fallback otherwise.
Demo recovers an oscillator R2=0.999996; 50 tests pass (7 sklearn-integration
skip cleanly when sklearn absent). Deterministic, offline.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-18 02:34:31 +03:00
..

lawsynth-sklearn

scikit-learn-compatible estimators for LawSynth — deterministic, offline governing-equation discovery that drops into the sklearn ecosystem with near-zero switching cost.

Three adapters wrap LawSynth's Study discovery loop in the scikit-learn estimator contract (fit / predict / score / get_params / set_params / __sklearn_tags__ / NotFittedError):

Estimator Mixin What it does
LawSynthDynamics BaseEstimator Flagship. Discover governing dynamics from a multivariate time-series; predict / simulate forecast trajectories, score returns trajectory R², equations() returns readable laws.
LawSynthRegressor RegressorMixin Strict static-fit framing for Pipeline / GridSearchCV: discovers the coupled world for [predictors…, target] and reconstructs the target trajectory.
LawSynthTransformer TransformerMixin Emit the polynomial / trigonometric feature library, with an optional deterministic correlation prune (least-correlated subset).

Install

pip install -e .            # requires the sibling `lawsynth` package
pip install -e .[sklearn]   # add scikit-learn + numpy for full interop

The estimators inherit real scikit-learn mixins when sklearn is installed, and degrade to a standalone implementation of the same contract when it is absent — the package stays importable and usable with plain Python lists in a pure-standard-library, offline environment.

Quickstart

import math
from lawsynth_sklearn import LawSynthDynamics

t = [i * 0.05 for i in range(160)]
X = [[math.cos(ti), -math.sin(ti)] for ti in t]        # columns: [x, v]

dyn = LawSynthDynamics(polynomial_degree=1).fit(X, t=t, state=["x", "v"])
print(dyn.equations())          # {'x': 'dx/dt = 0.9996·v', 'v': 'dv/dt = -0.9996·x'}
print(dyn.score(X, t))          # ~1.0  (trajectory R²)
traj = dyn.simulate(horizon=2.0, initial={"x": 1.0, "v": 0.0})

See examples/sklearn_quickstart.py for the Pipeline / clone / transformer / auto-parsimony walk-through.

Auto-parsimony

Pass parsimony="auto" to LawSynthDynamics / LawSynthRegressor to select the sparsity threshold automatically. It sweeps a deterministic grid of thresholds, filters candidates to the (complexity, loss) Pareto front, and prices complexity with

λ = Cov(complexity, loss) / Var(complexity)

(the same heuristic as gplearn's parsimony_coefficient='auto'), then selects the model minimising loss + |λ|·complexity. The chosen threshold and λ are exposed as parsimony_coefficient_ and the scored sweep as parsimony_candidates_.

Determinism

LawSynth discovery is bit-exact and offline; every path here — the threshold sweep, the correlation prune, the feature library — is deterministic, so identical inputs reproduce identical estimators, equations, and forecasts.