A Pythonic microframework for Multi-Armed Bandit algorithms.
Project description
bayesianbandits
Bayesian Multi-Armed Bandits for Python
Problem: Despite having a conceptually simple interface, putting together a multi-armed bandit in Python is a daunting task.
Solution: bayesianbandits is a Python package that provides a simple interface for creating and running Bayesian multi-armed bandits. It is built on top of scikit-learn and scipy, taking advantage of conjugate priors to provide fast and accurate inference.
While the API is still evolving, this library is already being used in production for marketing optimization, dynamic pricing, and other applications. Are you using bayesianbandits in your project? Let us know!
Features
- Simple API:
bayesianbanditsprovides a simple interface - most users will only need to callpullandupdateto get started. - Hybrid bandits with cross-arm learning: Share knowledge across similar arms for faster learning and better sample efficiency.
- Fast:
bayesianbanditsis built on top of already fast scientific Python libraries, but, if installed, will also use SuiteSparse to further speed up matrix operations on sparse matrices. Handling tens or even hundreds of thousands of features in a sparse model is no problem. - sklearn pipeline integration: Use sklearn pipelines and transformers to preprocess data before feeding it into your bandit.
- Adversarial bandits (EXP3A): Robust performance in non-stationary and adversarial environments.
- Flexible: Pick from a variety of policy algorithms, including Thompson sampling, upper confidence bound, and epsilon-greedy. Pick from a variety of prior distributions, including beta, gamma, normal, and normal-inverse-gamma.
- Extensible:
bayesianbanditsprovides simple interfaces for creating custom policies and priors. - Well-tested:
bayesianbanditsis well-tested, with nearly 100% test coverage.
Compatibility
bayesianbandits is tested with Python 3.10, 3.11, 3.12, 3.13, and 3.14 with scikit-learn 1.5.2, 1.6.1, 1.7.2, 1.8.0.
Getting Started
Install this package from PyPI.
pip install -U bayesianbandits
Define a LinearUCB contextual bandit with a normal prior.
import numpy as np
from bayesianbandits import (
Arm,
NormalInverseGammaRegressor,
ContextualAgent,
UpperConfidenceBound,
)
arms = [
Arm(1, learner=NormalInverseGammaRegressor()),
Arm(2, learner=NormalInverseGammaRegressor()),
Arm(3, learner=NormalInverseGammaRegressor()),
Arm(4, learner=NormalInverseGammaRegressor()),
]
policy = UpperConfidenceBound(alpha=0.84)
Instantiate the agent and pull an arm with context.
agent = ContextualAgent(arms, policy)
context = np.array([[1, 0, 0, 0]])
# Can be constructed with sklearn, formulaic, patsy, etc...
# context = formulaic.Formula("1 + article_number").get_model_matrix(data)
# context = sklearn.preprocessing.OneHotEncoder().fit_transform(data)
agent.pull(context)
Update the bandit with the reward.
agent.update(context, np.array([15.0]))
For shared learning across arms with hybrid bandits:
from bayesianbandits import LipschitzContextualAgent, ArmColumnFeaturizer, NormalRegressor
# Single shared learner across all arms
agent = LipschitzContextualAgent(
arms=[Arm(i) for i in range(100)], # 100 arms sharing knowledge
learner=NormalRegressor(),
arm_featurizer=ArmColumnFeaturizer(column_name='article_id'),
policy=ThompsonSampling()
)
That's it! Check out the documentation for more examples.
Project details
Release history Release notifications | RSS feed
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 bayesianbandits-1.2.0.tar.gz.
File metadata
- Download URL: bayesianbandits-1.2.0.tar.gz
- Upload date:
- Size: 2.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a6acde1e05928c942e78907c1f8bc5d94928e3c7b871a81f9c4d86fe797e19f
|
|
| MD5 |
4005d3aec96bdcfed8ee4da4c0a6f94a
|
|
| BLAKE2b-256 |
9c726d7a597b907eb16230dfbfdcd6727ce83d391616d62470c8998731fb16c5
|
File details
Details for the file bayesianbandits-1.2.0-py3-none-any.whl.
File metadata
- Download URL: bayesianbandits-1.2.0-py3-none-any.whl
- Upload date:
- Size: 60.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37050ad39b1da6511b9d0fc195418442d9f4690a5e9a6d3e89e9c4acea22ef03
|
|
| MD5 |
f0ff213f732c97d61d408a5bafb9107b
|
|
| BLAKE2b-256 |
b2d4a4f62b32db2510c8efa4405bdc8a96c6a33a1dc3b117af7fe773af1c5244
|