How to export the output of gplearn as a sympy expression or some other readable format?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
gplearn models are symbolic programs, so the main challenge is not getting a prediction out of them but making the evolved program readable and reusable elsewhere. In practice, the export path usually starts with the estimator's best program object, then moves either to a string representation, a Graphviz tree, or a custom conversion into a sympy expression.
Start with the Best Program Object
After fitting a SymbolicRegressor, the learned expression is stored on the estimator's internal program object.
That string form is often the first readable output you need. It is not yet a native sympy object, but it is a clear serialization of the evolved expression tree.
One caveat matters here: _program is an internal attribute by naming convention. It is widely used in examples, but it is still not the cleanest long-term public export API.
Use Built-In Readable Formats First
Before writing a symbolic converter, check whether a simpler export format already solves your problem.
For visual inspection, gplearn exposes Graphviz output on the program object:
That is often enough when you want:
- a readable tree
- documentation for a report
- quick manual inspection of the evolved expression
If your goal is human-readable output, the string form plus Graphviz may already be sufficient.
Convert to sympy with a Small Parser
If you want algebraic manipulation, simplification, or code generation, convert the exported form into sympy. The exact parser depends on which function set your model uses, but the general pattern is:
- map
gplearnfunction names tosympyequivalents - parse terminals such as
X0,X1, and constants - recursively build a
sympyexpression tree
Here is a minimal example for a small subset:
A full converter needs to parse the program string rather than build the tree manually, but this mapping is the central idea. Once the expression is in sympy, you can simplify it, pretty-print it, differentiate it, or generate code.
Validate the Converted Expression
Always verify that the converted expression matches the original gplearn model numerically on sample inputs.
This validation step matters because symbolic export bugs are often subtle. A wrong function mapping or argument order can produce a plausible-looking expression that does not actually match the evolved program.
Common Pitfalls
- Assuming
gplearnalready returns a ready-madesympyobject by default. - Building a custom parser without matching every function in the estimator's function set.
- Treating
_programas a guaranteed stable public API without checking library changes. - Skipping numeric validation after conversion and trusting the symbolic form blindly.
Summary
- The learned expression lives on the estimator's best program object, commonly exposed as
est._program. - The simplest exports are the program's string representation and Graphviz tree.
- Converting to
sympyrequires mappinggplearnfunctions and terminals into symbolic equivalents. - Numeric validation is essential after conversion.
- The best export format depends on whether you want readability, visualization, or symbolic manipulation.

