8000 [MRG] Skip constant dimensions for plot_objective and plot_evaluations by holgern · Pull Request #888 · scikit-optimize/scikit-optimize · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

[MRG] Skip constant dimensions for plot_objective and plot_evaluations #888

Merged
merged 6 commits into from
Feb 28, 2020
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
3 changes: 2 additions & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ def __call__(self, directory):
# thumbnails for the front page of the scikit-learn home page.
# key: first image in set
# values: (number of plot in set, height of thumbnail)
carousel_thumbs = {'sphx_glr_plot_ask-and-tell_002.png': 600,
carousel_thumbs = {'sphx_glr_sklearn-gridsearchcv-replacement_001.png': 600,
'sphx_glr_plot_ask-and-tell_002.png': 600,
'sphx_glr_bayesian-optimization_004.png': 600,
'sphx_glr_strategy-comparison_002.png': 600,
'sphx_glr_visualizing-results_008.png': 600}
Expand Down
1 change: 1 addition & 0 deletions doc/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ <h4 class="sk-landing-subheader text-dark font-italic mb-3">Sequential model-bas
</div>
<div class="overflow-hidden mx-2 text-center flex-fill">
<a href="auto_examples/sklearn-gridsearchcv-replacement.html" aria-label="sklearn-gridsearchcv-replacement">
<img src="_images/sphx_glr_sklearn-gridsearchcv-replacement_001.png" class="sk-index-img" alt="Scikit-learn hyperparameter search wrapper">
</a>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions doc/whats_new/v0.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Version 0.8.0
- |Enhancement| Add `is_constant` property to dimension and
`n_constant_dimensions` property to Space
:pr:`883` by :user:`Holger Nahrstaedt <holgern>`
- |Enhancement| Skip constant dimensions for plot_objective and
plot_evaluations
to allow plots using BayesSearchCV
:pr:`888` by :user:`Holger Nahrstaedt <holgern>`

:mod:`skopt.utils`
------------------
Expand Down
22 changes: 20 additions & 2 deletions examples/sklearn-gridsearchcv-replacement.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"""
print(__doc__)
import numpy as np
np.random.seed(123)
import matplotlib.pyplot as plt

#############################################################################
# Minimal example
Expand Down Expand Up @@ -79,6 +81,7 @@

from skopt import BayesSearchCV
from skopt.space import Real, Categorical, Integer
from skopt.plots import plot_objective, plot_histogram

from sklearn.datasets import load_digits
from sklearn.svm import LinearSVC, SVC
Expand Down Expand Up @@ -114,14 +117,30 @@

opt = BayesSearchCV(
pipe,
[(svc_search, 20), (linsvc_search, 16)], # (parameter space, # of evaluations)
# (parameter space, # of evaluations)
[(svc_search, 40), (linsvc_search, 16)],
cv=3
)

opt.fit(X_train, y_train)

print("val. score: %s" % opt.best_score_)
print("test score: %s" % opt.score(X_test, y_test))
print("best params: %s" % str(opt.best_params_))

#############################################################################
# Partial Dependence plot 8000 of the objective function for SVC
#
_ = plot_objective(opt.optimizer_results_[0],
dimensions=["C", "degree", "gamma", "kernel"],
n_minimum_search=int(1e8))
plt.show()

#############################################################################
# Plot of the histogram for LinearSVC
#
_ = plot_histogram(opt.optimizer_results_[1], 1)
plt.show()

#############################################################################
# Progress monitoring and control using `callback` argument of `fit` method
Expand Down Expand Up @@ -153,7 +172,6 @@
cv=3
)


# callback handler
def on_step(optim_result):
score = searchcv.best_score_
Expand Down
55 changes: 31 additions & 24 deletions skopt/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ def plot_gaussian_process(res, **kwargs):

if ax is None:
ax = plt.gca()
assert res.space.n_dims == 1, "Space dimension must be 1"
x, x_model = _evenly_sample(res.space.dimensions[0], n_points)
n_dims = res.space.n_dims
assert n_dims == 1, "Space dimension must be 1"
dimension = res.space.dimensions[0]
x, x_model = _evenly_sample(dimension, n_points)
x = x.reshape(-1, 1)
x_model = x_model.reshape(-1, 1)
if res.specs is not None and "args" in res.specs:
Expand Down Expand Up @@ -609,7 +611,8 @@ def plot_objective(result, levels=10, n_points=40, n_samples=250, size=2,
plot_dims : list of str and int, default=None
List of dimension names or dimension indices from the
search-space dimensions to be included in the plot.
If `None` then use all dimensions from the search-space.
If `None` then use all dimensions except constant ones
from the search-space.

sample_source : str or list of floats, default='random'
Defines to samples generation to use for averaging the model function
Expand All @@ -623,31 +626,26 @@ def plot_objective(result, levels=10, n_points=40, n_samples=250, size=2,

Valid strings:

- 'random' - `n_samples` random samples will used

- 'result' - Use only the best observed parameters

- 'expected_minimum' - Parameters that gives the best
minimum Calculated using scipy's minimize method.
This method currently does not work with categorical values.

- 'expected_minimum_random' - Parameters that gives the
best minimum when using naive random sampling.
Works with categorical values.
- 'random' - `n_samples` random samples will used
- 'result' - Use only the best observed parameters
- 'expected_minimum' - Parameters that gives the best
minimum Calculated using scipy's minimize method.
This method currently does not work with categorical values.
- 'expected_minimum_random' - Parameters that gives the
best minimum when using naive random sampling.
Works with categorical values.

minimum : str or list of floats, default = 'result'
Defines the values for the red points in the plots.
Valid strings:

- 'result' - Use best observed parameters

- 'expected_minimum' - Parameters that gives the best
minimum Calculated using scipy's minimize method.
This method currently does not work with categorical values.

- 'expected_minimum_random' - Parameters that gives the
best minimum when using naive random sampling.
Works with categorical values
- 'result' - Use best observed parameters
- 'expected_minimum' - Parameters that gives the best
minimum Calculated using scipy's minimize method.
This method currently does not work with categorical values.
- 'expected_minimum_random' - Parameters that gives the
best minimum when using naive random sampling.
Works with categorical values

n_minimum_search : int, default = None
Determines how many points should be evaluated
Expand All @@ -660,6 +658,7 @@ def plot_objective(result, levels=10, n_points=40, n_samples=250, size=2,
-------
ax : `Matplotlib.Axes`
A 2-d matrix of Axes-objects with the sub-plots.

"""
# Here we define the values for which to plot the red dot (2d plot) and
# the red dotted line (1d plot).
Expand All @@ -672,6 +671,8 @@ def plot_objective(result, levels=10, n_points=40, n_samples=250, size=2,
# Get all dimensions.
plot_dims = []
for row in range(space.n_dims):
if space.dimensions[row].is_constant:
continue
plot_dims.append((row, space.dimensions[row]))
else:
plot_dims = space[plot_dims]
Expand Down Expand Up @@ -771,12 +772,14 @@ def plot_evaluations(result, bins=20, dimensions=None,
plot_dims : list of str and int, default=None
List of dimension names or dimension indices from the
search-space dimensions to be included in the plot.
If `None` then use all dimensions from the search-space.
If `None` then use all dimensions except constant ones
F438 from the search-space.

Returns
-------
ax : `Matplotlib.Axes`
A 2-d matrix of Axes-objects with the sub-plots.

"""
space = result.space
# Convert categoricals to integers, so we can ensure consistent ordering.
Expand All @@ -790,6 +793,8 @@ def plot_evaluations(result, bins=20, dimensions=None,
# Get all dimensions.
plot_dims = []
for row in range(space.n_dims):
if space.dimensions[row].is_constant:
continue
plot_dims.append((row, space.dimensions[row]))
else:
plot_dims = space[plot_dims]
Expand Down Expand Up @@ -852,6 +857,7 @@ def _get_ylim_diagonal(ax):
-------
ylim_diagonal : tuple(int)
The common min and max ylim for the diagonal plots.

"""

# Number of search-space dimensions used in this plot.
Expand Down Expand Up @@ -913,6 +919,7 @@ def partial_dependence_1D(space, model, i, samples,
yi : np.array
The average value of the modelled objective function at
each point `xi`.

"""
# The idea is to step through one dimension, evaluating the model with
# that dimension fixed and averaging either over random values or over
Expand Down
5 changes: 5 additions & 0 deletions skopt/searchcv.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,11 @@ def _make_optimizer(self, params_space):
kwargs = self.optimizer_kwargs_.copy()
kwargs['dimensions'] = dimensions_aslist(params_space)
optimizer = Optimizer(**kwargs)
for i in range(len(optimizer.space.dimensions)):
if optimizer.space.dimensions[i].name is not None:
continue
optimizer.space.dimensions[i].name = list(sorted(
params_space.keys()))[i]

return optimizer

Expand Down
4 changes: 2 additions & 2 deletions skopt/space/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,9 @@ class Integer(Dimension):
Distribution to use when sampling random integers for
this dimension.

- If `"uniform"`, intgers are sampled uniformly between the lower
- If `"uniform"`, integers are sampled uniformly between the lower
and upper bounds.
- If `"log-uniform"`, intgers are sampled uniformly between
- If `"log-uniform"`, integers are sampled uniformly between
`log(lower, base)` and `log(upper, base)` where log
has base `base`.

Expand Down
0