File: partial-dependence-plot.py

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 (133 lines) | stat: -rw-r--r-- 4,343 bytes parent folder | download | duplicates (2)
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
"""
========================
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.
"""

print(__doc__)
import numpy as np

from skopt import forest_minimize
from skopt.plots import plot_objective

np.random.seed(123)

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


# 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


#############################################################################
# Optimisation using decision trees
# =================================
# We run forest_minimize on the function
bounds = [
    (-1, 1.0),
] * 3
n_calls = 50

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

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


_ = plot_objective(result, n_points=10)

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

_ = plot_objective(result, n_points=10, minimum='expected_minimum')
#############################################################################
# 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.

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

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

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

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

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

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

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

#############################################################################

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

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

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