.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code or to run this example in your browser via Binder
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_examples_sampler_initial-sampling-method.py:
==================================
Comparing initial sampling methods
==================================
Holger Nahrstaedt 2020 Sigurd Carlsen October 2019
.. currentmodule:: skopt
When doing baysian optimization we often want to reserve some of the
early part of the optimization to pure exploration. By default the
optimizer suggests purely random samples for the first n_initial_points
(10 by default). The downside to this is that there is no guarantee that
these samples are spread out evenly across all the dimensions.
Sampling methods as Latin hypercube, Sobol, Halton and Hammersly
take advantage of the fact that we know beforehand how many random
points we want to sample. Then these points can be "spread out" in
such a way that each dimension is explored.
See also the example on an integer space
:ref:`sphx_glr_auto_examples_initial_sampling_method_integer.py`
.. code-block:: default
print(__doc__)
import numpy as np
np.random.seed(123)
import matplotlib.pyplot as plt
from skopt.space import Space
from skopt.sampler import Sobol
from skopt.sampler import Lhs
from skopt.sampler import Halton
from skopt.sampler import Hammersly
from skopt.sampler import Grid
from scipy.spatial.distance import pdist
.. code-block:: default
def plot_searchspace(x, title):
fig, ax = plt.subplots()
plt.plot(np.array(x)[:, 0], np.array(x)[:, 1], 'bo', label='samples')
plt.plot(np.array(x)[:, 0], np.array(x)[:, 1], 'bo', markersize=80, alpha=0.5)
# ax.legend(loc="best", numpoints=1)
ax.set_xlabel("X1")
ax.set_xlim([-5, 10])
ax.set_ylabel("X2")
ax.set_ylim([0, 15])
plt.title(title)
n_samples = 10
space = Space([(-5., 10.), (0., 15.)])
# space.set_transformer("normalize")
Random sampling
---------------
.. code-block:: default
x = space.rvs(n_samples)
plot_searchspace(x, "Random samples")
pdist_data = []
x_label = []
pdist_data.append(pdist(x).flatten())
x_label.append("random")
.. image:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_001.png
:alt: Random samples
:class: sphx-glr-single-img
Sobol
-----
.. code-block:: default
sobol = Sobol()
x = sobol.generate(space.dimensions, n_samples)
plot_searchspace(x, 'Sobol')
pdist_data.append(pdist(x).flatten())
x_label.append("sobol")
.. image:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_002.png
:alt: Sobol
:class: sphx-glr-single-img
Classic Latin hypercube sampling
--------------------------------
.. code-block:: default
lhs = Lhs(lhs_type="classic", criterion=None)
x = lhs.generate(space.dimensions, n_samples)
plot_searchspace(x, 'classic LHS')
pdist_data.append(pdist(x).flatten())
x_label.append("lhs")
.. image:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_003.png
:alt: classic LHS
:class: sphx-glr-single-img
Centered Latin hypercube sampling
---------------------------------
.. code-block:: default
lhs = Lhs(lhs_type="centered", criterion=None)
x = lhs.generate(space.dimensions, n_samples)
plot_searchspace(x, 'centered LHS')
pdist_data.append(pdist(x).flatten())
x_label.append("center")
.. image:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_004.png
:alt: centered LHS
:class: sphx-glr-single-img
Maximin optimized hypercube sampling
------------------------------------
.. code-block:: default
lhs = Lhs(criterion="maximin", iterations=10000)
x = lhs.generate(space.dimensions, n_samples)
plot_searchspace(x, 'maximin LHS')
pdist_data.append(pdist(x).flatten())
x_label.append("maximin")
.. image:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_005.png
:alt: maximin LHS
:class: sphx-glr-single-img
Correlation optimized hypercube sampling
----------------------------------------
.. code-block:: default
lhs = Lhs(criterion="correlation", iterations=10000)
x = lhs.generate(space.dimensions, n_samples)
plot_searchspace(x, 'correlation LHS')
pdist_data.append(pdist(x).flatten())
x_label.append("corr")
.. image:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_006.png
:alt: correlation LHS
:class: sphx-glr-single-img
Ratio optimized hypercube sampling
----------------------------------
.. code-block:: default
lhs = Lhs(criterion="ratio", iterations=10000)
x = lhs.generate(space.dimensions, n_samples)
plot_searchspace(x, 'ratio LHS')
pdist_data.append(pdist(x).flatten())
x_label.append("ratio")
.. image:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_007.png
:alt: ratio LHS
:class: sphx-glr-single-img
Halton sampling
---------------
.. code-block:: default
halton = Halton()
x = halton.generate(space.dimensions, n_samples)
plot_searchspace(x, 'Halton')
pdist_data.append(pdist(x).flatten())
x_label.append("halton")
.. image:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_008.png
:alt: Halton
:class: sphx-glr-single-img
Hammersly sampling
------------------
.. code-block:: default
hammersly = Hammersly()
x = hammersly.generate(space.dimensions, n_samples)
plot_searchspace(x, 'Hammersly')
pdist_data.append(pdist(x).flatten())
x_label.append("hammersly")
.. image:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_009.png
:alt: Hammersly
:class: sphx-glr-single-img
Grid sampling
-------------
.. code-block:: default
grid = Grid(border="include", use_full_layout=False)
x = grid.generate(space.dimensions, n_samples)
plot_searchspace(x, 'Grid')
pdist_data.append(pdist(x).flatten())
x_label.append("grid")
.. image:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_010.png
:alt: Grid
:class: sphx-glr-single-img
Pdist boxplot of all methods
----------------------------
This boxplot shows the distance between all generated points using
Euclidian distance. The higher the value, the better the sampling method.
It can be seen that random has the worst performance
.. code-block:: default
fig, ax = plt.subplots()
ax.boxplot(pdist_data)
plt.grid(True)
plt.ylabel("pdist")
_ = ax.set_ylim(0, 12)
_ = ax.set_xticklabels(x_label, rotation=45, fontsize=8)
.. image:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_011.png
:alt: initial sampling method
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 9.183 seconds)
**Estimated memory usage:** 8 MB
.. _sphx_glr_download_auto_examples_sampler_initial-sampling-method.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: binder-badge
.. image:: /../../miniconda/envs/testenv/lib/python3.8/site-packages/sphinx_gallery/_static/binder_badge_logo.svg
:target: https://mybinder.org/v2/gh/scikit-optimize/scikit-optimize/master?urlpath=lab/tree/notebooks/auto_examples/sampler/initial-sampling-method.ipynb
:width: 150 px
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: initial-sampling-method.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: initial-sampling-method.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_