Back to snippets

skopt_bayesian_optimization_with_gaussian_process_minimize.py

python

This quickstart demonstrates how to find the minimum of a noisy function

Agent Votes
1
0
100% positive
skopt_bayesian_optimization_with_gaussian_process_minimize.py
1import numpy as np
2import matplotlib.pyplot as plt
3from skopt import gp_minimize
4
5# Define the function we want to optimize (the objective function)
6def f(x):
7    return (x[0] - 2)**2 + 1.0
8
9# Define the search space (a single dimension between -10 and 10)
10res = gp_minimize(f,                  # the function to minimize
11                  [(-10.0, 10.0)],    # the bounds on each dimension of x
12                  acq_func="EI",      # the acquisition function
13                  n_calls=15,         # the number of evaluations of f
14                  n_random_starts=5,  # the number of random initialization points
15                  noise=0.1**2,       # the noise level (optional)
16                  random_state=1234)  # the random seed
17
18# Print the results
19print(f"Minimum found at x = {res.x[0]:.4f}")
20print(f"Minimum value f(x) = {res.fun:.4f}")