scikit-optimize (skopt)

0.10.2 · active · verified Sun Apr 12

Scikit-Optimize, often referred to as skopt, is a simple and efficient Python library for sequential model-based optimization. It's designed to minimize expensive and noisy black-box functions, building on top of NumPy, SciPy, and Scikit-Learn. Version 0.10.2 is the current release. The library is under active development, with releases occurring periodically, making it a robust tool for tasks like hyperparameter tuning in machine learning.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `gp_minimize` to find the minimum of a noisy black-box function within a defined search space. It sets up a simple 1D objective function and then applies Gaussian Process-based Bayesian optimization. The `random_state` ensures reproducibility.

import numpy as np
from skopt import gp_minimize

def f(x):
    # An example objective function to minimize
    # In a real scenario, this could be a machine learning model training and evaluation
    return (np.sin(5 * x[0]) * (1 - np.tanh(x[0] ** 2)) * 
            np.random.randn() * 0.1 + (x[0] - 0.5)**2)

# Define the search space: a single dimension from -2.0 to 2.0
space = [(-2.0, 2.0)]

# Perform Bayesian optimization using Gaussian Processes
# n_calls: total number of objective evaluations
# n_random_starts: number of random points to sample before fitting the surrogate model
# random_state: for reproducibility
res = gp_minimize(f, space, n_calls=20, n_random_starts=5, random_state=123)

print(f"Optimal value found: x*={res.x[0]:.4f}, f(x*)={res.fun:.4f}")

view raw JSON →