Registry / ai-ml / scikit-optimize

scikit-optimize

JSON →
library0.10.2pypypi✓ verified 22d ago

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.

pip install scikit-optimize
INSTALL
IMPORT
SIG · SCIKIT-OPTIMIZE
S
scikit-optimize
ai-mlpythonv0.10.2
Install
12.1s avg
Import
3360ms
Disk
374MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.10.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
build_error
glibc
py 3.103.910 runs
installs and imports cleanly · install 12.1s · import 3.360s · 358MB
374MB installed
● package 374MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

gp_minimize
from skopt import gp_minimize
forest_minimize
from skopt import forest_minimize
Optimizer
from skopt import Optimizer
from skopt.optimizer import Optimizer
While 'skopt.optimizer' exists, the top-level import 'from skopt import Optimizer' is the commonly documented and simpler approach for direct use.
BayesSearchCV
from skopt import BayesSearchCV
from skopt.searchcv import BayesSearchCV
BayesSearchCV is exposed directly at the top level of the skopt package for convenience, despite residing within a submodule.

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}")
Debug
Known issues
gotchaDespite being actively developed, `scikit-optimize` has previously been described as 'experimental and under heavy development'. This can imply that API stability, especially between minor versions, might not be as rigid as more mature libraries, potentially leading to breaking changes.
fix
Always pin exact versions (`scikit-optimize==X.Y.Z`) in production environments and review changelogs carefully when upgrading, particularly for minor version bumps.
affects: <=0.10.1
gotchaReproducibility of optimization runs depends on setting the `random_state` parameter consistently across all components that use randomness (e.g., `gp_minimize`, `Optimizer`, base estimators in `BayesSearchCV`). Failing to do so can lead to different results across runs.
fix
Pass an integer to the `random_state` parameter in all relevant functions and classes (e.g., `gp_minimize(..., random_state=42)`).
affects: All versions
gotchaThe library offers two main interfaces: direct minimization functions (e.g., `gp_minimize`) for complete optimization loops, and the `Optimizer` class for an 'ask-and-tell' interface, providing more fine-grained control over the optimization process. Confusing these or misapplying the `ask-and-tell` pattern can lead to incorrect or inefficient optimization loops.
fix
For simple, self-contained optimization, use `gp_minimize` or similar functions. For custom loops, parallel evaluations, or dynamic stopping conditions, use the `Optimizer` class with its `ask()` and `tell()` methods, understanding how to manage the state.
affects: All versions
gotchaAs `scikit-optimize` is built on top of NumPy, SciPy, and Scikit-learn, version incompatibilities with these underlying libraries can occur. Outdated or incompatible versions of these dependencies might cause installation issues, runtime errors, or unexpected behavior.
fix
Ensure that your environment uses compatible versions of `numpy`, `scipy`, and `scikit-learn`. Refer to the `scikit-optimize` documentation or `setup.py` for recommended dependency versions. Using a fresh virtual environment or `conda-forge` for installation often helps manage these dependencies.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'skopt'
The scikit-optimize library is not installed in your current Python environment.
fix
pip install scikit-optimize
NameError: name 'Real' is not defined
The classes for defining search space dimensions (Real, Integer, Categorical) were used without being imported from 'skopt.space'.
fix
from skopt.space import Real, Integer, Categorical

def objective(params):
    # ...
    pass

dimensions = [
    Real(low=0.0, high=1.0, name='x1'),
    Integer(low=1, high=10, name='x2')
]

# res = gp_minimize(objective, dimensions)
TypeError: objective function must return a scalar value.
The objective function passed to scikit-optimize's optimizers (e.g., `gp_minimize`, `forest_minimize`) returned a non-scalar value (e.g., a list, tuple, or array) instead of a single float or integer.
fix
def my_objective_function(params):
    x = params[0]
    y = params[1]
    # Calculate a single scalar loss or metric
    loss_value = (x - 0.5)**2 + (y + 0.2)**2
    return loss_value  # Must return a single scalar
AttributeError: 'numpy.ndarray' object has no attribute 'func_vals'
The `plot_convergence` function expects a list of `OptimizeResult` objects (or objects with a 'func_vals' attribute), but it was passed a raw NumPy array of objective function values.
fix
from skopt import gp_minimize
from skopt.plots import plot_convergence
from skopt.space import Real

def objective_func(x): return x[0]**2

res = gp_minimize(objective_func, [Real(-5.0, 5.0)], n_calls=10)

# Correct: Pass a list containing the OptimizeResult object
plot_convergence([res])

# If comparing multiple results:
# res2 = gp_minimize(objective_func, [Real(-5.0, 5.0)], n_calls=10, random_state=42)
# plot_convergence([res, res2])
Upgrade
Version history
0.10.2latest on PyPI · released Jun 4, 2024
Audit
Dependencies
numpyrequiredCore dependency for numerical operations.
scipyrequiredCore dependency for scientific computing.
scikit-learnrequiredCore dependency, often used for hyperparameter tuning tasks.
matplotliboptionalOptional dependency for plotting optimization results.
Agent activity
24 hits · last 30 days
node
14
OpenAI (training)
1
Resources