Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/optimization_techniques/grid_search_sk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
GridSearchSk Optimizer Example

This example demonstrates how to use the GridSearchSk optimizer with
loky backend for parallel execution.
"""

import numpy as np
from hyperactive.opt import GridSearchSk
from hyperactive.experiment.toy import Sphere

# Define the optimization problem using a toy sphere function
# The sphere function f(x,y) = x² + y² has its minimum at (0,0)
sphere_experiment = Sphere(n_dim=2)

# Define parameter grid - creates a 9x9 = 81 point grid
param_grid = {
"x0": np.linspace(-2, 2, 9),
"x1": np.linspace(-2, 2, 9),
}

# Grid search with parallel execution using loky backend
grid_search = GridSearchSk(
param_grid=param_grid,
backend="loky", # Use loky backend for parallelization
backend_params={
"n_jobs": -1, # Use all available CPU cores
},
experiment=sphere_experiment,
)

best_params = grid_search.run()
print(f"Best params: {best_params}, Score: {grid_search.best_score_:.6f}")
44 changes: 44 additions & 0 deletions examples/optimization_techniques/random_search_sk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
RandomSearchSk Optimizer Example

This example demonstrates how to use the RandomSearchSk optimizer with
sklearn integration and threading backend for parallel execution.
"""

import numpy as np
from scipy.stats import uniform, randint
from sklearn.datasets import load_iris
from sklearn.svm import SVC
from hyperactive.opt import RandomSearchSk
from hyperactive.experiment.integrations import SklearnCvExperiment

# Load iris dataset and create sklearn experiment
X, y = load_iris(return_X_y=True)
sklearn_experiment = SklearnCvExperiment(
estimator=SVC(),
X=X,
y=y,
)

# Define parameter distributions for random search
# Mix of discrete and continuous distributions
param_distributions = {
"C": uniform(loc=0.1, scale=10), # Continuous uniform distribution
"gamma": ["scale", "auto"] + list(np.logspace(-4, 1, 20)), # Mixed discrete/continuous
"kernel": ["rbf", "poly", "sigmoid"], # Discrete choices
}

# Random search with threading backend
random_search = RandomSearchSk(
param_distributions=param_distributions,
n_iter=30, # Number of random samples
random_state=42, # For reproducible results
backend="threading", # Use threading backend for parallelization
backend_params={
"n_jobs": 2, # Use 2 threads
},
experiment=sklearn_experiment,
)

best_params = random_search.run()
print(f"Best params: {best_params}, Score: {random_search.best_score_:.4f}")
1 change: 1 addition & 0 deletions src/hyperactive/opt/gridsearch/_sk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Grid search optimizer."""

# copyright: hyperactive developers, MIT License (see LICENSE file)

from collections.abc import Sequence
Expand Down
26 changes: 26 additions & 0 deletions src/hyperactive/opt/random_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,32 @@ class RandomSearchSk(BaseOptimizer):
Callable returning a scalar score when invoked with keyword
arguments matching a parameter set.

Example
-------
Random search with different backend configurations:

>>> from hyperactive.opt import RandomSearchSk
>>> from scipy.stats import uniform
>>> param_distributions = {
... "C": uniform(loc=0.1, scale=10),
... "gamma": ["scale", "auto", 0.001, 0.01, 0.1, 1],
... }
>>>
>>> # Sequential execution
>>> random_search = RandomSearchSk(
... param_distributions=param_distributions,
... n_iter=20,
... backend="None",
... )
>>>
>>> # Parallel execution with threading backend
>>> random_search_parallel = RandomSearchSk(
... param_distributions=param_distributions,
... n_iter=20,
... backend="threading",
... backend_params={"n_jobs": 2},
... )

Attributes
----------
best_params_ : dict[str, Any]
Expand Down
3 changes: 2 additions & 1 deletion src/hyperactive/utils/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def _get_parallel_test_fixtures(naming="estimator"):
fixtures.append({"backend": "dask", "backend_params": {"scheduler": "sync"}})

# test ray backend
""" TODO: faster ray test
if _check_soft_dependencies("ray", severity="none"):
import os

Expand All @@ -285,5 +286,5 @@ def _get_parallel_test_fixtures(naming="estimator"):

fixtures = [x for x in fixtures if x["backend"] not in SKIP_FIXTURES]
# remove backends in SKIP_FIXTURES from fixtures

"""
return fixtures
Loading