Science Score: 64.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
✓Academic publication links
Links to: arxiv.org -
✓Committers with academic emails
3 of 36 committers (8.3%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (12.4%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
High-Performance Symbolic Regression in Python and Julia
Basic Info
- Host: GitHub
- Owner: MilesCranmer
- License: apache-2.0
- Language: Python
- Default Branch: master
- Homepage: https://ai.damtp.cam.ac.uk/pysr
- Size: 5.38 MB
Statistics
- Stars: 3,040
- Watchers: 34
- Forks: 279
- Open Issues: 108
- Releases: 90
Topics
Metadata Files
README.md
conda: [](https://anaconda.org/conda-forge/pysr)
If you find PySR useful, please cite the paper arXiv:2305.01582. If you've finished a project with PySR, please submit a PR to showcase your work on the research showcase page!
Contents:
Why PySR?
PySR is an open-source tool for Symbolic Regression: a machine learning task where the goal is to find an interpretable symbolic expression that optimizes some objective.
Over a period of several years, PySR has been engineered from the ground up to be (1) as high-performance as possible, (2) as configurable as possible, and (3) easy to use. PySR is developed alongside the Julia library SymbolicRegression.jl, which forms the powerful search engine of PySR. The details of these algorithms are described in the PySR paper.
Symbolic regression works best on low-dimensional datasets, but one can also extend these approaches to higher-dimensional spaces by using "Symbolic Distillation" of Neural Networks, as explained in 2006.11287, where we apply it to N-body problems. Here, one essentially uses symbolic regression to convert a neural net to an analytic equation. Thus, these tools simultaneously present an explicit and powerful way to interpret deep neural networks.
Installation
Pip
You can install PySR with pip:
bash
pip install pysr
Julia dependencies will be installed at first import.
Conda
Similarly, with conda:
bash
conda install -c conda-forge pysr
### Docker
You can also use the `Dockerfile` to install PySR in a docker container 1. Clone this repo. 2. Within the repo's directory, build the docker container: ```bash docker build -t pysr . ``` 3. You can then start the container with an IPython execution with: ```bash docker run -it --rm pysr ipython ``` For more details, see the [docker section](#docker).### Apptainer
If you are using PySR on a cluster where you do not have root access, you can use [Apptainer](https://apptainer.org/) to build a container instead of Docker. The `Apptainer.def` file is analogous to the `Dockerfile`, and can be built with: ```bash apptainer build --notest pysr.sif Apptainer.def ``` and launched with ```bash apptainer run pysr.sif ```### Troubleshooting
One issue you might run into can result in a hard crash at import with a message like "`GLIBCXX_...` not found". This is due to another one of the Python dependencies loading an incorrect `libstdc++` library. To fix this, you should modify your `LD_LIBRARY_PATH` variable to reference the Julia libraries. For example, if the Julia version of `libstdc++.so` is located in `$HOME/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/lib/julia/` (which likely differs on your system!), you could add: ``` export LD_LIBRARY_PATH=$HOME/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/lib/julia/:$LD_LIBRARY_PATH ``` to your `.bashrc` or `.zshrc` file.Quickstart
You might wish to try the interactive tutorial here, which uses the notebook in examples/pysr_demo.ipynb.
In practice, I highly recommend using IPython rather than Jupyter, as the printing is much nicer. Below is a quick demo here which you can paste into a Python runtime. First, let's import numpy to generate some test data:
```python import numpy as np
X = 2 * np.random.randn(100, 5) y = 2.5382 * np.cos(X[:, 3]) + X[:, 0] ** 2 - 0.5 ```
We have created a dataset with 100 datapoints, with 5 features each. The relation we wish to model is $2.5382 \cos(x3) + x0^2 - 0.5$.
Now, let's create a PySR model and train it. PySR's main interface is in the style of scikit-learn:
```python from pysr import PySRRegressor
model = PySRRegressor( maxsize=20, niterations=40, # < Increase me for better results binaryoperators=["+", "*"], unaryoperators=[ "cos", "exp", "sin", "inv(x) = 1/x", # ^ Custom operator (julia syntax) ], extrasympymappings={"inv": lambda x: 1 / x}, # ^ Define operator for SymPy as well elementwise_loss="loss(prediction, target) = (prediction - target)^2", # ^ Custom loss function (julia syntax) ) ```
This will set up the model for 40 iterations of the search code, which contains hundreds of thousands of mutations and equation evaluations.
Let's train this model on our dataset:
python
model.fit(X, y)
Internally, this launches a Julia process which will do a multithreaded search for equations to fit the dataset.
Equations will be printed during training, and once you are satisfied, you may quit early by hitting 'q' and then <enter>.
After the model has been fit, you can run model.predict(X)
to see the predictions on a given dataset using the automatically-selected expression,
or, for example, model.predict(X, 3) to see the predictions of the 3rd equation.
You may run:
python
print(model)
to print the learned equations:
python
PySRRegressor.equations_ = [
pick score equation loss complexity
0 0.000000 4.4324794 42.354317 1
1 1.255691 (x0 * x0) 3.437307 3
2 0.011629 ((x0 * x0) + -0.28087974) 3.358285 5
3 0.897855 ((x0 * x0) + cos(x3)) 1.368308 6
4 0.857018 ((x0 * x0) + (cos(x3) * 2.4566472)) 0.246483 8
5 >>>> inf (((cos(x3) + -0.19699033) * 2.5382123) + (x0 *... 0.000000 10
]
This arrow in the pick column indicates which equation is currently selected by your
model_selection strategy for prediction.
(You may change model_selection after .fit(X, y) as well.)
model.equations_ is a pandas DataFrame containing all equations, including callable format
(lambda_format),
SymPy format (sympy_format - which you can also get with model.sympy()), and even JAX and PyTorch format
(both of which are differentiable - which you can get with model.jax() and model.pytorch()).
Note that PySRRegressor stores the state of the last search, and will restart from where you left off the next time you call .fit(), assuming you have set warm_start=True.
This will cause problems if significant changes are made to the search parameters (like changing the operators). You can run model.reset() to reset the state.
You will notice that PySR will save two files:
hall_of_fame...csv and hall_of_fame...pkl.
The csv file is a list of equations and their losses, and the pkl file is a saved state of the model.
You may load the model from the pkl file with:
python
model = PySRRegressor.from_file("hall_of_fame.2022-08-10_100832.281.pkl")
There are several other useful features such as denoising (e.g., denoise=True),
feature selection (e.g., select_k_features=3).
For examples of these and other features, see the examples page.
For a detailed look at more options, see the options page.
You can also see the full API at this page.
There are also tips for tuning PySR on this page.
Detailed Example
The following code makes use of as many PySR features as possible. Note that is just a demonstration of features and you should not use this example as-is. For details on what each parameter does, check out the API page.
python
model = PySRRegressor(
populations=8,
# ^ Assuming we have 4 cores, this means 2 populations per core, so one is always running.
population_size=50,
# ^ Slightly larger populations, for greater diversity.
ncycles_per_iteration=500,
# ^ Generations between migrations.
niterations=10000000, # Run forever
early_stop_condition=(
"stop_if(loss, complexity) = loss < 1e-6 && complexity < 10"
# Stop early if we find a good and simple equation
),
timeout_in_seconds=60 * 60 * 24,
# ^ Alternatively, stop after 24 hours have passed.
maxsize=50,
# ^ Allow greater complexity.
maxdepth=10,
# ^ But, avoid deep nesting.
binary_operators=["*", "+", "-", "/"],
unary_operators=["square", "cube", "exp", "cos2(x)=cos(x)^2"],
constraints={
"/": (-1, 9),
"square": 9,
"cube": 9,
"exp": 9,
},
# ^ Limit the complexity within each argument.
# "inv": (-1, 9) states that the numerator has no constraint,
# but the denominator has a max complexity of 9.
# "exp": 9 simply states that `exp` can only have
# an expression of complexity 9 as input.
nested_constraints={
"square": {"square": 1, "cube": 1, "exp": 0},
"cube": {"square": 1, "cube": 1, "exp": 0},
"exp": {"square": 1, "cube": 1, "exp": 0},
},
# ^ Nesting constraints on operators. For example,
# "square(exp(x))" is not allowed, since "square": {"exp": 0}.
complexity_of_operators={"/": 2, "exp": 3},
# ^ Custom complexity of particular operators.
complexity_of_constants=2,
# ^ Punish constants more than variables
select_k_features=4,
# ^ Train on only the 4 most important features
progress=True,
# ^ Can set to false if printing to a file.
weight_randomize=0.1,
# ^ Randomize the tree much more frequently
cluster_manager=None,
# ^ Can be set to, e.g., "slurm", to run a slurm
# cluster. Just launch one script from the head node.
precision=64,
# ^ Higher precision calculations.
warm_start=True,
# ^ Start from where left off.
turbo=True,
# ^ Faster evaluation (experimental)
extra_sympy_mappings={"cos2": lambda x: sympy.cos(x)**2},
# extra_torch_mappings={sympy.cos: torch.cos},
# ^ Not needed as cos already defined, but this
# is how you define custom torch operators.
# extra_jax_mappings={sympy.cos: "jnp.cos"},
# ^ For JAX, one passes a string.
)
Docker
You can also test out PySR in Docker, without installing it locally, by running the following command in the root directory of this repo:
bash
docker build -t pysr .
This builds an image called pysr for your system's architecture,
which also contains IPython. You can select a specific version
of Python and Julia with:
bash
docker build -t pysr --build-arg JLVERSION=1.10.0 --build-arg PYVERSION=3.11.6 .
You can then run with this dockerfile using:
bash
docker run -it --rm -v "$PWD:/data" pysr ipython
which will link the current directory to the container's /data directory
and then launch ipython.
If you have issues building for your system's architecture,
you can emulate another architecture by including --platform linux/amd64,
before the build and run commands.
We are eager to welcome new contributors! Check out our contributors guide for tips 🚀. If you have an idea for a new feature, don't hesitate to share it on the issues or discussions page.
Mark Kittisopikul 💻 💡 🚇 📦 📣 👀 🔧 ⚠️ |
T Coxon 🐛 💻 🔌 💡 🚇 🚧 👀 🔧 ⚠️ 📓 |
Dhananjay Ashok 💻 🌍 💡 🚧 ⚠️ |
Johan Blåbäck 🐛 💻 💡 🚧 📣 👀 ⚠️ 📓 |
JuliusMartensen 🐛 💻 📖 🔌 💡 🚇 🚧 📦 📣 👀 🔧 📓 |
ngam 💻 🚇 📦 👀 🔧 ⚠️ |
Christopher Rowley 💻 💡 🚇 📦 👀 |
Kaze Wong 🐛 💻 💡 🚇 🚧 📣 👀 🔬 📓 |
Christopher Rackauckas 🐛 💻 🔌 💡 🚇 📣 👀 🔬 🔧 ⚠️ 📓 |
Patrick Kidger 🐛 💻 📖 🔌 💡 🚧 📣 👀 🔬 🔧 ⚠️ 📓 |
Okon Samuel 🐛 💻 📖 🚧 💡 🚇 👀 ⚠️ 📓 |
William Booth-Clibborn 💻 🌍 📖 📓 🚧 👀 🔧 ⚠️ |
Pablo Lemos 🐛 💡 📣 👀 🔬 📓 |
Jerry Ling 🐛 💻 📖 🌍 💡 📣 👀 📓 |
Charles Fox 🐛 💻 💡 🚧 📣 👀 🔬 📓 |
Johann Brehmer 💻 📖 💡 📣 👀 🔬 ⚠️ 📓 |
Marius Millea 💻 💡 📣 👀 📓 |
Coba 🐛 💻 💡 👀 📓 |
foxtran 💻 💡 🚧 🔧 📓 |
Shah Mahdi Hasan 🐛 💻 👀 📓 |
Pietro Monticone 🐛 📖 💡 |
Mateusz Kubica 📖 💡 |
Jay Wadekar 🐛 💡 📣 🔬 |
Anthony Blaom, PhD 🚇 💡 👀 |
Jgmedina95 🐛 💡 👀 |
Michael Abbott 💻 💡 👀 🔧 |
Oscar Smith 💻 💡 |
Eric Hanson 💡 📣 📓 |
Henrique Becker 💻 💡 👀 |
qwertyjl 🐛 📖 💡 📓 |
Rik Huijzer 💡 🚇 |
Hongyu Wang 💡 📣 🔬 |
Zehao Jin 🔬 📣 |
Tanner Mengel 🔬 📣 |
Arthur Grundner 🔬 📣 |
sjwetzel 🔬 📣 📓 |
Saurav Maheshkar 🔧 |
Owner
- Name: Miles Cranmer
- Login: MilesCranmer
- Kind: user
- Location: Cambridge, UK
- Company: University of Cambridge
- Website: astroautomata.com
- Twitter: MilesCranmer
- Repositories: 219
- Profile: https://github.com/MilesCranmer
Assistant Professor at University of Cambridge. Works on AI for the physical sciences.
Citation (CITATION.md)
# Citing
To cite PySR or SymbolicRegression.jl, please use the following BibTeX entry:
```bibtex
@misc{cranmerInterpretableMachineLearning2023,
title = {Interpretable {Machine} {Learning} for {Science} with {PySR} and {SymbolicRegression}.jl},
url = {http://arxiv.org/abs/2305.01582},
doi = {10.48550/arXiv.2305.01582},
urldate = {2023-07-17},
publisher = {arXiv},
author = {Cranmer, Miles},
month = may,
year = {2023},
note = {arXiv:2305.01582 [astro-ph, physics:physics]},
keywords = {Astrophysics - Instrumentation and Methods for Astrophysics, Computer Science - Machine Learning, Computer Science - Neural and Evolutionary Computing, Computer Science - Symbolic Computation, Physics - Data Analysis, Statistics and Probability},
}
```
To cite symbolic distillation of neural networks, the following BibTeX entry can be used:
```bibtex
@article{cranmerDiscovering2020,
title={Discovering Symbolic Models from Deep Learning with Inductive Biases},
author={Miles Cranmer and Alvaro Sanchez-Gonzalez and Peter Battaglia and Rui Xu and Kyle Cranmer and David Spergel and Shirley Ho},
journal={NeurIPS 2020},
year={2020},
eprint={2006.11287},
archivePrefix={arXiv},
primaryClass={cs.LG}
}
```
GitHub Events
Total
- Create event: 68
- Issues event: 91
- Release event: 17
- Watch event: 573
- Delete event: 40
- Issue comment event: 291
- Push event: 256
- Pull request review event: 16
- Pull request review comment event: 12
- Pull request event: 108
- Fork event: 65
Last Year
- Create event: 68
- Issues event: 91
- Release event: 17
- Watch event: 573
- Delete event: 40
- Issue comment event: 291
- Push event: 256
- Pull request review event: 16
- Pull request review comment event: 12
- Pull request event: 108
- Fork event: 65
Committers
Last synced: 8 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| MilesCranmer | m****r@g****m | 2,193 |
| deepsource-autofix[bot] | 6****] | 56 |
| Dhananjay Ashok | d****9@g****m | 33 |
| dependabot[bot] | 4****] | 25 |
| tttc3 | T****2@l****k | 23 |
| pre-commit-ci[bot] | 6****] | 18 |
| --global | b****n@g****m | 13 |
| Ward K Harold | w****h@g****m | 10 |
| wkharold | w****d@g****m | 7 |
| Johann Brehmer | m****l@j****e | 7 |
| --global | w****n@s****m | 5 |
| Mark Kittisopikul | m****t@g****m | 5 |
| Dilum Aluthge | d****m@a****m | 3 |
| Shah Mahdi Hasan | t****i@g****m | 2 |
| Saurav Maheshkar | s****r@g****m | 2 |
| Igor S. Gerasimov | i****r@y****u | 2 |
| github-actions[bot] | 4****] | 1 |
| Sebastian Heuchler | s****7@s****x | 1 |
| Zehao Jin | 5****n | 1 |
| William Thompson | w****n@o****m | 1 |
| VishalJ99 | 5****9 | 1 |
| Tom Jelen | t****m@j****k | 1 |
| Tanner Mengel | t****l@v****u | 1 |
| Roy Hvaara | r****y@l****o | 1 |
| Raúl Peralta Lozada | r****5@g****m | 1 |
| Miguel Crispim Romao | m****l@m****e | 1 |
| Manuel Morales | 6****a | 1 |
| LionessOfCintra | 9****a | 1 |
| Jay Wadekar | j****2@g****m | 1 |
| Ilya Orson | I****n | 1 |
| and 6 more... | ||
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 4 months ago
All Time
- Total issues: 196
- Total pull requests: 280
- Average time to close issues: 4 months
- Average time to close pull requests: 25 days
- Total issue authors: 107
- Total pull request authors: 38
- Average comments per issue: 3.46
- Average comments per pull request: 1.58
- Merged pull requests: 214
- Bot issues: 0
- Bot pull requests: 92
Past Year
- Issues: 62
- Pull requests: 93
- Average time to close issues: 8 days
- Average time to close pull requests: 13 days
- Issue authors: 39
- Pull request authors: 15
- Average comments per issue: 1.71
- Average comments per pull request: 1.4
- Merged pull requests: 74
- Bot issues: 0
- Bot pull requests: 32
Top Authors
Issue Authors
- MilesCranmer (58)
- jinbohou714 (6)
- tbuckworth (5)
- ibengtsson (5)
- dbl001 (4)
- sirisian (3)
- romanovzky (2)
- JacobZhao (2)
- wkharold (2)
- zhuyi-bjut (2)
- SrGonao (2)
- BrotherHa (2)
- jackeuylov (2)
- GoldenGoldy (2)
- Angelld23 (2)
Pull Request Authors
- MilesCranmer (154)
- dependabot[bot] (53)
- github-actions[bot] (27)
- pre-commit-ci[bot] (20)
- tbuckworth (4)
- deepsource-autofix[bot] (4)
- AndPuQing (2)
- sjwetzel (2)
- IlyaOrson (2)
- tomjelen (2)
- manuel-morales-a (2)
- nerai (2)
- romanovzky (2)
- chris-soelistyo (2)
- hvaara (2)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 4
-
Total downloads:
- pypi 12 last-month
-
Total dependent packages: 0
(may contain duplicates) -
Total dependent repositories: 2
(may contain duplicates) - Total versions: 404
- Total maintainers: 1
proxy.golang.org: github.com/milesCranmer/PySR
- Documentation: https://pkg.go.dev/github.com/milesCranmer/PySR#section-documentation
- License: apache-2.0
-
Latest release: v1.5.9
published 6 months ago
Rankings
proxy.golang.org: github.com/milescranmer/pysr
- Documentation: https://pkg.go.dev/github.com/milescranmer/pysr#section-documentation
- License: apache-2.0
-
Latest release: v1.5.9
published 6 months ago
Rankings
pypi.org: pysr-mcranmer
Simple and efficient symbolic regression
- Homepage: https://github.com/MilesCranmer/pysr
- Documentation: https://pysr-mcranmer.readthedocs.io/
- License: apache-2.0
-
Latest release: 0.0.1
published over 5 years ago
Rankings
Maintainers (1)
conda-forge.org: pysr
PySR is a simple, fast, and parallelized symbolic regression package for Python and Julia, which makes use of an algorithm based on regularized evolution and simulated annealing.
- Homepage: https://github.com/MilesCranmer/PySR
- License: Apache-2.0
-
Latest release: 0.11.9
published about 3 years ago
Rankings
Dependencies
- actions/cache v2 composite
- actions/checkout v2 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- conda-incubator/setup-miniconda v2 composite
- julia-actions/cache v1 composite
- julia-actions/setup-julia v1 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- julia-actions/cache v1 composite
- julia-actions/setup-julia v1 composite
- conda-incubator/setup-miniconda v2 composite
- actions/checkout v3 composite
- actions/checkout v3 composite
- docker/setup-qemu-action v2 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- julia-actions/setup-julia v1 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- julia-actions/cache v1 composite
- julia-actions/setup-julia v1 composite
- actions/checkout v3 composite
- github/codeql-action/analyze v2 composite
- github/codeql-action/autobuild v2 composite
- github/codeql-action/init v2 composite
- actions/checkout v3 composite
- docker/build-push-action v3 composite
- docker/login-action v2 composite
- docker/metadata-action v4 composite
- docker/setup-buildx-action v2 composite
- docker/setup-qemu-action v2 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- pypa/gh-action-pypi-publish release/v1 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- peter-evans/create-pull-request v3 composite
- julia ${JLVERSION}-${BASE_IMAGE} build
- python ${PYVERSION}-${BASE_IMAGE} build
- all-contributors-cli 6.25.1 development
- @babel/runtime 7.22.6
- all-contributors-cli 6.25.1
- ansi-escapes 4.3.2
- ansi-regex 5.0.1
- ansi-styles 4.3.0
- async 3.2.4
- camelcase 5.3.1
- chalk 4.1.2
- chardet 0.7.0
- cli-cursor 3.1.0
- cli-width 3.0.0
- cliui 6.0.0
- color-convert 2.0.1
- color-name 1.1.4
- decamelize 1.2.0
- didyoumean 1.2.2
- emoji-regex 8.0.0
- escape-string-regexp 1.0.5
- external-editor 3.1.0
- figures 3.2.0
- find-up 4.1.0
- get-caller-file 2.0.5
- has-flag 4.0.0
- iconv-lite 0.4.24
- inquirer 7.3.3
- is-fullwidth-code-point 3.0.0
- json-fixer 1.6.15
- locate-path 5.0.0
- lodash 4.17.21
- mimic-fn 2.1.0
- mute-stream 0.0.8
- node-fetch 2.6.12
- onetime 5.1.2
- os-tmpdir 1.0.2
- p-limit 2.3.0
- p-locate 4.1.0
- p-try 2.2.0
- path-exists 4.0.0
- pegjs 0.10.0
- pify 5.0.0
- regenerator-runtime 0.13.11
- require-directory 2.1.1
- require-main-filename 2.0.0
- restore-cursor 3.1.0
- run-async 2.4.1
- rxjs 6.6.7
- safer-buffer 2.1.2
- set-blocking 2.0.0
- signal-exit 3.0.7
- string-width 4.2.3
- strip-ansi 6.0.1
- supports-color 7.2.0
- through 2.3.8
- tmp 0.0.33
- tr46 0.0.3
- tslib 1.14.1
- type-fest 0.21.3
- webidl-conversions 3.0.1
- whatwg-url 5.0.0
- which-module 2.0.1
- wrap-ansi 6.2.0
- y18n 4.0.3
- yargs 15.4.1
- yargs-parser 18.1.3
- docstring_parser *
- mkdocs-autorefs *
- mkdocs-material *
- mkdocstrings *
- click >=7.0.0
- julia >=0.6.0
- numpy *
- pandas >=0.21.0
- scikit_learn >=1.0.0
- setuptools >=50.0.0
- sympy *