Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] experimental way to access expression methods #430

Open
wants to merge 1 commit into
base: store-options
Choose a base branch
from

Conversation

MilesCranmer
Copy link
Owner

@MilesCranmer MilesCranmer commented Sep 17, 2023

This creates an Expression class that wraps SymbolicRegression.Node. This would make it easier to access various internal methods of SymbolicRegression.

However, this is very experimental and I'm not sure what the best way forward is. There are a few options:

  1. Have this wrapper class and forward methods.
  2. Attach a few methods to PySRRegressor. Cleaner because we don't need the user to pass model and variable_names manually. However I'm worried the number of methods to PySRRegressor could explode.....
  3. Only create a method to convert from string to Node. Then, expect the user to call SymbolicRegression.<method> themselves, using model.sr_options_ when needed.

I think (3) would make the maintenance job the easiest was we wouldn't have to write a Python equivalent for everything..... However, one downside is that the printing functionality for manually-created expressions does not seem to use variable_names. Maybe for that we need to set define_helper_functions=True when creating the Options.

Fixes #339 and probably a few other issues which want an easier way to access backend internals.

Current version is an attempt at (1):

import numpy as np
from pysr import PySRRegressor, Expression

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

model = PySRRegressor(unary_operators=["cos"])
model.fit(X, y)

ex = Expression.from_string("cos(x0)", model=model)
ex.compute_complexity()  # = 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant