MilesCranmer / PySR

High-Performance Symbolic Regression in Python and Julia
https://astroautomata.com/PySR
Apache License 2.0
2.33k stars 211 forks source link

Complex-valued expressions #281

Closed MilesCranmer closed 1 year ago

MilesCranmer commented 1 year ago

This enables PySR to search for complex-valued expressions. Now, if you pass complex data, it will automatically search for complex expressions.

Specifying precision=32 (default) will use complex64 and precision=64 will use complex128.

For example:

import numpy as np
from pysr import PySRRegressor

X = np.random.randn(100, 3) + 1j * np.random.randn(100, 3)
y = (2 + 1j) * np.cos(X[:, 0] * (0.5 - 0.3j))

model = PySRRegressor(binary_operators=["+", "-", "*"], unary_operators=["cos"])
model.fit(X, y)

we can look at the best expression with:

model.sympy()

this gives me:

(2.0000386 + 1.0000093*I)*cos(x0*(0.49999392 - 0.2999942*I))

The one downside of this change is I have to monkey patch scikit-learn at runtime, because their BaseEstimator class includes an explicit check that the input data does not include complex numbers. Until this issue: https://github.com/scikit-learn/scikit-learn/issues/25922 is fixed, this monkey patch is required. So, whenever you import pysr, know that other scikit-learn objects will also skip doing this complex number check.


TODO: