File: partial-dependence-plot-2D.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 (343 lines) | stat: -rw-r--r-- 9,358 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

.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples\plots\partial-dependence-plot-2D.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_plots_partial-dependence-plot-2D.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_plots_partial-dependence-plot-2D.py:


===========================
Partial Dependence Plots 2D
===========================

Hvass-Labs Dec 2017
Holger Nahrstaedt 2020

.. currentmodule:: skopt

Simple example to show the new 2D plots.

.. GENERATED FROM PYTHON SOURCE LINES 13-27

.. code-block:: Python


    print(__doc__)
    from math import exp

    import numpy as np

    from skopt import gp_minimize
    from skopt.plots import plot_histogram, plot_objective, plot_objective_2D
    from skopt.space import Categorical, Integer, Real
    from skopt.utils import point_asdict

    np.random.seed(123)
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 28-63

.. code-block:: Python


    dim_learning_rate = Real(name='learning_rate', low=1e-6, high=1e-2, prior='log-uniform')
    dim_num_dense_layers = Integer(name='num_dense_layers', low=1, high=5)
    dim_num_dense_nodes = Integer(name='num_dense_nodes', low=5, high=512)
    dim_activation = Categorical(name='activation', categories=['relu', 'sigmoid'])

    dimensions = [
        dim_learning_rate,
        dim_num_dense_layers,
        dim_num_dense_nodes,
        dim_activation,
    ]

    default_parameters = [1e-4, 1, 64, 'relu']


    def model_fitness(x):
        learning_rate, num_dense_layers, num_dense_nodes, activation = x

        fitness = (
            ((exp(learning_rate) - 1.0) * 1000) ** 2
            + (num_dense_layers) ** 2
            + (num_dense_nodes / 100) ** 2
        )

        fitness *= 1.0 + 0.1 * np.random.rand()

        if activation == 'sigmoid':
            fitness += 10

        return fitness


    print(model_fitness(x=default_parameters))





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

 .. code-block:: none

    1.518471835296799




.. GENERATED FROM PYTHON SOURCE LINES 64-76

.. code-block:: Python


    search_result = gp_minimize(
        func=model_fitness,
        dimensions=dimensions,
        n_calls=30,
        x0=default_parameters,
        random_state=123,
    )

    print(search_result.x)
    print(search_result.fun)





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

 .. code-block:: none

    [4.929711467441911e-06, 1, 5, 'relu']
    1.0201137671146678




.. GENERATED FROM PYTHON SOURCE LINES 77-81

.. code-block:: Python


    for fitness, x in sorted(zip(search_result.func_vals, search_result.x_iters)):
        print(fitness, x)





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

 .. code-block:: none

    1.0201137671146678 [4.929711467441911e-06, 1, 5, 'relu']
    1.020825017708778 [5.447929143724181e-06, 1, 5, 'relu']
    1.021667781479872 [0.0001144786138006685, 1, 5, 'relu']
    1.0319553844579283 [3.2015950064095674e-06, 1, 5, 'relu']
    1.0350021584165856 [9.697008005639565e-05, 1, 5, 'relu']
    1.0387852240116218 [3.918631435807059e-06, 1, 5, 'relu']
    1.0558125588274407 [4.826512273477327e-06, 1, 5, 'relu']
    1.0626217337728787 [4.877033239311359e-06, 1, 23, 'relu']
    1.065773703106011 [4.15423808784293e-06, 1, 5, 'relu']
    1.0660997595359294 [1e-06, 1, 5, 'relu']
    1.066913466503469 [0.0001026463063419168, 1, 5, 'relu']
    1.0751293940451896 [1.6006592652941194e-06, 1, 5, 'relu']
    1.087696030687253 [6.1092363610861856e-06, 1, 5, 'relu']
    1.130168257786041 [0.0001280333478556736, 1, 19, 'relu']
    1.1690663250744864 [0.00010510628632493199, 1, 33, 'relu']
    1.4602213686635033 [0.0001, 1, 64, 'relu']
    4.174922707059395 [0.00011226037475857404, 2, 5, 'relu']
    14.337540595777632 [4.961649309025573e-06, 2, 44, 'sigmoid']
    15.811122459303194 [5.768045960755954e-05, 1, 366, 'relu']
    20.75714626376416 [4.6648726500116405e-05, 4, 195, 'relu']
    20.83105097254721 [3.629134387669892e-06, 3, 323, 'relu']
    25.045498550233685 [1.5528231282886148e-05, 3, 380, 'relu']
    25.725698564025883 [0.0010034940899532338, 4, 264, 'relu']
    26.808790139516606 [1e-06, 5, 5, 'relu']
    28.093314338813517 [1e-06, 1, 512, 'relu']
    31.67808942295837 [9.214584006695478e-05, 4, 213, 'sigmoid']
    32.60979725349034 [0.0007109209001237586, 3, 355, 'sigmoid']
    36.436844941374716 [9.52877578124997e-06, 4, 306, 'sigmoid']
    108.24130894769868 [0.01, 1, 5, 'relu']
    117.22558971730295 [0.008953258961145084, 4, 399, 'relu']




.. GENERATED FROM PYTHON SOURCE LINES 82-91

.. code-block:: Python


    space = search_result.space

    print(search_result.x_iters)

    search_space = {name: space[name][1] for name in space.dimension_names}

    print(point_asdict(search_space, default_parameters))





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

 .. code-block:: none

    [[0.0001, 1, 64, 'relu'], [0.0007109209001237586, 3, 355, 'sigmoid'], [9.214584006695478e-05, 4, 213, 'sigmoid'], [3.629134387669892e-06, 3, 323, 'relu'], [9.52877578124997e-06, 4, 306, 'sigmoid'], [5.768045960755954e-05, 1, 366, 'relu'], [1.5528231282886148e-05, 3, 380, 'relu'], [4.6648726500116405e-05, 4, 195, 'relu'], [0.008953258961145084, 4, 399, 'relu'], [4.961649309025573e-06, 2, 44, 'sigmoid'], [0.0010034940899532338, 4, 264, 'relu'], [0.00010510628632493199, 1, 33, 'relu'], [0.0001144786138006685, 1, 5, 'relu'], [0.00011226037475857404, 2, 5, 'relu'], [0.0001280333478556736, 1, 19, 'relu'], [5.447929143724181e-06, 1, 5, 'relu'], [4.929711467441911e-06, 1, 5, 'relu'], [4.826512273477327e-06, 1, 5, 'relu'], [0.0001026463063419168, 1, 5, 'relu'], [1e-06, 1, 5, 'relu'], [6.1092363610861856e-06, 1, 5, 'relu'], [1.6006592652941194e-06, 1, 5, 'relu'], [0.01, 1, 5, 'relu'], [1e-06, 5, 5, 'relu'], [1e-06, 1, 512, 'relu'], [3.918631435807059e-06, 1, 5, 'relu'], [9.697008005639565e-05, 1, 5, 'relu'], [3.2015950064095674e-06, 1, 5, 'relu'], [4.15423808784293e-06, 1, 5, 'relu'], [4.877033239311359e-06, 1, 23, 'relu']]
    OrderedDict([('activation', 0.0001), ('learning_rate', 1), ('num_dense_layers', 64), ('num_dense_nodes', 'relu')])




.. GENERATED FROM PYTHON SOURCE LINES 92-97

.. code-block:: Python

    print("Plotting now ...")

    _ = plot_histogram(result=search_result, dimension_identifier='learning_rate', bins=20)
    plt.show()




.. image-sg:: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-2D_001.png
   :alt: partial dependence plot 2D
   :srcset: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-2D_001.png
   :class: sphx-glr-single-img


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

 .. code-block:: none

    Plotting now ...




.. GENERATED FROM PYTHON SOURCE LINES 98-105

.. code-block:: Python

    _ = plot_objective_2D(
        result=search_result,
        dimension_identifier1='learning_rate',
        dimension_identifier2='num_dense_nodes',
    )
    plt.show()




.. image-sg:: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-2D_002.png
   :alt: partial dependence plot 2D
   :srcset: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-2D_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 106-114

.. code-block:: Python


    _ = plot_objective_2D(
        result=search_result,
        dimension_identifier1='num_dense_layers',
        dimension_identifier2='num_dense_nodes',
    )
    plt.show()




.. image-sg:: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-2D_003.png
   :alt: partial dependence plot 2D
   :srcset: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-2D_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 115-120

.. code-block:: Python


    _ = plot_objective(
        result=search_result, plot_dims=['num_dense_layers', 'num_dense_nodes']
    )
    plt.show()



.. image-sg:: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-2D_004.png
   :alt: partial dependence plot 2D
   :srcset: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-2D_004.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_plots_partial-dependence-plot-2D.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/plots/partial-dependence-plot-2D.ipynb
        :alt: Launch binder
        :width: 150 px

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

      :download:`Download Jupyter notebook: partial-dependence-plot-2D.ipynb <partial-dependence-plot-2D.ipynb>`

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

      :download:`Download Python source code: partial-dependence-plot-2D.py <partial-dependence-plot-2D.py>`


.. only:: html

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

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