
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples\sampler\initial-sampling-method-integer.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_sampler_initial-sampling-method-integer.py>`
        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-integer.py:


===================================================
Comparing initial sampling methods on integer space
===================================================

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 a real space
:ref:`sphx_glr_auto_examples_initial_sampling_method.py`

.. GENERATED FROM PYTHON SOURCE LINES 24-35

.. code-block:: Python


    print(__doc__)
    import numpy as np

    np.random.seed(1234)
    import matplotlib.pyplot as plt
    from scipy.spatial.distance import pdist

    from skopt.sampler import Grid, Halton, Hammersly, Lhs, Sobol
    from skopt.space import Space








.. GENERATED FROM PYTHON SOURCE LINES 36-54

.. code-block:: Python



    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], 'bs', markersize=40, alpha=0.5)
        # ax.legend(loc="best", numpoints=1)
        ax.set_xlabel("X1")
        ax.set_xlim([0, 5])
        ax.set_ylabel("X2")
        ax.set_ylim([0, 5])
        plt.title(title)
        ax.grid(True)


    n_samples = 10
    space = Space([(0, 5), (0, 5)])








.. GENERATED FROM PYTHON SOURCE LINES 55-57

Random sampling
---------------

.. GENERATED FROM PYTHON SOURCE LINES 57-65

.. code-block:: Python

    x = space.rvs(n_samples)
    plot_searchspace(x, "Random samples")
    pdist_data = []
    x_label = []
    print("empty fields: %d" % (36 - np.size(np.unique(x, axis=0), 0)))
    pdist_data.append(pdist(x).flatten())
    x_label.append("random")




.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_001.png
   :alt: Random samples
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    empty fields: 27




.. GENERATED FROM PYTHON SOURCE LINES 66-68

Sobol'
------

.. GENERATED FROM PYTHON SOURCE LINES 68-76

.. code-block:: Python


    sobol = Sobol()
    x = sobol.generate(space.dimensions, n_samples)
    plot_searchspace(x, "Sobol'")
    print("empty fields: %d" % (36 - np.size(np.unique(x, axis=0), 0)))
    pdist_data.append(pdist(x).flatten())
    x_label.append("sobol'")




.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_002.png
   :alt: Sobol'
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_002.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    D:\git\scikit-optimize\skopt\sampler\sobol.py:521: UserWarning: The balance properties of Sobol' points require n to be a power of 2. 0 points have been previously generated, then: n=0+10=10. 
      warnings.warn(
    empty fields: 26




.. GENERATED FROM PYTHON SOURCE LINES 77-79

Classic latin hypercube sampling
--------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 79-87

.. code-block:: Python


    lhs = Lhs(lhs_type="classic", criterion=None)
    x = lhs.generate(space.dimensions, n_samples)
    plot_searchspace(x, 'classic LHS')
    print("empty fields: %d" % (36 - np.size(np.unique(x, axis=0), 0)))
    pdist_data.append(pdist(x).flatten())
    x_label.append("lhs")




.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_003.png
   :alt: classic LHS
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_003.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    empty fields: 26




.. GENERATED FROM PYTHON SOURCE LINES 88-90

Centered latin hypercube sampling
---------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 90-98

.. code-block:: Python


    lhs = Lhs(lhs_type="centered", criterion=None)
    x = lhs.generate(space.dimensions, n_samples)
    plot_searchspace(x, 'centered LHS')
    print("empty fields: %d" % (36 - np.size(np.unique(x, axis=0), 0)))
    pdist_data.append(pdist(x).flatten())
    x_label.append("center")




.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_004.png
   :alt: centered LHS
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_004.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    empty fields: 26




.. GENERATED FROM PYTHON SOURCE LINES 99-101

Maximin optimized hypercube sampling
------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 101-109

.. code-block:: Python


    lhs = Lhs(criterion="maximin", iterations=10000)
    x = lhs.generate(space.dimensions, n_samples)
    plot_searchspace(x, 'maximin LHS')
    print("empty fields: %d" % (36 - np.size(np.unique(x, axis=0), 0)))
    pdist_data.append(pdist(x).flatten())
    x_label.append("maximin")




.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_005.png
   :alt: maximin LHS
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_005.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    empty fields: 26




.. GENERATED FROM PYTHON SOURCE LINES 110-112

Correlation optimized hypercube sampling
----------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 112-120

.. code-block:: Python


    lhs = Lhs(criterion="correlation", iterations=10000)
    x = lhs.generate(space.dimensions, n_samples)
    plot_searchspace(x, 'correlation LHS')
    print("empty fields: %d" % (36 - np.size(np.unique(x, axis=0), 0)))
    pdist_data.append(pdist(x).flatten())
    x_label.append("corr")




.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_006.png
   :alt: correlation LHS
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_006.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    empty fields: 26




.. GENERATED FROM PYTHON SOURCE LINES 121-123

Ratio optimized hypercube sampling
----------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 123-131

.. code-block:: Python


    lhs = Lhs(criterion="ratio", iterations=10000)
    x = lhs.generate(space.dimensions, n_samples)
    plot_searchspace(x, 'ratio LHS')
    print("empty fields: %d" % (36 - np.size(np.unique(x, axis=0), 0)))
    pdist_data.append(pdist(x).flatten())
    x_label.append("ratio")




.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_007.png
   :alt: ratio LHS
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_007.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    empty fields: 26




.. GENERATED FROM PYTHON SOURCE LINES 132-134

Halton sampling
---------------

.. GENERATED FROM PYTHON SOURCE LINES 134-142

.. code-block:: Python


    halton = Halton()
    x = halton.generate(space.dimensions, n_samples)
    plot_searchspace(x, 'Halton')
    print("empty fields: %d" % (36 - np.size(np.unique(x, axis=0), 0)))
    pdist_data.append(pdist(x).flatten())
    x_label.append("halton")




.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_008.png
   :alt: Halton
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_008.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    empty fields: 26




.. GENERATED FROM PYTHON SOURCE LINES 143-145

Hammersly sampling
------------------

.. GENERATED FROM PYTHON SOURCE LINES 145-153

.. code-block:: Python


    hammersly = Hammersly()
    x = hammersly.generate(space.dimensions, n_samples)
    plot_searchspace(x, 'Hammersly')
    print("empty fields: %d" % (36 - np.size(np.unique(x, axis=0), 0)))
    pdist_data.append(pdist(x).flatten())
    x_label.append("hammersly")




.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_009.png
   :alt: Hammersly
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_009.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    empty fields: 26




.. GENERATED FROM PYTHON SOURCE LINES 154-156

Grid sampling
-------------

.. GENERATED FROM PYTHON SOURCE LINES 156-164

.. code-block:: Python


    grid = Grid(border="include", use_full_layout=False)
    x = grid.generate(space.dimensions, n_samples)
    plot_searchspace(x, 'Grid')
    print("empty fields: %d" % (36 - np.size(np.unique(x, axis=0), 0)))
    pdist_data.append(pdist(x).flatten())
    x_label.append("grid")




.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_010.png
   :alt: Grid
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_010.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    empty fields: 26




.. GENERATED FROM PYTHON SOURCE LINES 165-171

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

.. GENERATED FROM PYTHON SOURCE LINES 171-178

.. code-block:: Python


    fig, ax = plt.subplots()
    ax.boxplot(pdist_data)
    plt.grid(True)
    plt.ylabel("pdist")
    _ = ax.set_ylim(0, 6)
    _ = ax.set_xticklabels(x_label, rotation=45, fontsize=8)



.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_011.png
   :alt: initial sampling method integer
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method-integer_011.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 5.355 seconds)


.. _sphx_glr_download_auto_examples_sampler_initial-sampling-method-integer.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/holgern/scikit-optimize/master?urlpath=lab/tree/notebooks/auto_examples/sampler/initial-sampling-method-integer.ipynb
        :alt: Launch binder
        :width: 150 px

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: initial-sampling-method-integer.ipynb <initial-sampling-method-integer.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: initial-sampling-method-integer.py <initial-sampling-method-integer.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
