Run scipy.optimize with keyword params, e.g. x0={'x': 1, 'y': 2}, and other QoL improvements
Project description
ez-optimize
Author: Quinn Marsh
GitHub: https://github.com/qthedoc/ez-optimize/
PyPI: https://pypi.org/project/ez-optimize/
ez-optimize is a more intuitive front-end for scipy.optimize that simplifies optimization with features like:
- keyword-based parameter definitions (e.g.,
x0={'x': 1, 'y': 2}) - easy switching between minimization and maximization (
direction='max')
ez-optimize is your Ironman suit for optimization.
Why ez-optimize?
Keyword-Based Optimization (e.g.: x0={'x': 1, 'y': 2})
By default, optimization uses arrays x0=[1, 2]. However sometimes it's more intuitive to use named parameters x0={'x': 1, 'y': 2}. ez-optimize allows you to define parameters as dictionaries. Then under the hood, ez-optimize automatically flattens parameters (and wraps your function) for SciPy while restoring the original structure in results. Keyword-based optimization is especially useful in physical simulations where parameters have meaningful names representing physical quantities.
Switch to Maximize with direction='max'
By default, optimization minimizes the objective function. To maximize, you typically need to write a negated wrapper around your function. With ez-optimize, simply set direction='max' and the library will automatically handle negation under the hood.
Quick Start
Install:
pip install ez-optimize
Full set of examples: examples.ipynb*
*This is currently the main form of documentation.
Example 1: Minimizing with Keyword-Based Parameters
from ez_optimize import minimize
def rosenbrock_2d(x, y, a=1, b=100):
return (a - x)**2 + b * (y - x**2)**2
x0 = {'x': 1.3, 'y': 0.7}
result = minimize(rosenbrock_2d, x0, method='BFGS')
print(f"Optimal x: {result.x}")
print(f"Optimal value: {result.fun}")
Optimal x: {'x': 1.0, 'y': 1.0}
Optimal value: 0.0
Example 2: Keyword-Based Bounds
x0 = {'x': 1.3, 'y': 0.7}
bounds = {'x': (0, 2), 'y': (0, 2)}
result = minimize(rosenbrock_2d, x0, method='SLSQP', bounds=bounds)
Example 3: Maximization
def quadratic(x):
return - (x - 1)**2
result = minimize(quadratic, {'x': 0.}, method='SLSQP', direction='max')
print(f"Optimal x: {result.x}")
print(f"Optimal value: {result.fun}")
Optimal x: {'x': 1.0}
Optimal value: 0.0
The Array in the Room
Lets be honest, there is good reason optimization typically uses arrays and always minimizes... it makes the math simple and efficient. For example, optimizing in a vector space allows the curvature to be represented in a Hessian matrix. However, this isn't always necessary like with black-box functions that have no defined gradient or hessian. In those cases, the convenience of defining keyword-based parameters can outweigh the mathematical perfection of array-based optimization.
Acknowledgments
Inspired by better_optimize by Jesse Grabowski, licensed under MIT.
Contributing
Would love any feedback and contributions! Report bugs, request features, or improve documentation via GitHub issues or pull requests.
Development Setup
- Clone the repo:
git clone https://github.com/qthedoc/ez-optimize.git - Navigate to the project directory:
cd ez-optimize - Create a virtual environment:
python -m venv .venv - Activate the virtual environment:
- On Windows:
.\.venv\Scripts\activate - On macOS/Linux:
source .venv/bin/activate
- On Windows:
- Install the package in editable mode with test dependencies:
pip install -e .[test]
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ez_optimize-0.3.1.tar.gz.
File metadata
- Download URL: ez_optimize-0.3.1.tar.gz
- Upload date:
- Size: 202.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
167ea0ec453e3befb685c50ad90bca9c584f5e6e46705953f41657a4b11acf2d
|
|
| MD5 |
a6ee949652d941044950032ae4d3bdd7
|
|
| BLAKE2b-256 |
3bb0769f102e02b4246a43f65f89a9002ab1448c3116ec203aebfd73d2341765
|
Provenance
The following attestation bundles were made for ez_optimize-0.3.1.tar.gz:
Publisher:
release-please.yml on qthedoc/ez-optimize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ez_optimize-0.3.1.tar.gz -
Subject digest:
167ea0ec453e3befb685c50ad90bca9c584f5e6e46705953f41657a4b11acf2d - Sigstore transparency entry: 955823547
- Sigstore integration time:
-
Permalink:
qthedoc/ez-optimize@ad6f7d6d68e4656c3903d428dd47fea112bd80a3 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/qthedoc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-please.yml@ad6f7d6d68e4656c3903d428dd47fea112bd80a3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ez_optimize-0.3.1-py3-none-any.whl.
File metadata
- Download URL: ez_optimize-0.3.1-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2ce04bdf4836db386bc289981a9b68a272e122761c193d4fed44cd08deab852
|
|
| MD5 |
3614f0f0654e9acdb4be3fe071fac73a
|
|
| BLAKE2b-256 |
edb4766b908cb949ccdaed7d8be042d7c0ad2c627cbd1a57eaac5a1b358bd862
|
Provenance
The following attestation bundles were made for ez_optimize-0.3.1-py3-none-any.whl:
Publisher:
release-please.yml on qthedoc/ez-optimize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ez_optimize-0.3.1-py3-none-any.whl -
Subject digest:
d2ce04bdf4836db386bc289981a9b68a272e122761c193d4fed44cd08deab852 - Sigstore transparency entry: 955823563
- Sigstore integration time:
-
Permalink:
qthedoc/ez-optimize@ad6f7d6d68e4656c3903d428dd47fea112bd80a3 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/qthedoc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-please.yml@ad6f7d6d68e4656c3903d428dd47fea112bd80a3 -
Trigger Event:
push
-
Statement type: