
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/tree/plot_iris_dtc.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_tree_plot_iris_dtc.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_tree_plot_iris_dtc.py:


=======================================================================
Plot the decision surface of decision trees trained on the iris dataset
=======================================================================

Plot the decision surface of a decision tree trained on pairs
of features of the iris dataset.

See :ref:`decision tree <tree>` for more information on the estimator.

For each pair of iris features, the decision tree learns decision
boundaries made of combinations of simple thresholding rules inferred from
the training samples.

We also show the tree structure of a model built on all of the features.

.. GENERATED FROM PYTHON SOURCE LINES 17-21

.. code-block:: Python


    # Authors: The scikit-learn developers
    # SPDX-License-Identifier: BSD-3-Clause








.. GENERATED FROM PYTHON SOURCE LINES 22-23

First load the copy of the Iris dataset shipped with scikit-learn:

.. GENERATED FROM PYTHON SOURCE LINES 23-28

.. code-block:: Python

    from sklearn.datasets import load_iris

    iris = load_iris()









.. GENERATED FROM PYTHON SOURCE LINES 29-30

Display the decision functions of trees trained on all pairs of features.

.. GENERATED FROM PYTHON SOURCE LINES 30-76

.. code-block:: Python

    import matplotlib.pyplot as plt
    from matplotlib.colors import ListedColormap

    from sklearn.inspection import DecisionBoundaryDisplay
    from sklearn.tree import DecisionTreeClassifier

    for pairidx, pair in enumerate([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]):
        # We only take the two corresponding features
        X = iris.data[:, pair]
        y = iris.target

        # Train
        clf = DecisionTreeClassifier().fit(X, y)

        # Plot the decision boundary
        ax = plt.subplot(2, 3, pairidx + 1)
        plt.tight_layout(h_pad=0.5, w_pad=0.5, pad=2.5)
        disp = DecisionBoundaryDisplay.from_estimator(
            clf,
            X,
            response_method="predict",
            ax=ax,
            xlabel=iris.feature_names[pair[0]],
            ylabel=iris.feature_names[pair[1]],
            alpha=0.5,
        )

        # Plot the training points
        scatter = disp.ax_.scatter(
            X[:, 0],
            X[:, 1],
            c=y,
            cmap=ListedColormap(disp.multiclass_colors_),
            edgecolor="black",
            s=15,
        )

    plt.suptitle("Decision surface of decision trees trained on pairs of features")
    plt.figlegend(
        scatter.legend_elements()[0],
        iris.target_names,
        loc="lower center",
        ncols=len(iris.target_names),
    )
    _ = plt.axis("tight")




.. image-sg:: /auto_examples/tree/images/sphx_glr_plot_iris_dtc_001.png
   :alt: Decision surface of decision trees trained on pairs of features
   :srcset: /auto_examples/tree/images/sphx_glr_plot_iris_dtc_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 77-79

Display the structure of a single decision tree trained on all the features
together.

.. GENERATED FROM PYTHON SOURCE LINES 79-86

.. code-block:: Python

    from sklearn.tree import plot_tree

    plt.figure()
    clf = DecisionTreeClassifier().fit(iris.data, iris.target)
    plot_tree(clf, filled=True)
    plt.title("Decision tree trained on all the iris features")
    plt.show()



.. image-sg:: /auto_examples/tree/images/sphx_glr_plot_iris_dtc_002.png
   :alt: Decision tree trained on all the iris features
   :srcset: /auto_examples/tree/images/sphx_glr_plot_iris_dtc_002.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_tree_plot_iris_dtc.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

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

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

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

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_iris_dtc.zip <plot_iris_dtc.zip>`


.. include:: plot_iris_dtc.recommendations


.. only:: html

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

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