Spaces:
Sleeping
Sleeping
MilesCranmer
commited on
Commit
•
296ee04
1
Parent(s):
9ab2202
Simplify README example
Browse files
README.md
CHANGED
@@ -89,30 +89,32 @@ If none of these folders contain your Julia binary, then you need to add Julia's
|
|
89 |
|
90 |
Let's create a PySR example. First, let's import
|
91 |
numpy to generate some test data:
|
|
|
92 |
```python
|
93 |
import numpy as np
|
94 |
|
95 |
X = 2 * np.random.randn(100, 5)
|
96 |
y = 2.5382 * np.cos(X[:, 3]) + X[:, 0] ** 2 - 0.5
|
97 |
```
|
|
|
98 |
We have created a dataset with 100 datapoints, with 5 features each.
|
99 |
The relation we wish to model is $2.5382 \cos(x_3) + x_0^2 - 0.5$.
|
100 |
|
101 |
Now, let's create a PySR model and train it.
|
102 |
PySR's main interface is in the style of scikit-learn:
|
|
|
103 |
```python
|
104 |
from pysr import PySRRegressor
|
105 |
|
106 |
model = PySRRegressor(
|
107 |
-
|
108 |
-
niterations=40,
|
109 |
binary_operators=["+", "*"],
|
110 |
unary_operators=[
|
111 |
"cos",
|
112 |
"exp",
|
113 |
"sin",
|
114 |
"inv(x) = 1/x",
|
115 |
-
|
116 |
],
|
117 |
extra_sympy_mappings={"inv": lambda x: 1 / x},
|
118 |
# ^ Define operator for SymPy as well
|
@@ -120,12 +122,15 @@ model = PySRRegressor(
|
|
120 |
# ^ Custom loss function (julia syntax)
|
121 |
)
|
122 |
```
|
|
|
123 |
This will set up the model for 40 iterations of the search code, which contains hundreds of thousands of mutations and equation evaluations.
|
124 |
|
125 |
Let's train this model on our dataset:
|
|
|
126 |
```python
|
127 |
model.fit(X, y)
|
128 |
```
|
|
|
129 |
Internally, this launches a Julia process which will do a multithreaded search for equations to fit the dataset.
|
130 |
|
131 |
Equations will be printed during training, and once you are satisfied, you may
|
@@ -135,10 +140,13 @@ After the model has been fit, you can run `model.predict(X)`
|
|
135 |
to see the predictions on a given dataset.
|
136 |
|
137 |
You may run:
|
|
|
138 |
```python
|
139 |
print(model)
|
140 |
```
|
|
|
141 |
to print the learned equations:
|
|
|
142 |
```python
|
143 |
PySRRegressor.equations_ = [
|
144 |
pick score equation loss complexity
|
@@ -150,6 +158,7 @@ PySRRegressor.equations_ = [
|
|
150 |
5 >>>> inf (((cos(x3) + -0.19699033) * 2.5382123) + (x0 *... 0.000000 10
|
151 |
]
|
152 |
```
|
|
|
153 |
This arrow in the `pick` column indicates which equation is currently selected by your
|
154 |
`model_selection` strategy for prediction.
|
155 |
(You may change `model_selection` after `.fit(X, y)` as well.)
|
@@ -165,6 +174,7 @@ This will cause problems if significant changes are made to the search parameter
|
|
165 |
You will notice that PySR will save two files: `hall_of_fame...csv` and `hall_of_fame...pkl`.
|
166 |
The csv file is a list of equations and their losses, and the pkl file is a saved state of the model.
|
167 |
You may load the model from the `pkl` file with:
|
|
|
168 |
```python
|
169 |
model = PySRRegressor.from_file("hall_of_fame.2022-08-10_100832.281.pkl")
|
170 |
```
|
@@ -254,22 +264,25 @@ model = PySRRegressor(
|
|
254 |
)
|
255 |
```
|
256 |
|
257 |
-
|
258 |
# Docker
|
259 |
|
260 |
You can also test out PySR in Docker, without
|
261 |
installing it locally, by running the following command in
|
262 |
the root directory of this repo:
|
|
|
263 |
```bash
|
264 |
docker build -t pysr .
|
265 |
```
|
|
|
266 |
This builds an image called `pysr` for your system's architecture,
|
267 |
which also contains IPython.
|
268 |
|
269 |
You can then run this with:
|
|
|
270 |
```bash
|
271 |
docker run -it --rm -v "$PWD:/data" pysr ipython
|
272 |
```
|
|
|
273 |
which will link the current directory to the container's `/data` directory
|
274 |
and then launch ipython.
|
275 |
|
|
|
89 |
|
90 |
Let's create a PySR example. First, let's import
|
91 |
numpy to generate some test data:
|
92 |
+
|
93 |
```python
|
94 |
import numpy as np
|
95 |
|
96 |
X = 2 * np.random.randn(100, 5)
|
97 |
y = 2.5382 * np.cos(X[:, 3]) + X[:, 0] ** 2 - 0.5
|
98 |
```
|
99 |
+
|
100 |
We have created a dataset with 100 datapoints, with 5 features each.
|
101 |
The relation we wish to model is $2.5382 \cos(x_3) + x_0^2 - 0.5$.
|
102 |
|
103 |
Now, let's create a PySR model and train it.
|
104 |
PySR's main interface is in the style of scikit-learn:
|
105 |
+
|
106 |
```python
|
107 |
from pysr import PySRRegressor
|
108 |
|
109 |
model = PySRRegressor(
|
110 |
+
niterations=40, # < Increase me for better results
|
|
|
111 |
binary_operators=["+", "*"],
|
112 |
unary_operators=[
|
113 |
"cos",
|
114 |
"exp",
|
115 |
"sin",
|
116 |
"inv(x) = 1/x",
|
117 |
+
# ^ Custom operator (julia syntax)
|
118 |
],
|
119 |
extra_sympy_mappings={"inv": lambda x: 1 / x},
|
120 |
# ^ Define operator for SymPy as well
|
|
|
122 |
# ^ Custom loss function (julia syntax)
|
123 |
)
|
124 |
```
|
125 |
+
|
126 |
This will set up the model for 40 iterations of the search code, which contains hundreds of thousands of mutations and equation evaluations.
|
127 |
|
128 |
Let's train this model on our dataset:
|
129 |
+
|
130 |
```python
|
131 |
model.fit(X, y)
|
132 |
```
|
133 |
+
|
134 |
Internally, this launches a Julia process which will do a multithreaded search for equations to fit the dataset.
|
135 |
|
136 |
Equations will be printed during training, and once you are satisfied, you may
|
|
|
140 |
to see the predictions on a given dataset.
|
141 |
|
142 |
You may run:
|
143 |
+
|
144 |
```python
|
145 |
print(model)
|
146 |
```
|
147 |
+
|
148 |
to print the learned equations:
|
149 |
+
|
150 |
```python
|
151 |
PySRRegressor.equations_ = [
|
152 |
pick score equation loss complexity
|
|
|
158 |
5 >>>> inf (((cos(x3) + -0.19699033) * 2.5382123) + (x0 *... 0.000000 10
|
159 |
]
|
160 |
```
|
161 |
+
|
162 |
This arrow in the `pick` column indicates which equation is currently selected by your
|
163 |
`model_selection` strategy for prediction.
|
164 |
(You may change `model_selection` after `.fit(X, y)` as well.)
|
|
|
174 |
You will notice that PySR will save two files: `hall_of_fame...csv` and `hall_of_fame...pkl`.
|
175 |
The csv file is a list of equations and their losses, and the pkl file is a saved state of the model.
|
176 |
You may load the model from the `pkl` file with:
|
177 |
+
|
178 |
```python
|
179 |
model = PySRRegressor.from_file("hall_of_fame.2022-08-10_100832.281.pkl")
|
180 |
```
|
|
|
264 |
)
|
265 |
```
|
266 |
|
|
|
267 |
# Docker
|
268 |
|
269 |
You can also test out PySR in Docker, without
|
270 |
installing it locally, by running the following command in
|
271 |
the root directory of this repo:
|
272 |
+
|
273 |
```bash
|
274 |
docker build -t pysr .
|
275 |
```
|
276 |
+
|
277 |
This builds an image called `pysr` for your system's architecture,
|
278 |
which also contains IPython.
|
279 |
|
280 |
You can then run this with:
|
281 |
+
|
282 |
```bash
|
283 |
docker run -it --rm -v "$PWD:/data" pysr ipython
|
284 |
```
|
285 |
+
|
286 |
which will link the current directory to the container's `/data` directory
|
287 |
and then launch ipython.
|
288 |
|