File: partial-dependence-plot.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 (363 lines) | stat: -rw-r--r-- 8,340 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

.. 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.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.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.py:


========================
Partial Dependence Plots
========================

Sigurd Carlsen Feb 2019
Holger Nahrstaedt 2020

.. currentmodule:: skopt

Plot objective now supports optional use of partial dependence as well as
different methods of defining parameter values for dependency plots.

.. GENERATED FROM PYTHON SOURCE LINES 14-23

.. code-block:: Python


    print(__doc__)
    import numpy as np

    from skopt import forest_minimize
    from skopt.plots import plot_objective

    np.random.seed(123)








.. GENERATED FROM PYTHON SOURCE LINES 24-28

Objective function
==================
Plot objective now supports optional use of partial dependence as well as
different methods of defining parameter values for dependency plots

.. GENERATED FROM PYTHON SOURCE LINES 28-38

.. code-block:: Python



    # Here we define a function that we evaluate.
    def funny_func(x):
        s = 0
        for i in range(len(x)):
            s += (x[i] * i) ** 2
        return s









.. GENERATED FROM PYTHON SOURCE LINES 39-42

Optimisation using decision trees
=================================
We run forest_minimize on the function

.. GENERATED FROM PYTHON SOURCE LINES 42-51

.. code-block:: Python

    bounds = [
        (-1, 1.0),
    ] * 3
    n_calls = 50

    result = forest_minimize(
        funny_func, bounds, n_calls=n_calls, base_estimator="ET", random_state=4
    )








.. GENERATED FROM PYTHON SOURCE LINES 52-58

Partial dependence plot
=======================
Here we see an example of using partial dependence. Even when setting
n_points all the way down to 10 from the default of 40, this method is
still very slow. This is because partial dependence calculates 250 extra
predictions for each point on the plots.

.. GENERATED FROM PYTHON SOURCE LINES 58-62

.. code-block:: Python



    _ = plot_objective(result, n_points=10)




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





.. GENERATED FROM PYTHON SOURCE LINES 63-67

It is possible to change the location of the red dot, which normally shows
the position of the found minimum. We can set it 'expected_minimum',
which is the minimum value of the surrogate function, obtained by a
minimum search method.

.. GENERATED FROM PYTHON SOURCE LINES 67-69

.. code-block:: Python


    _ = plot_objective(result, n_points=10, minimum='expected_minimum')



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





.. GENERATED FROM PYTHON SOURCE LINES 70-76

Plot without partial dependence
===============================
Here we plot without partial dependence. We see that it is a lot faster.
Also the values for the other parameters are set to the default "result"
which is the parameter set of the best observed value so far. In the case
of funny_func this is close to 0 for all parameters.

.. GENERATED FROM PYTHON SOURCE LINES 76-79

.. code-block:: Python


    _ = plot_objective(result, sample_source='result', n_points=10)




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





.. GENERATED FROM PYTHON SOURCE LINES 80-86

Modify the shown minimum
========================
Here we try with setting the `minimum` parameters to something other than
"result". First we try with "expected_minimum" which is the set of
parameters that gives the miniumum value of the surrogate function,
using scipys minimum search method.

.. GENERATED FROM PYTHON SOURCE LINES 86-91

.. code-block:: Python


    _ = plot_objective(
        result, n_points=10, sample_source='expected_minimum', minimum='expected_minimum'
    )




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





.. GENERATED FROM PYTHON SOURCE LINES 92-94

"expected_minimum_random" is a naive way of finding the minimum of the
surrogate by only using random sampling:

.. GENERATED FROM PYTHON SOURCE LINES 94-102

.. code-block:: Python


    _ = plot_objective(
        result,
        n_points=10,
        sample_source='expected_minimum_random',
        minimum='expected_minimum_random',
    )




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





.. GENERATED FROM PYTHON SOURCE LINES 103-106

We can also specify how many initial samples are used for the two different
"expected_minimum" methods. We set it to a low value in the next examples
to showcase how it affects the minimum for the two methods.

.. GENERATED FROM PYTHON SOURCE LINES 106-115

.. code-block:: Python


    _ = plot_objective(
        result,
        n_points=10,
        sample_source='expected_minimum_random',
        minimum='expected_minimum_random',
        n_minimum_search=10,
    )




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





.. GENERATED FROM PYTHON SOURCE LINES 116-125

.. code-block:: Python


    _ = plot_objective(
        result,
        n_points=10,
        sample_source="expected_minimum",
        minimum='expected_minimum',
        n_minimum_search=2,
    )




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





.. GENERATED FROM PYTHON SOURCE LINES 126-130

Set a minimum location
======================
Lastly we can also define these parameters ourself by parsing a list
as the minimum argument:

.. GENERATED FROM PYTHON SOURCE LINES 130-134

.. code-block:: Python


    _ = plot_objective(
        result, n_points=10, sample_source=[1, -0.5, 0.5], minimum=[1, -0.5, 0.5]
    )



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






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

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


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

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

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

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

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


.. only:: html

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

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