{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Plot the decision surface of decision trees trained on the iris dataset\n\nPlot the decision surface of a decision tree trained on pairs\nof features of the iris dataset.\n\nSee `decision tree ` for more information on the estimator.\n\nFor each pair of iris features, the decision tree learns decision\nboundaries made of combinations of simple thresholding rules inferred from\nthe training samples.\n\nWe also show the tree structure of a model built on all of the features.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First load the copy of the Iris dataset shipped with scikit-learn:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sklearn.datasets import load_iris\n\niris = load_iris()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Display the decision functions of trees trained on all pairs of features.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn.datasets import load_iris\nfrom sklearn.inspection import DecisionBoundaryDisplay\nfrom sklearn.tree import DecisionTreeClassifier\n\n# Parameters\nn_classes = 3\nplot_colors = \"ryb\"\nplot_step = 0.02\n\n\nfor pairidx, pair in enumerate([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]):\n # We only take the two corresponding features\n X = iris.data[:, pair]\n y = iris.target\n\n # Train\n clf = DecisionTreeClassifier().fit(X, y)\n\n # Plot the decision boundary\n ax = plt.subplot(2, 3, pairidx + 1)\n plt.tight_layout(h_pad=0.5, w_pad=0.5, pad=2.5)\n DecisionBoundaryDisplay.from_estimator(\n clf,\n X,\n cmap=plt.cm.RdYlBu,\n response_method=\"predict\",\n ax=ax,\n xlabel=iris.feature_names[pair[0]],\n ylabel=iris.feature_names[pair[1]],\n )\n\n # Plot the training points\n for i, color in zip(range(n_classes), plot_colors):\n idx = np.where(y == i)\n plt.scatter(\n X[idx, 0],\n X[idx, 1],\n c=color,\n label=iris.target_names[i],\n edgecolor=\"black\",\n s=15,\n )\n\nplt.suptitle(\"Decision surface of decision trees trained on pairs of features\")\nplt.legend(loc=\"lower right\", borderpad=0, handletextpad=0)\n_ = plt.axis(\"tight\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Display the structure of a single decision tree trained on all the features\ntogether.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sklearn.tree import plot_tree\n\nplt.figure()\nclf = DecisionTreeClassifier().fit(iris.data, iris.target)\nplot_tree(clf, filled=True)\nplt.title(\"Decision tree trained on all the iris features\")\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.21" } }, "nbformat": 4, "nbformat_minor": 0 }