Quickstart with PySRRegressor
masterPySR's main interface follows the scikit-learn style. You can use PySRRegressor to perform symbolic regression by defining binary and unary operators, custom loss functions, and SymPy mappings for the discovered equations.
To use custom operators defined in Julia syntax, provide them in unary_operators and map them to Python/SymPy functions using extra_sympy_mappings.
import numpy as np
from pysr import PySRRegressor
# Generate test data
X = 2 * np.random.randn(100, 5)
y = 2.5382 * np.cos(X[:, 3]) + X[:, 0] ** 2 - 0.5
# Initialize and train the model
model = PySRRegressor(
maxsize=20,
niterations=40,
binary_operators=["+", "*"],
unary_operators=[
"cos",
"exp",
"sin",
"inv(x) = 1/x",
],
extra_sympy_mappings={"inv": lambda x: 1 / x},
elementwise_loss="loss(prediction, target) = (prediction - target)^2",
)
model.fit(X, y)
# Predict using the best equation
predictions = model.predict(X)
# Print learned equations
print(model)