Spaces:
Running
Running
MilesCranmer
commited on
Commit
•
54b5d8c
1
Parent(s):
fe1cb56
Update start.md
Browse files- docs/start.md +78 -29
docs/start.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
# Getting Started
|
2 |
|
3 |
-
|
4 |
PySR uses both Julia and Python, so you need to have both installed.
|
5 |
|
6 |
Install Julia - see [downloads](https://julialang.org/downloads/), and
|
@@ -16,47 +16,96 @@ python3 -c 'import pysr; pysr.install()'
|
|
16 |
The second line will install and update the required Julia packages, including
|
17 |
`PyCall.jl`.
|
18 |
|
|
|
|
|
|
|
|
|
|
|
19 |
## Quickstart
|
20 |
|
|
|
|
|
21 |
```python
|
22 |
import numpy as np
|
23 |
-
from pysr import pysr, best, get_hof
|
24 |
-
|
25 |
-
# Dataset
|
26 |
-
X = 2*np.random.randn(100, 5)
|
27 |
-
y = 2*np.cos(X[:, 3]) + X[:, 0]**2 - 2
|
28 |
-
|
29 |
-
# Learn equations
|
30 |
-
equations = pysr(X, y, niterations=5,
|
31 |
-
binary_operators=["plus", "mult"],
|
32 |
-
unary_operators=["cos", "exp", "sin"])
|
33 |
-
|
34 |
-
...# (you can use ctl-c to exit early)
|
35 |
|
36 |
-
|
|
|
37 |
```
|
|
|
|
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
|
|
41 |
```python
|
42 |
-
|
43 |
```
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
and cache functions from the symbolic regression backend.
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
```python
|
54 |
-
print(
|
55 |
```
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
- `score` - a metric akin to Occam's razor; you should use this to help select the "true" equation.
|
60 |
-
- `sympy_format` - sympy equation.
|
61 |
-
- `lambda_format` - a lambda function for that equation, that you can pass values through.
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# Getting Started
|
2 |
|
3 |
+
# Installation
|
4 |
PySR uses both Julia and Python, so you need to have both installed.
|
5 |
|
6 |
Install Julia - see [downloads](https://julialang.org/downloads/), and
|
|
|
16 |
The second line will install and update the required Julia packages, including
|
17 |
`PyCall.jl`.
|
18 |
|
19 |
+
|
20 |
+
Most common issues at this stage are solved
|
21 |
+
by [tweaking the Julia package server](https://github.com/MilesCranmer/PySR/issues/27).
|
22 |
+
to use up-to-date packages.
|
23 |
+
|
24 |
## Quickstart
|
25 |
|
26 |
+
Let's create a PySR example. First, let's import
|
27 |
+
numpy to generate some test data:
|
28 |
```python
|
29 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
X = 2 * np.random.randn(100, 5)
|
32 |
+
y = 2.5382 * np.cos(X[:, 3]) + X[:, 0] ** 2 - 0.5
|
33 |
```
|
34 |
+
We have created a dataset with 100 datapoints, with 5 features each.
|
35 |
+
The relation we wish to model is $2.5382 \cos(x_3) + x_0^2 - 0.5$.
|
36 |
|
37 |
+
Now, let's create a PySR model and train it.
|
38 |
+
PySR's main interface is in the style of scikit-learn:
|
39 |
+
```python
|
40 |
+
from pysr import PySRRegressor
|
41 |
+
model = PySRRegressor(
|
42 |
+
niterations=5,
|
43 |
+
populations=8,
|
44 |
+
binary_operators=["+", "*"],
|
45 |
+
unary_operators=[
|
46 |
+
"cos",
|
47 |
+
"exp",
|
48 |
+
"sin",
|
49 |
+
],
|
50 |
+
model_selection="best",
|
51 |
+
)
|
52 |
+
```
|
53 |
+
This will set up the model for 5 iterations of the search code, which contains hundreds of thousands of mutations and equation evaluations.
|
54 |
|
55 |
+
Let's train this model on our dataset:
|
56 |
```python
|
57 |
+
model.fit(X, y)
|
58 |
```
|
59 |
+
Internally, this launches a Julia process which will do a multithreaded search for equations to fit the dataset.
|
60 |
|
61 |
+
Equations will be printed during training, and once you are satisfied, you may
|
62 |
+
quit early by hitting 'q' and then \<enter\>.
|
|
|
63 |
|
64 |
+
After the model has been fit, you can run `model.predict(X)`
|
65 |
+
to see the predictions on a given dataset.
|
66 |
+
|
67 |
+
You may run:
|
68 |
```python
|
69 |
+
print(model)
|
70 |
```
|
71 |
+
to print the learned equations:
|
72 |
+
```python
|
73 |
+
PySRRegressor.equations = [
|
74 |
+
pick score Equation MSE Complexity
|
75 |
+
0 0.000000 3.5082064 2.710828e+01 1
|
76 |
+
1 0.964260 (x0 * x0) 3.940544e+00 3
|
77 |
+
2 0.030096 (-0.47978288 + (x0 * x0)) 3.710349e+00 5
|
78 |
+
3 0.840770 ((x0 * x0) + cos(x3)) 1.600564e+00 6
|
79 |
+
4 0.928380 ((x0 * x0) + (2.5313091 * cos(x3))) 2.499724e-01 8
|
80 |
+
5 >>>> 13.956461 ((-0.49999997 + (x0 * x0)) + (2.5382001 * cos(... 1.885665e-13 10
|
81 |
+
]
|
82 |
+
```
|
83 |
+
This arrow in the `pick` column indicates which equation is currently selected by your
|
84 |
+
`model_selection` strategy for prediction.
|
85 |
+
(You may change `model_selection` after `.fit(X, y)` as well.)
|
86 |
+
|
87 |
+
`model.equations` is a pandas DataFrame containing all equations, including callable format
|
88 |
+
(`lambda_format`),
|
89 |
+
SymPy format (`sympy_format`), and even JAX and PyTorch format
|
90 |
+
(both of which are differentiable).
|
91 |
+
|
92 |
+
There are several other useful features such as denoising (e.g., `denoising=True`),
|
93 |
+
feature selection (e.g., `select_k_features=3`).
|
94 |
+
For a summary of features and options, see [this docs page](https://pysr.readthedocs.io/en/latest/docs/options/).
|
95 |
+
You can see the full API at [this page](https://pysr.readthedocs.io/en/latest/docs/api-documentation/).
|
96 |
+
|
97 |
|
98 |
+
# Docker
|
|
|
|
|
|
|
99 |
|
100 |
+
You can also test out PySR in Docker, without
|
101 |
+
installing it locally, by running the following command in
|
102 |
+
the root directory of this repo:
|
103 |
+
```bash
|
104 |
+
docker build --pull --rm -f "Dockerfile" -t pysr "."
|
105 |
+
```
|
106 |
+
This builds an image called `pysr`. You can then run this with:
|
107 |
+
```bash
|
108 |
+
docker run -it --rm -v "$PWD:/data" pysr ipython
|
109 |
+
```
|
110 |
+
which will link the current directory to the container's `/data` directory
|
111 |
+
and then launch ipython.
|