Spaces:
Running
Running
MilesCranmer
commited on
Commit
•
8cfda07
1
Parent(s):
54f6484
Make Julia optimization level parametrized
Browse files- pysr/sr.py +5 -1
- test/test.py +6 -3
pysr/sr.py
CHANGED
@@ -37,6 +37,7 @@ def pysr(X=None, y=None, weights=None,
|
|
37 |
verbosity=1e9,
|
38 |
maxsize=20,
|
39 |
threads=None, #deprecated
|
|
|
40 |
):
|
41 |
"""Run symbolic regression to fit f(X[i, :]) ~ y[i] for all i.
|
42 |
Note: most default parameters have been tuned over several example
|
@@ -45,6 +46,8 @@ def pysr(X=None, y=None, weights=None,
|
|
45 |
|
46 |
:param X: np.ndarray, 2D array. Rows are examples, columns are features.
|
47 |
:param y: np.ndarray, 1D array. Rows are examples.
|
|
|
|
|
48 |
:param procs: int, Number of processes (=number of populations running).
|
49 |
:param niterations: int, Number of iterations of the algorithm to run. The best
|
50 |
equations are printed, and migrate between populations, at the
|
@@ -87,6 +90,7 @@ def pysr(X=None, y=None, weights=None,
|
|
87 |
:param equation_file: str, Where to save the files (.csv separated by |)
|
88 |
:param test: str, What test to run, if X,y not passed.
|
89 |
:param maxsize: int, Max size of an equation.
|
|
|
90 |
:returns: pd.DataFrame, Results dataframe, giving complexity, MSE, and equations
|
91 |
(as strings).
|
92 |
|
@@ -203,7 +207,7 @@ const weights = convert(Array{Float32, 1}, """f"{weight_str})"
|
|
203 |
|
204 |
|
205 |
command = [
|
206 |
-
'julia -
|
207 |
f'-p {procs}',
|
208 |
f'/tmp/.runfile_{rand_string}.jl',
|
209 |
]
|
|
|
37 |
verbosity=1e9,
|
38 |
maxsize=20,
|
39 |
threads=None, #deprecated
|
40 |
+
julia_optimization=3,
|
41 |
):
|
42 |
"""Run symbolic regression to fit f(X[i, :]) ~ y[i] for all i.
|
43 |
Note: most default parameters have been tuned over several example
|
|
|
46 |
|
47 |
:param X: np.ndarray, 2D array. Rows are examples, columns are features.
|
48 |
:param y: np.ndarray, 1D array. Rows are examples.
|
49 |
+
:param weights: np.ndarray, 1D array. Each row is how to weight the
|
50 |
+
mean-square-error loss on weights.
|
51 |
:param procs: int, Number of processes (=number of populations running).
|
52 |
:param niterations: int, Number of iterations of the algorithm to run. The best
|
53 |
equations are printed, and migrate between populations, at the
|
|
|
90 |
:param equation_file: str, Where to save the files (.csv separated by |)
|
91 |
:param test: str, What test to run, if X,y not passed.
|
92 |
:param maxsize: int, Max size of an equation.
|
93 |
+
:param julia_optimization: int, Optimization level (0, 1, 2, 3)
|
94 |
:returns: pd.DataFrame, Results dataframe, giving complexity, MSE, and equations
|
95 |
(as strings).
|
96 |
|
|
|
207 |
|
208 |
|
209 |
command = [
|
210 |
+
f'julia -O{julia_optimization:d}',
|
211 |
f'-p {procs}',
|
212 |
f'/tmp/.runfile_{rand_string}.jl',
|
213 |
]
|
test/test.py
CHANGED
@@ -1,18 +1,21 @@
|
|
1 |
import numpy as np
|
2 |
from pysr import pysr
|
3 |
X = np.random.randn(100, 5)
|
4 |
-
y = X[:, 0]
|
5 |
|
|
|
|
|
6 |
equations = pysr(X, y,
|
|
|
7 |
niterations=100)
|
8 |
print(equations)
|
9 |
-
# Accuracy assertion
|
10 |
assert equations.iloc[-1]['MSE'] < 1e-10
|
11 |
|
|
|
12 |
y = X[:, 0]**2
|
13 |
equations = pysr(X, y,
|
14 |
unary_operators=["square(x) = x^2"], binary_operators=["plus"],
|
|
|
15 |
niterations=100)
|
16 |
print(equations)
|
17 |
-
# Accuracy assertion
|
18 |
assert equations.iloc[-1]['MSE'] < 1e-10
|
|
|
|
1 |
import numpy as np
|
2 |
from pysr import pysr
|
3 |
X = np.random.randn(100, 5)
|
|
|
4 |
|
5 |
+
# Test 1
|
6 |
+
y = X[:, 0]
|
7 |
equations = pysr(X, y,
|
8 |
+
julia_optimization=0,
|
9 |
niterations=100)
|
10 |
print(equations)
|
|
|
11 |
assert equations.iloc[-1]['MSE'] < 1e-10
|
12 |
|
13 |
+
# Test 2
|
14 |
y = X[:, 0]**2
|
15 |
equations = pysr(X, y,
|
16 |
unary_operators=["square(x) = x^2"], binary_operators=["plus"],
|
17 |
+
julia_optimization=0,
|
18 |
niterations=100)
|
19 |
print(equations)
|
|
|
20 |
assert equations.iloc[-1]['MSE'] < 1e-10
|
21 |
+
|