.. 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 ` 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_tree_plot_iris_dtc.py: ======================================================================= 绘制在鸢尾花数据集上训练的决策树的决策边界 ======================================================================= 绘制在鸢尾花数据集的特征对上训练的决策树的决策边界。 有关估计器的更多信息,请参见 :ref:`决策树 ` 。 对于每对鸢尾花特征,决策树通过从训练样本中推断出的简单阈值规则组合来学习决策边界。 我们还展示了基于所有特征构建的模型的树结构。 .. GENERATED FROM PYTHON SOURCE LINES 16-17 首先加载scikit-learn附带的Iris数据集副本: .. GENERATED FROM PYTHON SOURCE LINES 17-23 .. code-block:: Python from sklearn.datasets import load_iris iris = load_iris() .. GENERATED FROM PYTHON SOURCE LINES 24-25 显示在所有特征对上训练的树的决策函数。 .. GENERATED FROM PYTHON SOURCE LINES 25-75 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import load_iris from sklearn.inspection import DecisionBoundaryDisplay from sklearn.tree import DecisionTreeClassifier # Parameters n_classes = 3 plot_colors = "ryb" plot_step = 0.02 for pairidx, pair in enumerate([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]): # 我们只取两个对应的特征 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) DecisionBoundaryDisplay.from_estimator( clf, X, cmap=plt.cm.RdYlBu, response_method="predict", ax=ax, xlabel=iris.feature_names[pair[0]], ylabel=iris.feature_names[pair[1]], ) # Plot the training points for i, color in zip(range(n_classes), plot_colors): idx = np.where(y == i) plt.scatter( X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i], edgecolor="black", s=15, ) plt.suptitle("Decision surface of decision trees trained on pairs of features") plt.legend(loc="lower right", borderpad=0, handletextpad=0) _ = 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 76-77 显示在所有特征上训练的单个决策树的结构。 .. GENERATED FROM PYTHON SOURCE LINES 77-85 .. 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.346 seconds) .. _sphx_glr_download_auto_examples_tree_plot_iris_dtc.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/main?urlpath=lab/tree/notebooks/auto_examples/tree/plot_iris_dtc.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_iris_dtc.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_iris_dtc.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_iris_dtc.zip ` .. include:: plot_iris_dtc.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_