Bayesian Optimization

3.2.1 · active · verified Sun Apr 12

The bayesian-optimization library provides a Python implementation of the Bayesian Optimization (BO) algorithm, specifically designed for constrained global optimization of expensive black-box functions. It leverages Bayesian inference and Gaussian processes to efficiently find the maximum value of an unknown function with minimal evaluations. The current version is 3.2.1, and the project is actively maintained with a regular release cadence, requiring Python >=3.9.

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up and run a basic Bayesian Optimization to maximize a simple two-dimensional black-box function. It defines the objective function, specifies the search space bounds, initializes the optimizer, and then runs the maximization process for a set number of iterations.

from bayes_opt import BayesianOptimization
import os

def black_box_function(x, y):
    """Function with unknown internals we wish to maximize.
    This is just serving as an example, for all intents and purposes think of
    the internals of this function, i.e.: the process which generates its
    output values, as unknown.
    """
    # Example: A simple 2D quadratic function
    return -x ** 2 - (y - 1) ** 2 + 1

# Bounded region of parameter space
pbounds = {'x': (0, 2), 'y': (0, 3)}

optimizer = BayesianOptimization(
    f=black_box_function,
    pbounds=pbounds,
    random_state=1,
)

# Perform 2 initial random points and 5 iterations of Bayesian Optimization
optimizer.maximize(
    init_points=2,
    n_iter=5,
)

print(f"Best parameters found: {optimizer.max['params']}")
print(f"Maximum value found: {optimizer.max['target']}")

view raw JSON →