.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/miscellaneous/plot_partial_dependence_visualization_api.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_miscellaneous_plot_partial_dependence_visualization_api.py>`
        to download the full example code or to run this example in your browser via JupyterLite or Binder

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_miscellaneous_plot_partial_dependence_visualization_api.py:


=========================================
Advanced Plotting With Partial Dependence
=========================================
The :class:`~sklearn.inspection.PartialDependenceDisplay` object can be used
for plotting without needing to recalculate the partial dependence. In this
example, we show how to plot partial dependence plots and how to quickly
customize the plot with the visualization API.

.. note::

    See also :ref:`sphx_glr_auto_examples_miscellaneous_plot_roc_curve_visualization_api.py`

.. GENERATED FROM PYTHON SOURCE LINES 15-26

.. code-block:: default


    import matplotlib.pyplot as plt
    import pandas as pd

    from sklearn.datasets import load_diabetes
    from sklearn.inspection import PartialDependenceDisplay
    from sklearn.neural_network import MLPRegressor
    from sklearn.pipeline import make_pipeline
    from sklearn.preprocessing import StandardScaler
    from sklearn.tree import DecisionTreeRegressor








.. GENERATED FROM PYTHON SOURCE LINES 27-32

Train models on the diabetes dataset
================================================

First, we train a decision tree and a multi-layer perceptron on the diabetes
dataset.

.. GENERATED FROM PYTHON SOURCE LINES 32-45

.. code-block:: default


    diabetes = load_diabetes()
    X = pd.DataFrame(diabetes.data, columns=diabetes.feature_names)
    y = diabetes.target

    tree = DecisionTreeRegressor()
    mlp = make_pipeline(
        StandardScaler(),
        MLPRegressor(hidden_layer_sizes=(100, 100), tol=1e-2, max_iter=500, random_state=0),
    )
    tree.fit(X, y)
    mlp.fit(X, y)






.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <style>#sk-container-id-38 {color: black;}#sk-container-id-38 pre{padding: 0;}#sk-container-id-38 div.sk-toggleable {background-color: white;}#sk-container-id-38 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-38 label.sk-toggleable__label-arrow:before {content: "▸";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-38 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-38 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-38 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-38 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-38 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-38 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: "▾";}#sk-container-id-38 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-38 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-38 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-38 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-38 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-38 div.sk-parallel-item::after {content: "";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-38 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-38 div.sk-serial::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-38 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-38 div.sk-item {position: relative;z-index: 1;}#sk-container-id-38 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-38 div.sk-item::before, #sk-container-id-38 div.sk-parallel-item::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-38 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-38 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-38 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-38 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-38 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-38 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-38 div.sk-label-container {text-align: center;}#sk-container-id-38 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-38 div.sk-text-repr-fallback {display: none;}</style><div id="sk-container-id-38" class="sk-top-container"><div class="sk-text-repr-fallback"><pre>Pipeline(steps=[(&#x27;standardscaler&#x27;, StandardScaler()),
                    (&#x27;mlpregressor&#x27;,
                     MLPRegressor(hidden_layer_sizes=(100, 100), max_iter=500,
                                  random_state=0, tol=0.01))])</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class="sk-container" hidden><div class="sk-item sk-dashed-wrapped"><div class="sk-label-container"><div class="sk-label sk-toggleable"><input class="sk-toggleable__control sk-hidden--visually" id="sk-estimator-id-169" type="checkbox" ><label for="sk-estimator-id-169" class="sk-toggleable__label sk-toggleable__label-arrow">Pipeline</label><div class="sk-toggleable__content"><pre>Pipeline(steps=[(&#x27;standardscaler&#x27;, StandardScaler()),
                    (&#x27;mlpregressor&#x27;,
                     MLPRegressor(hidden_layer_sizes=(100, 100), max_iter=500,
                                  random_state=0, tol=0.01))])</pre></div></div></div><div class="sk-serial"><div class="sk-item"><div class="sk-estimator sk-toggleable"><input class="sk-toggleable__control sk-hidden--visually" id="sk-estimator-id-170" type="checkbox" ><label for="sk-estimator-id-170" class="sk-toggleable__label sk-toggleable__label-arrow">StandardScaler</label><div class="sk-toggleable__content"><pre>StandardScaler()</pre></div></div></div><div class="sk-item"><div class="sk-estimator sk-toggleable"><input class="sk-toggleable__control sk-hidden--visually" id="sk-estimator-id-171" type="checkbox" ><label for="sk-estimator-id-171" class="sk-toggleable__label sk-toggleable__label-arrow">MLPRegressor</label><div class="sk-toggleable__content"><pre>MLPRegressor(hidden_layer_sizes=(100, 100), max_iter=500, random_state=0,
                 tol=0.01)</pre></div></div></div></div></div></div></div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 46-54

Plotting partial dependence for two features
============================================

We plot partial dependence curves for features "age" and "bmi" (body mass
index) for the decision tree. With two features,
:func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator` expects to plot
two curves. Here the plot function place a grid of two plots using the space
defined by `ax` .

.. GENERATED FROM PYTHON SOURCE LINES 54-58

.. code-block:: default

    fig, ax = plt.subplots(figsize=(12, 6))
    ax.set_title("Decision Tree")
    tree_disp = PartialDependenceDisplay.from_estimator(tree, X, ["age", "bmi"], ax=ax)




.. image-sg:: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_001.png
   :alt: Decision Tree
   :srcset: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 59-63

The partial dependence curves can be plotted for the multi-layer perceptron.
In this case, `line_kw` is passed to
:func:`~sklearn.inspection.PartialDependenceDisplay.from_estimator` to change the
color of the curve.

.. GENERATED FROM PYTHON SOURCE LINES 63-69

.. code-block:: default

    fig, ax = plt.subplots(figsize=(12, 6))
    ax.set_title("Multi-layer Perceptron")
    mlp_disp = PartialDependenceDisplay.from_estimator(
        mlp, X, ["age", "bmi"], ax=ax, line_kw={"color": "red"}
    )




.. image-sg:: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_002.png
   :alt: Multi-layer Perceptron
   :srcset: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 70-87

Plotting partial dependence of the two models together
======================================================

The `tree_disp` and `mlp_disp`
:class:`~sklearn.inspection.PartialDependenceDisplay` objects contain all the
computed information needed to recreate the partial dependence curves. This
means we can easily create additional plots without needing to recompute the
curves.

One way to plot the curves is to place them in the same figure, with the
curves of each model on each row. First, we create a figure with two axes
within two rows and one column. The two axes are passed to the
:func:`~sklearn.inspection.PartialDependenceDisplay.plot` functions of
`tree_disp` and `mlp_disp`. The given axes will be used by the plotting
function to draw the partial dependence. The resulting plot places the
decision tree partial dependence curves in the first row of the
multi-layer perceptron in the second row.

.. GENERATED FROM PYTHON SOURCE LINES 87-94

.. code-block:: default


    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10))
    tree_disp.plot(ax=ax1)
    ax1.set_title("Decision Tree")
    mlp_disp.plot(ax=ax2, line_kw={"color": "red"})
    ax2.set_title("Multi-layer Perceptron")




.. image-sg:: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_003.png
   :alt: Decision Tree, Multi-layer Perceptron
   :srcset: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_003.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    Text(0.5, 1.0, 'Multi-layer Perceptron')



.. GENERATED FROM PYTHON SOURCE LINES 95-100

Another way to compare the curves is to plot them on top of each other. Here,
we create a figure with one row and two columns. The axes are passed into the
:func:`~sklearn.inspection.PartialDependenceDisplay.plot` function as a list,
which will plot the partial dependence curves of each model on the same axes.
The length of the axes list must be equal to the number of plots drawn.

.. GENERATED FROM PYTHON SOURCE LINES 100-109

.. code-block:: default


    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))
    tree_disp.plot(ax=[ax1, ax2], line_kw={"label": "Decision Tree"})
    mlp_disp.plot(
        ax=[ax1, ax2], line_kw={"label": "Multi-layer Perceptron", "color": "red"}
    )
    ax1.legend()
    ax2.legend()




.. image-sg:: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_004.png
   :alt: plot partial dependence visualization api
   :srcset: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_004.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    <matplotlib.legend.Legend object at 0x7f44c91b65e0>



.. GENERATED FROM PYTHON SOURCE LINES 111-117

`tree_disp.axes_` is a numpy array container the axes used to draw the
partial dependence plots. This can be passed to `mlp_disp` to have the same
affect of drawing the plots on top of each other. Furthermore, the
`mlp_disp.figure_` stores the figure, which allows for resizing the figure
after calling `plot`. In this case `tree_disp.axes_` has two dimensions, thus
`plot` will only show the y label and y ticks on the left most plot.

.. GENERATED FROM PYTHON SOURCE LINES 117-127

.. code-block:: default


    tree_disp.plot(line_kw={"label": "Decision Tree"})
    mlp_disp.plot(
        line_kw={"label": "Multi-layer Perceptron", "color": "red"}, ax=tree_disp.axes_
    )
    tree_disp.figure_.set_size_inches(10, 6)
    tree_disp.axes_[0, 0].legend()
    tree_disp.axes_[0, 1].legend()
    plt.show()




.. image-sg:: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_005.png
   :alt: plot partial dependence visualization api
   :srcset: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_005.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 128-134

Plotting partial dependence for one feature
===========================================

Here, we plot the partial dependence curves for a single feature, "age", on
the same axes. In this case, `tree_disp.axes_` is passed into the second
plot function.

.. GENERATED FROM PYTHON SOURCE LINES 134-138

.. code-block:: default

    tree_disp = PartialDependenceDisplay.from_estimator(tree, X, ["age"])
    mlp_disp = PartialDependenceDisplay.from_estimator(
        mlp, X, ["age"], ax=tree_disp.axes_, line_kw={"color": "red"}
    )



.. image-sg:: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_006.png
   :alt: plot partial dependence visualization api
   :srcset: /auto_examples/miscellaneous/images/sphx_glr_plot_partial_dependence_visualization_api_006.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_miscellaneous_plot_partial_dependence_visualization_api.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/scikit-learn/scikit-learn/1.3.X?urlpath=lab/tree/notebooks/auto_examples/miscellaneous/plot_partial_dependence_visualization_api.ipynb
        :alt: Launch binder
        :width: 150 px



    .. container:: lite-badge

      .. image:: images/jupyterlite_badge_logo.svg
        :target: ../../lite/lab/?path=auto_examples/miscellaneous/plot_partial_dependence_visualization_api.ipynb
        :alt: Launch JupyterLite
        :width: 150 px

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

      :download:`Download Python source code: plot_partial_dependence_visualization_api.py <plot_partial_dependence_visualization_api.py>`

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

      :download:`Download Jupyter notebook: plot_partial_dependence_visualization_api.ipynb <plot_partial_dependence_visualization_api.ipynb>`


.. only:: html

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

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