-
Notifications
You must be signed in to change notification settings - Fork 283
Open
Labels
bugSomething isn't workingSomething isn't working
Description
What happened?
I have dfined a template expresion and I can't get the sympy expresion of the model. It worked perfectly fine without that, but I wanted to yield expresions of 2 variables but I need a sympy expresion te evaluate the result and plot the function.
Here is my function and an example:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from pysr import PySRRegressor, TemplateExpressionSpec
def symbolic_regression(df, input_cols, target_col, threshold=0.2):
X = df[input_cols].values
y = df[target_col].values
# Split the data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=12
)
if len(input_cols) == 2:
spec = TemplateExpressionSpec(
expressions=["f"],
variable_names=["x0", "x1"],
combine="f(x0, x1)"
)
else:
spec = TemplateExpressionSpec(
expressions=["f"],
variable_names=["x0"],
combine="f(x0)"
)
model = PySRRegressor(
binary_operators=["+", "-", "*", "/", "^"],
unary_operators=["exp", "log", "sqrt"],
model_selection="best",
verbosity=0,
constraints={'^': (-2, 2)},
expression_spec=spec
)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
return model.sympy()
# --------------------------
# Example
# --------------------------
np.random.seed(0)
x = np.random.uniform(-2, 2, 100)
y = np.random.uniform(-2, 2, 100)
z = 3 * x + 2 * y + np.sin(x)
df = pd.DataFrame({
"x": x,
"y": y,
"z": z
})
expr = symbolic_regression(df, input_cols=["x", "y"], target_col="z")
print(expr)
Code example
Version
1.5.6
Operating System
Windows
Package Manager
Other (specify below)
Interface
Script (i.e., python my_script.py
)
Relevant log output
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[1], line 59
52 df = pd.DataFrame({
53 "x": x,
54 "y": y,
55 "z": z
56 })
58
---> 59 expr = symbolic_regression(df, input_cols=["x", "y"], target_col="z")
60 print(expr)
Cell In[1], line 40, in symbolic_regression(df, input_cols, target_col, threshold)
37 model.fit(X_train, y_train)
38 y_pred = model.predict(X_test)
---> 40 return model.sympy()
File c:\Users\alvar\Desktop\UCM\Cuarto Curso\TFG\.venv\Lib\site-packages\pysr\sr.py:2437, in PySRRegressor.sympy(self, index)
2419 """
2420 Return sympy representation of the equation(s) chosen by `model_selection`.
2421
(...)
2434 SymPy representation of the best equation.
2435 """
2436 if not self.expression_spec_.supports_sympy:
-> 2437 raise ValueError(
2438 f"`expression_spec={self.expression_spec_}` does not support sympy export."
2439 )
2440 self.refresh()
2441 best_equation = self.get_best(index=index)
ValueError: `expression_spec=<pysr.expression_specs.TemplateExpressionSpec object at 0x000001CC97F84090>` does not support sympy export.
Extra Info
I'm using poetry created a virtual enviroment so I'm running my script like using a regular enviroment, I'm not doing poetry run something.py
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working