File: initial-sampling-method.rst

package info (click to toggle)
scikit-optimize 0.10.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,672 kB
  • sloc: python: 10,659; javascript: 438; makefile: 136; sh: 6
file content (447 lines) | stat: -rw-r--r-- 10,444 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447

.. 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.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.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.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`

.. GENERATED FROM PYTHON SOURCE LINES 25-36

.. code-block:: Python


    print(__doc__)
    import numpy as np

    np.random.seed(123)
    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 37-56

.. 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], '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.0, 10.0), (0.0, 15.0)])
    # space.set_transformer("normalize")








.. GENERATED FROM PYTHON SOURCE LINES 57-59

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

.. GENERATED FROM PYTHON SOURCE LINES 59-66

.. code-block:: Python

    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-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_001.png
   :alt: Random samples
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 67-69

Sobol'
------

.. GENERATED FROM PYTHON SOURCE LINES 69-76

.. code-block:: Python


    sobol = Sobol()
    x = sobol.generate(space.dimensions, n_samples)
    plot_searchspace(x, "Sobol'")
    pdist_data.append(pdist(x).flatten())
    x_label.append("sobol'")




.. image-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_002.png
   :alt: Sobol'
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_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(




.. GENERATED FROM PYTHON SOURCE LINES 77-79

Classic Latin hypercube sampling
--------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 79-86

.. code-block:: Python


    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-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_003.png
   :alt: classic LHS
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 87-89

Centered Latin hypercube sampling
---------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 89-96

.. code-block:: Python


    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-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_004.png
   :alt: centered LHS
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_004.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 97-99

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

.. GENERATED FROM PYTHON SOURCE LINES 99-106

.. code-block:: Python


    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-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_005.png
   :alt: maximin LHS
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_005.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 107-109

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

.. GENERATED FROM PYTHON SOURCE LINES 109-116

.. code-block:: Python


    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-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_006.png
   :alt: correlation LHS
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_006.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 117-119

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

.. GENERATED FROM PYTHON SOURCE LINES 119-126

.. code-block:: Python


    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-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_007.png
   :alt: ratio LHS
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_007.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 127-129

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

.. GENERATED FROM PYTHON SOURCE LINES 129-136

.. code-block:: Python


    halton = Halton()
    x = halton.generate(space.dimensions, n_samples)
    plot_searchspace(x, 'Halton')
    pdist_data.append(pdist(x).flatten())
    x_label.append("halton")




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





.. GENERATED FROM PYTHON SOURCE LINES 137-139

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

.. GENERATED FROM PYTHON SOURCE LINES 139-146

.. code-block:: Python


    hammersly = Hammersly()
    x = hammersly.generate(space.dimensions, n_samples)
    plot_searchspace(x, 'Hammersly')
    pdist_data.append(pdist(x).flatten())
    x_label.append("hammersly")




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





.. GENERATED FROM PYTHON SOURCE LINES 147-149

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

.. GENERATED FROM PYTHON SOURCE LINES 149-156

.. code-block:: Python


    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-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_010.png
   :alt: Grid
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_010.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 157-163

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 163-170

.. code-block:: Python


    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-sg:: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_011.png
   :alt: initial sampling method
   :srcset: /auto_examples/sampler/images/sphx_glr_initial-sampling-method_011.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_sampler_initial-sampling-method.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.ipynb
        :alt: Launch binder
        :width: 150 px

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

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

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

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


.. only:: html

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

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