Spaces:
Sleeping
Sleeping
MilesCranmer
commited on
Commit
•
d26d668
1
Parent(s):
faa83d3
Add __repr__ method that lists selected equation
Browse files- TODO.md +3 -0
- example.py +8 -10
- pysr/sklearn.py +27 -3
TODO.md
CHANGED
@@ -65,6 +65,9 @@
|
|
65 |
|
66 |
- [ ] Automatically convert log, log10, log2, pow to the correct operators.
|
67 |
- [ ] I think the simplification isn't working correctly (post-merging SymbolicUtils.)
|
|
|
|
|
|
|
68 |
|
69 |
## Feature ideas
|
70 |
|
|
|
65 |
|
66 |
- [ ] Automatically convert log, log10, log2, pow to the correct operators.
|
67 |
- [ ] I think the simplification isn't working correctly (post-merging SymbolicUtils.)
|
68 |
+
- [ ] Show demo of PySRRegressor. Fit equations, then show how to view equations.
|
69 |
+
- [ ] Add "selected" column string to regular equations dict.
|
70 |
+
- [ ] List "Loss" instead of "MSE"
|
71 |
|
72 |
## Feature ideas
|
73 |
|
example.py
CHANGED
@@ -1,25 +1,23 @@
|
|
1 |
import numpy as np
|
2 |
-
from pysr import
|
3 |
|
4 |
# Dataset
|
5 |
-
X =
|
6 |
-
y =
|
7 |
|
8 |
# Learn equations
|
9 |
-
|
10 |
-
|
11 |
-
y,
|
12 |
-
niterations=5,
|
13 |
binary_operators=["plus", "mult"],
|
14 |
unary_operators=[
|
15 |
"cos",
|
16 |
"exp",
|
17 |
"sin", # Pre-defined library of operators (see https://pysr.readthedocs.io/en/latest/docs/operators/)
|
18 |
-
"inv(x) =
|
19 |
],
|
20 |
loss="loss(x, y) = abs(x - y)", # Custom loss function
|
21 |
) # Define your own operator! (Julia syntax)
|
22 |
|
23 |
-
|
24 |
|
25 |
-
print(
|
|
|
1 |
import numpy as np
|
2 |
+
from pysr import PySRRegressor
|
3 |
|
4 |
# Dataset
|
5 |
+
X = 3 * np.random.randn(100, 5)
|
6 |
+
y = 3 * np.cos(X[:, 3]) + X[:, 0] ** 2 - 2
|
7 |
|
8 |
# Learn equations
|
9 |
+
model = PySRRegressor(
|
10 |
+
niterations=6,
|
|
|
|
|
11 |
binary_operators=["plus", "mult"],
|
12 |
unary_operators=[
|
13 |
"cos",
|
14 |
"exp",
|
15 |
"sin", # Pre-defined library of operators (see https://pysr.readthedocs.io/en/latest/docs/operators/)
|
16 |
+
"inv(x) = 2/x",
|
17 |
],
|
18 |
loss="loss(x, y) = abs(x - y)", # Custom loss function
|
19 |
) # Define your own operator! (Julia syntax)
|
20 |
|
21 |
+
model.fit(X, y)
|
22 |
|
23 |
+
print(model)
|
pysr/sklearn.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
from pysr import pysr, best_row
|
2 |
-
from sklearn.base import BaseEstimator
|
3 |
import inspect
|
|
|
4 |
|
5 |
|
6 |
-
class PySRRegressor(BaseEstimator):
|
7 |
def __init__(self, model_selection="accuracy", **params):
|
8 |
"""Initialize settings for pysr.pysr call.
|
9 |
|
@@ -18,7 +19,30 @@ class PySRRegressor(BaseEstimator):
|
|
18 |
self.equations = None
|
19 |
|
20 |
def __repr__(self):
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def set_params(self, **params):
|
24 |
"""Set parameters for pysr.pysr call or model_selection strategy."""
|
|
|
1 |
from pysr import pysr, best_row
|
2 |
+
from sklearn.base import BaseEstimator, RegressorMixin
|
3 |
import inspect
|
4 |
+
import pandas as pd
|
5 |
|
6 |
|
7 |
+
class PySRRegressor(BaseEstimator, RegressorMixin):
|
8 |
def __init__(self, model_selection="accuracy", **params):
|
9 |
"""Initialize settings for pysr.pysr call.
|
10 |
|
|
|
19 |
self.equations = None
|
20 |
|
21 |
def __repr__(self):
|
22 |
+
if self.equations is None:
|
23 |
+
return "PySRRegressor.equations=None"
|
24 |
+
|
25 |
+
equations = self.equations
|
26 |
+
selected = ["" for _ in range(len(equations))]
|
27 |
+
if self.model_selection == "accuracy":
|
28 |
+
chosen_row = -1
|
29 |
+
elif self.model_selection == "best":
|
30 |
+
chosen_row = equations["score"].idxmax()
|
31 |
+
else:
|
32 |
+
raise NotImplementedError
|
33 |
+
selected[chosen_row] = ">"
|
34 |
+
output = "PySRRegressor.equations=[\n"
|
35 |
+
repr_equations = pd.DataFrame(
|
36 |
+
dict(
|
37 |
+
selected=selected,
|
38 |
+
score=equations["score"],
|
39 |
+
MSE=equations["MSE"],
|
40 |
+
Complexity=equations["Complexity"],
|
41 |
+
)
|
42 |
+
)
|
43 |
+
output += repr_equations.__repr__()
|
44 |
+
output += "\n]"
|
45 |
+
return output
|
46 |
|
47 |
def set_params(self, **params):
|
48 |
"""Set parameters for pysr.pysr call or model_selection strategy."""
|