.. 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 Click :ref:`here ` 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-23 .. code-block:: default print(__doc__) import numpy as np from math import exp from skopt import gp_minimize from skopt.space import Real, Categorical, Integer from skopt.plots import plot_histogram, plot_objective_2D, plot_objective from skopt.utils import point_asdict np.random.seed(123) import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 24-53 .. code-block:: default 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 Out: .. code-block:: none 1.518471835296799 .. GENERATED FROM PYTHON SOURCE LINES 54-65 .. code-block:: default 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 Out: .. code-block:: none [2.5683326296760544e-06, 1, 5, 'relu'] 1.0117401773345693 .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: default for fitness, x in sorted(zip(search_result.func_vals, search_result.x_iters)): print(fitness, x) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 1.0117401773345693 [2.5683326296760544e-06, 1, 5, 'relu'] 1.02011376627145 [4.9296274178364756e-06, 1, 5, 'relu'] 1.0208250164867194 [5.447818995527194e-06, 1, 5, 'relu'] 1.0216677303833677 [0.00011447839199199741, 1, 5, 'relu'] 1.0319553707990767 [3.1995220781684726e-06, 1, 5, 'relu'] 1.035000761229889 [9.696303738529007e-05, 1, 5, 'relu'] 1.0387852167773188 [3.917740504174571e-06, 1, 5, 'relu'] 1.0558125553699018 [4.826172160300783e-06, 1, 5, 'relu'] 1.0657737857823664 [4.163587744230921e-06, 1, 5, 'relu'] 1.0660997595359294 [1e-06, 1, 5, 'relu'] 1.0669131656352118 [0.00010264491499511595, 1, 5, 'relu'] 1.0751293914133275 [1.5998925006763378e-06, 1, 5, 'relu'] 1.0876960517563532 [6.11082549910025e-06, 1, 5, 'relu'] 1.1301695294147942 [0.0001280379715890307, 1, 19, 'relu'] 1.1690663251629732 [0.00010510628672809584, 1, 33, 'relu'] 1.4602213686635033 [0.0001, 1, 64, 'relu'] 4.174922771740464 [0.00011226065176861382, 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 71-80 .. code-block:: default 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 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.00010510628672809584, 1, 33, 'relu'], [0.00011447839199199741, 1, 5, 'relu'], [0.00011226065176861382, 2, 5, 'relu'], [0.0001280379715890307, 1, 19, 'relu'], [5.447818995527194e-06, 1, 5, 'relu'], [4.9296274178364756e-06, 1, 5, 'relu'], [4.826172160300783e-06, 1, 5, 'relu'], [0.00010264491499511595, 1, 5, 'relu'], [1e-06, 1, 5, 'relu'], [6.11082549910025e-06, 1, 5, 'relu'], [1.5998925006763378e-06, 1, 5, 'relu'], [0.01, 1, 5, 'relu'], [1e-06, 5, 5, 'relu'], [1e-06, 1, 512, 'relu'], [3.917740504174571e-06, 1, 5, 'relu'], [9.696303738529007e-05, 1, 5, 'relu'], [3.1995220781684726e-06, 1, 5, 'relu'], [4.163587744230921e-06, 1, 5, 'relu'], [2.5683326296760544e-06, 1, 5, 'relu']] OrderedDict([('activation', 0.0001), ('learning_rate', 1), ('num_dense_layers', 64), ('num_dense_nodes', 'relu')]) .. GENERATED FROM PYTHON SOURCE LINES 81-87 .. code-block:: default 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 Out: .. code-block:: none Plotting now ... .. GENERATED FROM PYTHON SOURCE LINES 88-93 .. code-block:: default _ = 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 94-100 .. code-block:: default _ = 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 101-106 .. code-block:: default _ = 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 13.353 seconds) **Estimated memory usage:** 9 MB .. _sphx_glr_download_auto_examples_plots_partial-dependence-plot-2D.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-optimize/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-python :download:`Download Python source code: partial-dependence-plot-2D.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: partial-dependence-plot-2D.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_