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
|
.. 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-with-categorical.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-with-categorical.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-with-categorical.py:
=================================================
Partial Dependence Plots with categorical values
=================================================
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-30
.. code-block:: Python
print(__doc__)
import numpy as np
from skopt.plots import plot_objective
np.random.seed(123)
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
from skopt import gp_minimize
from skopt.plots import plot_objective
from skopt.space import Categorical, Integer
.. GENERATED FROM PYTHON SOURCE LINES 31-34
objective function
==================
Here we define a function that we evaluate.
.. GENERATED FROM PYTHON SOURCE LINES 34-43
.. code-block:: Python
def objective(params):
clf = DecisionTreeClassifier(
**{dim.name: val for dim, val in zip(SPACE, params) if dim.name != 'dummy'}
)
return -np.mean(cross_val_score(clf, *load_breast_cancer(return_X_y=True)))
.. GENERATED FROM PYTHON SOURCE LINES 44-46
Bayesian optimization
=====================
.. GENERATED FROM PYTHON SOURCE LINES 46-59
.. code-block:: Python
SPACE = [
Integer(1, 20, name='max_depth'),
Integer(2, 100, name='min_samples_split'),
Integer(5, 30, name='min_samples_leaf'),
Integer(1, 30, name='max_features'),
Categorical(list('abc'), name='dummy'),
Categorical(['gini', 'entropy'], name='criterion'),
Categorical(list('def'), name='dummy'),
]
result = gp_minimize(objective, SPACE, n_calls=20)
.. GENERATED FROM PYTHON SOURCE LINES 60-67
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 67-70
.. code-block:: Python
_ = plot_objective(result, n_points=10)
.. image-sg:: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-with-categorical_001.png
:alt: partial dependence plot with categorical
:srcset: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-with-categorical_001.png
:class: sphx-glr-single-img
.. GENERATED FROM PYTHON SOURCE LINES 71-77
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 77-80
.. code-block:: Python
_ = plot_objective(result, sample_source='result', n_points=10)
.. image-sg:: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-with-categorical_002.png
:alt: partial dependence plot with categorical
:srcset: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-with-categorical_002.png
:class: sphx-glr-single-img
.. GENERATED FROM PYTHON SOURCE LINES 81-89
Modify the shown minimum
========================
Here we try with setting the other parameters to something other than
"result". When dealing with categorical dimensions we can't use
'expected_minimum'. Therefore we try with "expected_minimum_random"
which is a naive way of finding the minimum of the surrogate by only
using random sampling. `n_minimum_search` sets the number of random samples,
which is used to find the minimum
.. GENERATED FROM PYTHON SOURCE LINES 89-98
.. code-block:: Python
_ = plot_objective(
result,
n_points=10,
sample_source='expected_minimum_random',
minimum='expected_minimum_random',
n_minimum_search=10000,
)
.. image-sg:: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-with-categorical_003.png
:alt: partial dependence plot with categorical
:srcset: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-with-categorical_003.png
:class: sphx-glr-single-img
.. GENERATED FROM PYTHON SOURCE LINES 99-103
Set a minimum location
======================
Lastly we can also define these parameters ourselfs by
parsing a list as the pars argument:
.. GENERATED FROM PYTHON SOURCE LINES 103-110
.. code-block:: Python
_ = plot_objective(
result,
n_points=10,
sample_source=[15, 4, 7, 15, 'b', 'entropy', 'e'],
minimum=[15, 4, 7, 15, 'b', 'entropy', 'e'],
)
.. image-sg:: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-with-categorical_004.png
:alt: partial dependence plot with categorical
:srcset: /auto_examples/plots/images/sphx_glr_partial-dependence-plot-with-categorical_004.png
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-timing
**Total running time of the script:** (0 minutes 8.190 seconds)
.. _sphx_glr_download_auto_examples_plots_partial-dependence-plot-with-categorical.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-with-categorical.ipynb
:alt: Launch binder
:width: 150 px
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: partial-dependence-plot-with-categorical.ipynb <partial-dependence-plot-with-categorical.ipynb>`
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: partial-dependence-plot-with-categorical.py <partial-dependence-plot-with-categorical.py>`
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
|