handson-ml/06_decision_trees.ipynb

2042 lines
257 KiB
Plaintext
Raw Permalink Normal View History

2016-09-27 23:31:21 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
2016-09-27 23:31:21 +02:00
"source": [
"**Chapter 6 Decision Trees**"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
2016-09-27 23:31:21 +02:00
"source": [
"_This notebook contains all the sample code and solutions to the exercises in chapter 6._"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/ageron/handson-ml3/blob/main/06_decision_trees.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://kaggle.com/kernels/welcome?src=https://github.com/ageron/handson-ml3/blob/main/06_decision_trees.ipynb\"><img src=\"https://kaggle.com/static/images/open-in-kaggle.svg\" /></a>\n",
" </td>\n",
"</table>"
]
},
2016-09-27 23:31:21 +02:00
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
2016-09-27 23:31:21 +02:00
"source": [
"# Setup"
]
},
{
"cell_type": "markdown",
"metadata": {},
2016-09-27 23:31:21 +02:00
"source": [
2022-02-19 11:03:20 +01:00
"This project requires Python 3.7 or above:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
2022-02-19 11:03:20 +01:00
"assert sys.version_info >= (3, 7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It also requires Scikit-Learn ≥ 1.0.1:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from packaging import version\n",
"import sklearn\n",
"\n",
"assert version.parse(sklearn.__version__) >= version.parse(\"1.0.1\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As we did in previous chapters, let's define the default font sizes to make the figures prettier:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
2021-11-27 01:47:56 +01:00
"import matplotlib.pyplot as plt\n",
"\n",
"plt.rc('font', size=14)\n",
2021-11-27 01:47:56 +01:00
"plt.rc('axes', labelsize=14, titlesize=14)\n",
"plt.rc('legend', fontsize=14)\n",
2021-12-08 03:16:42 +01:00
"plt.rc('xtick', labelsize=10)\n",
"plt.rc('ytick', labelsize=10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And let's create the `images/decision_trees` folder (if it doesn't already exist), and define the `save_fig()` function which is used through this notebook to save the figures in high-res for the book:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"\n",
"IMAGES_PATH = Path() / \"images\" / \"decision_trees\"\n",
"IMAGES_PATH.mkdir(parents=True, exist_ok=True)\n",
"\n",
"def save_fig(fig_id, tight_layout=True, fig_extension=\"png\", resolution=300):\n",
" path = IMAGES_PATH / f\"{fig_id}.{fig_extension}\"\n",
" if tight_layout:\n",
" plt.tight_layout()\n",
" plt.savefig(path, format=fig_extension, dpi=resolution)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Training and Visualizing a Decision Tree"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"DecisionTreeClassifier(max_depth=2, random_state=42)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.datasets import load_iris\n",
"from sklearn.tree import DecisionTreeClassifier\n",
"\n",
"iris = load_iris(as_frame=True)\n",
"X_iris = iris.data[[\"petal length (cm)\", \"petal width (cm)\"]].values\n",
"y_iris = iris.target\n",
"\n",
"tree_clf = DecisionTreeClassifier(max_depth=2, random_state=42)\n",
"tree_clf.fit(X_iris, y_iris)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**This code example generates Figure 61. Iris Decision Tree:**"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.tree import export_graphviz\n",
"\n",
"export_graphviz(\n",
" tree_clf,\n",
" out_file=str(IMAGES_PATH / \"iris_tree.dot\"), # path differs in the book\n",
" feature_names=[\"petal length (cm)\", \"petal width (cm)\"],\n",
" class_names=iris.target_names,\n",
" rounded=True,\n",
" filled=True\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.49.1 (0)\n",
" -->\n",
"<!-- Title: Tree Pages: 1 -->\n",
"<svg width=\"360pt\" height=\"314pt\"\n",
" viewBox=\"0.00 0.00 360.00 314.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 310)\">\n",
"<title>Tree</title>\n",
"<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-310 356,-310 356,4 -4,4\"/>\n",
"<!-- 0 -->\n",
"<g id=\"node1\" class=\"node\">\n",
"<title>0</title>\n",
"<path fill=\"#ffffff\" stroke=\"black\" d=\"M215,-306C215,-306 67,-306 67,-306 61,-306 55,-300 55,-294 55,-294 55,-235 55,-235 55,-229 61,-223 67,-223 67,-223 215,-223 215,-223 221,-223 227,-229 227,-235 227,-235 227,-294 227,-294 227,-300 221,-306 215,-306\"/>\n",
"<text text-anchor=\"middle\" x=\"141\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">petal length (cm) &lt;= 2.45</text>\n",
"<text text-anchor=\"middle\" x=\"141\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.667</text>\n",
"<text text-anchor=\"middle\" x=\"141\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 150</text>\n",
"<text text-anchor=\"middle\" x=\"141\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [50, 50, 50]</text>\n",
"<text text-anchor=\"middle\" x=\"141\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = setosa</text>\n",
"</g>\n",
"<!-- 1 -->\n",
"<g id=\"node2\" class=\"node\">\n",
"<title>1</title>\n",
"<path fill=\"#e58139\" stroke=\"black\" d=\"M108,-179.5C108,-179.5 12,-179.5 12,-179.5 6,-179.5 0,-173.5 0,-167.5 0,-167.5 0,-123.5 0,-123.5 0,-117.5 6,-111.5 12,-111.5 12,-111.5 108,-111.5 108,-111.5 114,-111.5 120,-117.5 120,-123.5 120,-123.5 120,-167.5 120,-167.5 120,-173.5 114,-179.5 108,-179.5\"/>\n",
"<text text-anchor=\"middle\" x=\"60\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\n",
"<text text-anchor=\"middle\" x=\"60\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 50</text>\n",
"<text text-anchor=\"middle\" x=\"60\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [50, 0, 0]</text>\n",
"<text text-anchor=\"middle\" x=\"60\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = setosa</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;1 -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>0&#45;&gt;1</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M112.9,-222.91C105.11,-211.65 96.64,-199.42 88.8,-188.11\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"91.53,-185.9 82.96,-179.67 85.78,-189.88 91.53,-185.9\"/>\n",
"<text text-anchor=\"middle\" x=\"78.49\" y=\"-200.56\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g id=\"node3\" class=\"node\">\n",
"<title>2</title>\n",
"<path fill=\"#ffffff\" stroke=\"black\" d=\"M293.5,-187C293.5,-187 150.5,-187 150.5,-187 144.5,-187 138.5,-181 138.5,-175 138.5,-175 138.5,-116 138.5,-116 138.5,-110 144.5,-104 150.5,-104 150.5,-104 293.5,-104 293.5,-104 299.5,-104 305.5,-110 305.5,-116 305.5,-116 305.5,-175 305.5,-175 305.5,-181 299.5,-187 293.5,-187\"/>\n",
"<text text-anchor=\"middle\" x=\"222\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">petal width (cm) &lt;= 1.75</text>\n",
"<text text-anchor=\"middle\" x=\"222\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.5</text>\n",
"<text text-anchor=\"middle\" x=\"222\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 100</text>\n",
"<text text-anchor=\"middle\" x=\"222\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 50, 50]</text>\n",
"<text text-anchor=\"middle\" x=\"222\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = versicolor</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;2 -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>0&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M169.1,-222.91C175.26,-214.01 181.84,-204.51 188.19,-195.33\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"191.13,-197.24 193.95,-187.02 185.38,-193.25 191.13,-197.24\"/>\n",
"<text text-anchor=\"middle\" x=\"198.42\" y=\"-207.92\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g id=\"node4\" class=\"node\">\n",
"<title>3</title>\n",
"<path fill=\"#4de88e\" stroke=\"black\" d=\"M202,-68C202,-68 102,-68 102,-68 96,-68 90,-62 90,-56 90,-56 90,-12 90,-12 90,-6 96,0 102,0 102,0 202,0 202,0 208,0 214,-6 214,-12 214,-12 214,-56 214,-56 214,-62 208,-68 202,-68\"/>\n",
"<text text-anchor=\"middle\" x=\"152\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.168</text>\n",
"<text text-anchor=\"middle\" x=\"152\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 54</text>\n",
"<text text-anchor=\"middle\" x=\"152\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 49, 5]</text>\n",
"<text text-anchor=\"middle\" x=\"152\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = versicolor</text>\n",
"</g>\n",
"<!-- 2&#45;&gt;3 -->\n",
"<g id=\"edge3\" class=\"edge\">\n",
"<title>2&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M195.93,-103.73C190.34,-94.97 184.41,-85.7 178.79,-76.91\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"181.62,-74.84 173.29,-68.3 175.72,-78.61 181.62,-74.84\"/>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g id=\"node5\" class=\"node\">\n",
"<title>4</title>\n",
"<path fill=\"#843de6\" stroke=\"black\" d=\"M340,-68C340,-68 244,-68 244,-68 238,-68 232,-62 232,-56 232,-56 232,-12 232,-12 232,-6 238,0 244,0 244,0 340,0 340,0 346,0 352,-6 352,-12 352,-12 352,-56 352,-56 352,-62 346,-68 340,-68\"/>\n",
"<text text-anchor=\"middle\" x=\"292\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.043</text>\n",
"<text text-anchor=\"middle\" x=\"292\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 46</text>\n",
"<text text-anchor=\"middle\" x=\"292\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1, 45]</text>\n",
"<text text-anchor=\"middle\" x=\"292\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = virginica</text>\n",
"</g>\n",
"<!-- 2&#45;&gt;4 -->\n",
"<g id=\"edge4\" class=\"edge\">\n",
"<title>2&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M248.07,-103.73C253.66,-94.97 259.59,-85.7 265.21,-76.91\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"268.28,-78.61 270.71,-68.3 262.38,-74.84 268.28,-78.61\"/>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.files.Source at 0x7fbfb8e45850>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from graphviz import Source\n",
"\n",
"Source.from_file(IMAGES_PATH / \"iris_tree.dot\") # path differs in the book"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Graphviz also provides the `dot` command line tool to convert `.dot` files to a variety of formats. The following command converts the dot file to a png image:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# extra code\n",
"!dot -Tpng {IMAGES_PATH / \"iris_tree.dot\"} -o {IMAGES_PATH / \"iris_tree.png\"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Making Predictions"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjAAAAEQCAY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M7wB4YHOgylXKYJjFJKKeZ8MIc5H/h3lnGlckP7wCillGLc1HGBDkEpt2gCo5RSioFDdaZrFVr0FZJSSimlQo4mMEoppfh+0fd8v+j7QIehlMv0FZJSSimG9B4C6DB/FTo0gVFKKUXnmzsHOgSl3OK3V0giUkVEfhSR7SKyVUQet3FMRxE5LSKbrNuL/opPKaXysg8XfsiHCz8MdBh+k3Q2ib6z+nL8nPfWf3JUpy+ul9f5sw/MFeBpY0w9oDXwiIjUt3HcL8aYptbtZT/GF3JuuKELw4blyAODXmxsLcaPn+C1+kL1OSilAmfyL5NJPJjI5J8n+6VOX1wvr/NbAmOMOWqM2WD9fBbYDlTy1/VDyeDBQ4iP7+30uHnzPmfcuLG+D8jLEhN/4+GHHwx0GEqpPCrpbBJfbP4CYwxfbP7CK60ijur0xfVUgEYhiUh14BpgjY3iNiKyWUS+EZEGds4fKiLrRGTdiRN/+zLUoHTp0iUASpUqRbFixQIcTVaXL192ekzZsmUpXLiwH6JxTVpaGqmpqYEOQ6mAqhpRlaoRVQMdhl9M/mUy6esAppk0r7SKOKrTF9dTAUhgRKQoMA94whhzJlvxBqCaMaYJ8A7wla06jDHTjDEtjDEtypYt7dN4K1TIT758BXJsFSr4r/9zeovM66+/SZUqsVSpEgvkfHUyf/4CmjRpRuHC0ZQuHUPHjp1JSkqyWeeAAYPo06dfln1paWlUrRr
"text/plain": [
"<Figure size 576x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# extra code just formatting details\n",
"from matplotlib.colors import ListedColormap\n",
2021-12-08 03:16:42 +01:00
"custom_cmap = ListedColormap(['#fafab0', '#9898ff', '#a0faa0'])\n",
"plt.figure(figsize=(8, 4))\n",
"\n",
"lengths, widths = np.meshgrid(np.linspace(0, 7.2, 100), np.linspace(0, 3, 100))\n",
"X_iris_all = np.c_[lengths.ravel(), widths.ravel()]\n",
"y_pred = tree_clf.predict(X_iris_all).reshape(lengths.shape)\n",
"plt.contourf(lengths, widths, y_pred, alpha=0.3, cmap=custom_cmap)\n",
"for idx, (name, style) in enumerate(zip(iris.target_names, (\"yo\", \"bs\", \"g^\"))):\n",
" plt.plot(X_iris[:, 0][y_iris == idx], X_iris[:, 1][y_iris == idx],\n",
" style, label=f\"Iris {name}\")\n",
"\n",
"# extra code this section beautifies and saves Figure 62\n",
"tree_clf_deeper = DecisionTreeClassifier(max_depth=3, random_state=42)\n",
"tree_clf_deeper.fit(X_iris, y_iris)\n",
"th0, th1, th2a, th2b = tree_clf_deeper.tree_.threshold[[0, 2, 3, 6]]\n",
"plt.xlabel(\"Petal length (cm)\")\n",
"plt.ylabel(\"Petal width (cm)\")\n",
"plt.plot([th0, th0], [0, 3], \"k-\", linewidth=2)\n",
"plt.plot([th0, 7.2], [th1, th1], \"k--\", linewidth=2)\n",
"plt.plot([th2a, th2a], [0, th1], \"k:\", linewidth=2)\n",
"plt.plot([th2b, th2b], [th1, 3], \"k:\", linewidth=2)\n",
"plt.text(th0 - 0.05, 1.0, \"Depth=0\", horizontalalignment=\"right\", fontsize=15)\n",
"plt.text(3.2, th1 + 0.02, \"Depth=1\", verticalalignment=\"bottom\", fontsize=13)\n",
"plt.text(th2a + 0.05, 0.5, \"(Depth=2)\", fontsize=11)\n",
"plt.axis([0, 7.2, 0, 3])\n",
"plt.legend()\n",
"save_fig(\"decision_tree_decision_boundaries_plot\")\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can access the tree structure via the `tree_` attribute:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"<sklearn.tree._tree.Tree at 0x7fbfa8b563b0>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree_clf.tree_"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For more information, check out this class's documentation:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# help(sklearn.tree._tree.Tree)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See the extra material section below for an example."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Estimating Class Probabilities"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([[0. , 0.907, 0.093]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree_clf.predict_proba([[5, 1.5]]).round(3)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([1])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree_clf.predict([[5, 1.5]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Regularization Hyperparameters"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"DecisionTreeClassifier(min_samples_leaf=5, random_state=42)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.datasets import make_moons\n",
"\n",
"X_moons, y_moons = make_moons(n_samples=150, noise=0.2, random_state=42)\n",
"\n",
"tree_clf1 = DecisionTreeClassifier(random_state=42)\n",
"tree_clf2 = DecisionTreeClassifier(min_samples_leaf=5, random_state=42)\n",
"tree_clf1.fit(X_moons, y_moons)\n",
"tree_clf2.fit(X_moons, y_moons)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAsAAAAEQCAYAAAC++cJdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLj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rsvFp9L6IPEc2Ey8L/DnPNz2E8Tz9Q/A74ifbfqXHMuXm8vFyLkt59wDxC9w8S3i/ZWXEM9mP0wnfqx7AVhK/EvLRc653T6Vp6bZsS5GIiIiIoVLdLU40Tn3Vr/LIlIMtQCLiIiISF3xpQJsZncnrkSzKcv9l5tZm5ltSPz4dTpCRETEd2b2gxyzC/zA7/JVg5nNzDPLQsFd1ER86QJhZm8k3rH9Z865szLcfznxCwTolIqIiNQ9M5sIjM1yd7tz7kA1y+MHi1++/eQci+xKzHgjkpcvs0A4556s8LW0RUREakaiglvzldxcEpXb7X6XQ2pDkPsAX2xmLYmJpc/0uzAiIiIiUhuCOg/wOuITV3ea2TXAvWS5EoqZ3QTcBDBq1IjzTzv95GqVUUTK0kxPdx9tbW20tbUxasQwpk083u9CBcraTdsPOecmVGr9yk+REHNNdHf30draRnt7G8ePHcXEE7L1kqlPuTLUt2nQEl0gHszUBzjDsruAhVkuFDDg/IVnuKef+S9vCigiFWVuBju27eehBx/i4WXLOG/eDP7js+/2u1iBEpnztrXOuYXV2JbyUyRkotPYsvll7r//AZY/9ig3LD6PT9/4Zr9LFSi5MjSQXSDMbHLiqi2Y2QXEy/mav6USERERkVrgSxcIM/slcDlwopm9TPyqNE0AzrkfAO8E/sHM+oGjwHudrtgRai0btvD48lW0tXZw3LgxLF6yiPkL5vldLBGRUFCGinjLr1kgcl5
"text/plain": [
"<Figure size 720x288 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# extra code this cell generates and saves Figure 63\n",
"\n",
"def plot_decision_boundary(clf, X, y, axes, cmap):\n",
" x1, x2 = np.meshgrid(np.linspace(axes[0], axes[1], 100),\n",
" np.linspace(axes[2], axes[3], 100))\n",
" X_new = np.c_[x1.ravel(), x2.ravel()]\n",
" y_pred = clf.predict(X_new).reshape(x1.shape)\n",
" \n",
" plt.contourf(x1, x2, y_pred, alpha=0.3, cmap=cmap)\n",
" plt.contour(x1, x2, y_pred, cmap=\"Greys\", alpha=0.8)\n",
" colors = {\"Wistia\": [\"#78785c\", \"#c47b27\"], \"Pastel1\": [\"red\", \"blue\"]}\n",
" markers = (\"o\", \"^\")\n",
" for idx in (0, 1):\n",
" plt.plot(X[:, 0][y == idx], X[:, 1][y == idx],\n",
" color=colors[cmap][idx], marker=markers[idx], linestyle=\"none\")\n",
" plt.axis(axes)\n",
" plt.xlabel(r\"$x_1$\")\n",
" plt.ylabel(r\"$x_2$\", rotation=0)\n",
"\n",
"fig, axes = plt.subplots(ncols=2, figsize=(10, 4), sharey=True)\n",
"plt.sca(axes[0])\n",
"plot_decision_boundary(tree_clf1, X_moons, y_moons,\n",
" axes=[-1.5, 2.4, -1, 1.5], cmap=\"Wistia\")\n",
"plt.title(\"No restrictions\")\n",
"plt.sca(axes[1])\n",
"plot_decision_boundary(tree_clf2, X_moons, y_moons,\n",
" axes=[-1.5, 2.4, -1, 1.5], cmap=\"Wistia\")\n",
"plt.title(f\"min_samples_leaf = {tree_clf2.min_samples_leaf}\")\n",
"plt.ylabel(\"\")\n",
"save_fig(\"min_samples_leaf_plot\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"0.898"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X_moons_test, y_moons_test = make_moons(n_samples=1000, noise=0.2,\n",
" random_state=43)\n",
"tree_clf1.score(X_moons_test, y_moons_test)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"0.92"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree_clf2.score(X_moons_test, y_moons_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Regression"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's prepare a simple quadratic training set:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Code example:**"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"DecisionTreeRegressor(max_depth=2, random_state=42)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.tree import DecisionTreeRegressor\n",
"\n",
"np.random.seed(42)\n",
"X_quad = np.random.rand(200, 1) - 0.5 # a single random input feature\n",
"y_quad = X_quad ** 2 + 0.025 * np.random.randn(200, 1)\n",
"\n",
"tree_reg = DecisionTreeRegressor(max_depth=2, random_state=42)\n",
"tree_reg.fit(X_quad, y_quad)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.49.1 (0)\n",
" -->\n",
"<!-- Title: Tree Pages: 1 -->\n",
"<svg width=\"682pt\" height=\"269pt\"\n",
" viewBox=\"0.00 0.00 682.00 269.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 265)\">\n",
"<title>Tree</title>\n",
"<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-265 678,-265 678,4 -4,4\"/>\n",
"<!-- 0 -->\n",
"<g id=\"node1\" class=\"node\">\n",
"<title>0</title>\n",
"<path fill=\"#f6d6be\" stroke=\"black\" d=\"M402,-261C402,-261 271,-261 271,-261 265,-261 259,-255 259,-249 259,-249 259,-205 259,-205 259,-199 265,-193 271,-193 271,-193 402,-193 402,-193 408,-193 414,-199 414,-205 414,-205 414,-249 414,-249 414,-255 408,-261 402,-261\"/>\n",
"<text text-anchor=\"middle\" x=\"336.5\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">x1 &lt;= &#45;0.303</text>\n",
"<text text-anchor=\"middle\" x=\"336.5\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">squared_error = 0.006</text>\n",
"<text text-anchor=\"middle\" x=\"336.5\" y=\"-215.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 200</text>\n",
"<text text-anchor=\"middle\" x=\"336.5\" y=\"-200.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.088</text>\n",
"</g>\n",
"<!-- 1 -->\n",
"<g id=\"node2\" class=\"node\">\n",
"<title>1</title>\n",
"<path fill=\"#eb9d65\" stroke=\"black\" d=\"M316,-157C316,-157 185,-157 185,-157 179,-157 173,-151 173,-145 173,-145 173,-101 173,-101 173,-95 179,-89 185,-89 185,-89 316,-89 316,-89 322,-89 328,-95 328,-101 328,-101 328,-145 328,-145 328,-151 322,-157 316,-157\"/>\n",
"<text text-anchor=\"middle\" x=\"250.5\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">x1 &lt;= &#45;0.408</text>\n",
"<text text-anchor=\"middle\" x=\"250.5\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">squared_error = 0.002</text>\n",
"<text text-anchor=\"middle\" x=\"250.5\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 44</text>\n",
"<text text-anchor=\"middle\" x=\"250.5\" y=\"-96.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.172</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;1 -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>0&#45;&gt;1</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M308.58,-192.88C301.07,-183.98 292.86,-174.24 285.03,-164.96\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"287.7,-162.69 278.58,-157.3 282.35,-167.2 287.7,-162.69\"/>\n",
"<text text-anchor=\"middle\" x=\"276.46\" y=\"-178.51\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g id=\"node5\" class=\"node\">\n",
"<title>4</title>\n",
"<path fill=\"#fae6d7\" stroke=\"black\" d=\"M489,-157C489,-157 358,-157 358,-157 352,-157 346,-151 346,-145 346,-145 346,-101 346,-101 346,-95 352,-89 358,-89 358,-89 489,-89 489,-89 495,-89 501,-95 501,-101 501,-101 501,-145 501,-145 501,-151 495,-157 489,-157\"/>\n",
"<text text-anchor=\"middle\" x=\"423.5\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">x1 &lt;= 0.272</text>\n",
"<text text-anchor=\"middle\" x=\"423.5\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">squared_error = 0.005</text>\n",
"<text text-anchor=\"middle\" x=\"423.5\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 156</text>\n",
"<text text-anchor=\"middle\" x=\"423.5\" y=\"-96.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.065</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;4 -->\n",
"<g id=\"edge4\" class=\"edge\">\n",
"<title>0&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M364.75,-192.88C372.34,-183.98 380.65,-174.24 388.57,-164.96\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"391.27,-167.18 395.1,-157.3 385.95,-162.64 391.27,-167.18\"/>\n",
"<text text-anchor=\"middle\" x=\"397.08\" y=\"-178.52\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g id=\"node3\" class=\"node\">\n",
"<title>2</title>\n",
"<path fill=\"#e58139\" stroke=\"black\" d=\"M143,-53C143,-53 12,-53 12,-53 6,-53 0,-47 0,-41 0,-41 0,-12 0,-12 0,-6 6,0 12,0 12,0 143,0 143,0 149,0 155,-6 155,-12 155,-12 155,-41 155,-41 155,-47 149,-53 143,-53\"/>\n",
"<text text-anchor=\"middle\" x=\"77.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">squared_error = 0.001</text>\n",
"<text text-anchor=\"middle\" x=\"77.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 20</text>\n",
"<text text-anchor=\"middle\" x=\"77.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.213</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;2 -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>1&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M189.99,-88.95C171.66,-78.93 151.6,-67.98 133.57,-58.13\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"134.8,-54.81 124.34,-53.09 131.44,-60.95 134.8,-54.81\"/>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g id=\"node4\" class=\"node\">\n",
"<title>3</title>\n",
"<path fill=\"#f0b489\" stroke=\"black\" d=\"M316,-53C316,-53 185,-53 185,-53 179,-53 173,-47 173,-41 173,-41 173,-12 173,-12 173,-6 179,0 185,0 185,0 316,0 316,0 322,0 328,-6 328,-12 328,-12 328,-41 328,-41 328,-47 322,-53 316,-53\"/>\n",
"<text text-anchor=\"middle\" x=\"250.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">squared_error = 0.001</text>\n",
"<text text-anchor=\"middle\" x=\"250.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 24</text>\n",
"<text text-anchor=\"middle\" x=\"250.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.138</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;3 -->\n",
"<g id=\"edge3\" class=\"edge\">\n",
"<title>1&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M250.5,-88.95C250.5,-80.72 250.5,-71.85 250.5,-63.48\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"254,-63.24 250.5,-53.24 247,-63.24 254,-63.24\"/>\n",
"</g>\n",
"<!-- 5 -->\n",
"<g id=\"node6\" class=\"node\">\n",
"<title>5</title>\n",
"<path fill=\"#ffffff\" stroke=\"black\" d=\"M489,-53C489,-53 358,-53 358,-53 352,-53 346,-47 346,-41 346,-41 346,-12 346,-12 346,-6 352,0 358,0 358,0 489,0 489,0 495,0 501,-6 501,-12 501,-12 501,-41 501,-41 501,-47 495,-53 489,-53\"/>\n",
"<text text-anchor=\"middle\" x=\"423.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">squared_error = 0.001</text>\n",
"<text text-anchor=\"middle\" x=\"423.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 110</text>\n",
"<text text-anchor=\"middle\" x=\"423.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.028</text>\n",
"</g>\n",
"<!-- 4&#45;&gt;5 -->\n",
"<g id=\"edge5\" class=\"edge\">\n",
"<title>4&#45;&gt;5</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M423.5,-88.95C423.5,-80.72 423.5,-71.85 423.5,-63.48\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"427,-63.24 423.5,-53.24 420,-63.24 427,-63.24\"/>\n",
"</g>\n",
"<!-- 6 -->\n",
"<g id=\"node7\" class=\"node\">\n",
"<title>6</title>\n",
"<path fill=\"#edaa79\" stroke=\"black\" d=\"M662,-53C662,-53 531,-53 531,-53 525,-53 519,-47 519,-41 519,-41 519,-12 519,-12 519,-6 525,0 531,0 531,0 662,0 662,0 668,0 674,-6 674,-12 674,-12 674,-41 674,-41 674,-47 668,-53 662,-53\"/>\n",
"<text text-anchor=\"middle\" x=\"596.5\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">squared_error = 0.002</text>\n",
"<text text-anchor=\"middle\" x=\"596.5\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 46</text>\n",
"<text text-anchor=\"middle\" x=\"596.5\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = 0.154</text>\n",
"</g>\n",
"<!-- 4&#45;&gt;6 -->\n",
"<g id=\"edge6\" class=\"edge\">\n",
"<title>4&#45;&gt;6</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M484.01,-88.95C502.34,-78.93 522.4,-67.98 540.43,-58.13\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"542.56,-60.95 549.66,-53.09 539.2,-54.81 542.56,-60.95\"/>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.files.Source at 0x7fbfb925c760>"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# extra code we've already seen how to use export_graphviz()\n",
"export_graphviz(\n",
" tree_reg,\n",
" out_file=str(IMAGES_PATH / \"regression_tree.dot\"),\n",
" feature_names=[\"x1\"],\n",
" rounded=True,\n",
" filled=True\n",
")\n",
"Source.from_file(IMAGES_PATH / \"regression_tree.dot\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"DecisionTreeRegressor(max_depth=3, random_state=42)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree_reg2 = DecisionTreeRegressor(max_depth=3, random_state=42)\n",
"tree_reg2.fit(X_quad, y_quad)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([-0.30265072, -0.40830374, -2. , -2. , 0.27175756,\n",
" -2. , -2. ])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree_reg.tree_.threshold"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([-0.30265072, -0.40830374, -0.45416115, -2. , -2. ,\n",
" -0.37022041, -2. , -2. , 0.27175756, -0.21270403,\n",
" -2. , -2. , 0.40399227, -2. , -2. ])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree_reg2.tree_.threshold"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAsAAAAEQCAYAAAC++cJdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAABoTUlEQVR4nO29eXwV1f3//zy5WYgJEESFgmwKFO0HFxKXSKuxiksLrl0QLdaqkVr9aottRWsbxYLtp/hBW6sEtWpbq21dWqi2Ll+i/r4J/RAQtVbFDREQURogbCG5Ob8/5t5kcrnLzNzZ7r3v5+Mxj+TOds6ZOec17znzPu+jtNYIgiAIgiAIQqFQFHQGBEEQBEEQBMFPxAAWBEEQBEEQCgoxgAVBEARBEISCQgxgQRAEQRAEoaAQA1gQBEEQBEEoKMQAFgRBEARBEAoKMYAFwQJKqRqllFZKjfYhrW8qpXZ4nY4gCIJXiGYKYUcMYEEIEKXUWqXUdT6kM0cptUIptV0p9YlSaolS6r+8TlcQBMFNfNTM7yilXo1p5nalVItS6stepyv4hxjAglAY1AG/Bk4Avgh0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drIfqxlPGR2MHFdG5d4MPsDvvw/bttE/EqE9hfEL7mlm//79aW9vz/5EAZIPZRAEJ4Sx7uetAez3RBhCMBSq20My5FoI2fAmhzGJViIRmHsJzJmT4YCf/hR+9CM2XnONL/nLhwkk8qEMguCEMNb9oqAz4BX9+xs9v5GI+OsKghBeWlpg/nzjb5AoZVMvS0oAaHr/fU/zFScfwoflQxkEwQlu1X039TJve4D9ngjDi5jDglCIFFJbShda0W8++1mYOdOGXsYM4MbVq5l
"text/plain": [
"<Figure size 720x288 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# extra code this cell generates and saves Figure 65\n",
"\n",
"def plot_regression_predictions(tree_reg, X, y, axes=[-0.5, 0.5, -0.05, 0.25]):\n",
" x1 = np.linspace(axes[0], axes[1], 500).reshape(-1, 1)\n",
" y_pred = tree_reg.predict(x1)\n",
" plt.axis(axes)\n",
" plt.xlabel(\"$x_1$\")\n",
" plt.plot(X, y, \"b.\")\n",
" plt.plot(x1, y_pred, \"r.-\", linewidth=2, label=r\"$\\hat{y}$\")\n",
"\n",
"fig, axes = plt.subplots(ncols=2, figsize=(10, 4), sharey=True)\n",
"plt.sca(axes[0])\n",
"plot_regression_predictions(tree_reg, X_quad, y_quad)\n",
"\n",
"th0, th1a, th1b = tree_reg.tree_.threshold[[0, 1, 4]]\n",
"for split, style in ((th0, \"k-\"), (th1a, \"k--\"), (th1b, \"k--\")):\n",
" plt.plot([split, split], [-0.05, 0.25], style, linewidth=2)\n",
"plt.text(th0, 0.16, \"Depth=0\", fontsize=15)\n",
"plt.text(th1a + 0.01, -0.01, \"Depth=1\", horizontalalignment=\"center\", fontsize=13)\n",
"plt.text(th1b + 0.01, -0.01, \"Depth=1\", fontsize=13)\n",
"plt.ylabel(\"$y$\", rotation=0)\n",
"plt.legend(loc=\"upper center\", fontsize=16)\n",
"plt.title(\"max_depth=2\")\n",
"\n",
"plt.sca(axes[1])\n",
"th2s = tree_reg2.tree_.threshold[[2, 5, 9, 12]]\n",
"plot_regression_predictions(tree_reg2, X_quad, y_quad)\n",
"for split, style in ((th0, \"k-\"), (th1a, \"k--\"), (th1b, \"k--\")):\n",
" plt.plot([split, split], [-0.05, 0.25], style, linewidth=2)\n",
"for split in th2s:\n",
" plt.plot([split, split], [-0.05, 0.25], \"k:\", linewidth=1)\n",
"plt.text(th2s[2] + 0.01, 0.15, \"Depth=2\", fontsize=13)\n",
"plt.title(\"max_depth=3\")\n",
"\n",
"save_fig(\"tree_regression_plot\")\n",
"plt.show()"
]
},
2016-09-27 23:31:21 +02:00
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAsAAAAEQCAYAAAC++cJdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAACA20lEQVR4nO2dd5gb1bn/P0dli9dlXXHDBWMMhgVDTFEoXiAJJQWCU/iRYEiANS0JKZhLEggJyTU45ZJC8dKdC8lNYiDUYIplU0QxGFiwMWBjwLjb677e1Urn98eZkUbaUdvVStrV+3kePZpyZnRG0pz5zjtvUVprBEEQBEEQBKFc8BS7A4IgCIIgCIJQSEQAC4IgCIIgCGWFCGBBEARBEAShrBABLAiCIAiCIJQVIoAFQRAEQRCEskIEsCAIgiAIglBWiAAWhG5EKXW+UmpXF/dRr5TSSqkh+eqXIAjFwzqfv1bsfnQ3Sql7lFKP9uTPUko1KKU+VkpFlVLX5Xv/QvEQASwUFWvQ0kqpnyctL1nRp5QKKqX+kmXz/wP2y2Hfq5VSP0la/CIwAtiS7X4EQShpRgCPFLsTQnqUUgOBm4HfAqOA33ViHycopR5WSn1qXdPOd2mjlFLXKaXWKqVarGvMwV0+ACEtIoCFUmAvMEspNbS7P0gp5e/uz3B+lta6RW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oekop9a7WenHSdg1AA0BNTc1nDjzwQFi3DtauheHDYdQo+OAD2L4dampgt5WqcPBgGDeuywcoCILQaXbuJLyxmR2+QVQPqKDPyibwW0WpwmGoq4MKU7zrtdde26y1HtrZj3IdKwVBEHoAu3fDzp3
"text/plain": [
"<Figure size 720x288 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"# extra code this cell generates and saves Figure 66\n",
2016-09-27 23:31:21 +02:00
"\n",
"tree_reg1 = DecisionTreeRegressor(random_state=42)\n",
"tree_reg2 = DecisionTreeRegressor(random_state=42, min_samples_leaf=10)\n",
"tree_reg1.fit(X_quad, y_quad)\n",
"tree_reg2.fit(X_quad, y_quad)\n",
"\n",
"x1 = np.linspace(-0.5, 0.5, 500).reshape(-1, 1)\n",
"y_pred1 = tree_reg1.predict(x1)\n",
"y_pred2 = tree_reg2.predict(x1)\n",
2016-09-27 23:31:21 +02:00
"\n",
"fig, axes = plt.subplots(ncols=2, figsize=(10, 4), sharey=True)\n",
2016-09-27 23:31:21 +02:00
"\n",
"plt.sca(axes[0])\n",
"plt.plot(X_quad, y_quad, \"b.\")\n",
"plt.plot(x1, y_pred1, \"r.-\", linewidth=2, label=r\"$\\hat{y}$\")\n",
"plt.axis([-0.5, 0.5, -0.05, 0.25])\n",
"plt.xlabel(\"$x_1$\")\n",
"plt.ylabel(\"$y$\", rotation=0)\n",
"plt.legend(loc=\"upper center\")\n",
"plt.title(\"No restrictions\")\n",
2016-09-27 23:31:21 +02:00
"\n",
"plt.sca(axes[1])\n",
"plt.plot(X_quad, y_quad, \"b.\")\n",
"plt.plot(x1, y_pred2, \"r.-\", linewidth=2, label=r\"$\\hat{y}$\")\n",
"plt.axis([-0.5, 0.5, -0.05, 0.25])\n",
"plt.xlabel(\"$x_1$\")\n",
"plt.title(f\"min_samples_leaf={tree_reg2.min_samples_leaf}\")\n",
2016-09-27 23:31:21 +02:00
"\n",
"save_fig(\"tree_regression_regularization_plot\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sensitivity to axis orientation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Rotating the dataset also leads to completely different decision boundaries:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAsAAAAEQCAYAAAC++cJdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAA1K0lEQVR4nO3de5Ac9XUv8O/ZXUlIJkQPBBIr2WAXtsEPgbI29jWOQaAKr0REcmQcHEglXEIRcZ26ETayDM51rKwdu1x2ZMcUwQ/hQLgE6SKVInBANthgCWvBAoRkDDgOWrEg0IM3Wmnn3D+mB2Znu2e6Z7p/r/5+qrZ2d7Z35szs7K9Pnz6/X4uqgoiIiIioLLpsB0BEREREZBITYCIiIiIqFSbARERERFQqTICJiIiIqFSYABMRERFRqTABJiIiIqJS6bEdQJ6OnDZVj50123YYFLhKVxdeefVVvPDCC3j99dcxadIkzJw503ZYVAIPPvjg86o6PY/74nhJRKF74OGHE8fMoBLgY2fNxi/+8w7bYVDgXj1sEh7YuhX/sX49tu/Ygblz5+Kaa66xHRaVwLhx4/47r/vieElEoeuecUzimMkWCCIiIiIqFSbARERERFQqTICJiIiIqFSYABMRERFRqTABJiIiIqJSYQJMRERERKXCBJiIiIiISoUJMBERERGVChNgIiIiIioVJsBEREREVCpWEmAROUtEHhORJ0TkqoRtThORrSLyqIjcYzpGIiIiIgpTj+kHFJFuAN8GMB/AIIAtIrJOVbfXbTMZwD8DOEtVnxKRo0zHSURERERhslEB/iCAJ1T1N6o6DOBmAAsatvlTAGtU9SkAUNXdhmMkIiIiokDZSIB7Aeys+34wuq3eOwFMEZG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
},
"output_type": "display_data"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"# extra code this cell generates and saves Figure 67\n",
"\n",
"np.random.seed(6)\n",
"X_square = np.random.rand(100, 2) - 0.5\n",
"y_square = (X_square[:, 0] > 0).astype(np.int64)\n",
2016-09-27 23:31:21 +02:00
"\n",
"angle = np.pi / 4 # 45 degrees\n",
"rotation_matrix = np.array([[np.cos(angle), -np.sin(angle)],\n",
" [np.sin(angle), np.cos(angle)]])\n",
"X_rotated_square = X_square.dot(rotation_matrix)\n",
2016-09-27 23:31:21 +02:00
"\n",
"tree_clf_square = DecisionTreeClassifier(random_state=42)\n",
"tree_clf_square.fit(X_square, y_square)\n",
"tree_clf_rotated_square = DecisionTreeClassifier(random_state=42)\n",
"tree_clf_rotated_square.fit(X_rotated_square, y_square)\n",
"\n",
"fig, axes = plt.subplots(ncols=2, figsize=(10, 4), sharey=True)\n",
"plt.sca(axes[0])\n",
"plot_decision_boundary(tree_clf_square, X_square, y_square,\n",
" axes=[-0.7, 0.7, -0.7, 0.7], cmap=\"Pastel1\")\n",
"plt.sca(axes[1])\n",
"plot_decision_boundary(tree_clf_rotated_square, X_rotated_square, y_square,\n",
" axes=[-0.7, 0.7, -0.7, 0.7], cmap=\"Pastel1\")\n",
"plt.ylabel(\"\")\n",
"\n",
"save_fig(\"sensitivity_to_rotation_plot\")\n",
"plt.show()"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"DecisionTreeClassifier(max_depth=2, random_state=42)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.decomposition import PCA\n",
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"pca_pipeline = make_pipeline(StandardScaler(), PCA())\n",
"X_iris_rotated = pca_pipeline.fit_transform(X_iris)\n",
"tree_clf_pca = DecisionTreeClassifier(max_depth=2, random_state=42)\n",
"tree_clf_pca.fit(X_iris_rotated, y_iris)"
]
},
2016-09-27 23:31:21 +02:00
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjAAAAEQCAYAAACutU7EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAABB7UlEQVR4nO3dd3xUVf7/8ddhgASpIiWhhCYgiCAQsIKgYEFUxEZdG6IogrvrT4hYVlGRFWWjsihiwRVpEpRFRAULqDRBQBEFpAZJBL8iARcCyfn9kWLKTOrM3Dsz7+fjMQ8y997c+UwC5JPP+ZxzjLUWERERkVBSwekAREREREpLCYyIiIiEHCUwIiIiEnKUwIiIiEjIUQIjIiIiIUcJjIiIiIScik4H4C916tS2TZs2djoMCRHr1m0C4KyzOjsciYifVE53OgIRv/t23bcHrbV1vZ0z4bIOTHx8B7tmzYdOhyEhwuOJBWDnzvD4+y/iidvrdAgifhfniVtnrY33dk5DSCIiYWLmtJnMnDbT6TBEgiJshpBERCJdwogEAAYPH+xwJCKBpwqMiIiIhBwlMCIiIhJyNIQk4kfp6Yc5ceIXMjNPOB2KhBCPpypVqjTCGP1OKVJSSmBE/CQ9/TAnT6bSqFFDoqKqYIxxOiQJAZmZmaSk7OP48YNER9dzOhyRkKF0X8RPTpz4hYYNGxIdfYqSFymxChUqULdufTIyfnc6FJGQogRGxE8yM08QFVXF6TAkBFWsWInMzJNOhyESUjSEJOJHqrxIWfjr782ejD1+uY9IKFAFRkREREKOEhgRKZHevXtw330jnQ5DRARwKIExxlxujPnRGLPdGDPWxzU9jDEbjDGbjTGfBztGkUgxbNgtXHtt32KvmzMnifHjJwQhIu+iow1JSe849vqhoE+XPvTp0sfpMESCIug9MMYYDzAF6A0kA2uN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znPPO60xsbAMeeugfHDzou5dHpDg51Zf0jKxhyPSMdOZtnMfo7qOpV01LmvmT8TZFMhTFx3ewa9Z86HQYEiI8nlgAdu7039//tLQttGrVxm/3k8iydesWqlcv+98fT9xeNq3bBED7zu39FZaU0oOLH2Tuhrm5CQxAZU9lbjr7JlVhyiDOE7fOWhvv7ZwqMCIiYaJv16wtIbQrtXPWJ6/Pl7xAVhVmXfI6hyIKX0pgRERE/GTJ8CVOhxAxtBu1iIiIhBwlMCIiIhJylMCIiIhIyFECIyIiIiFHCYyIiIiEHM1CEhEJE4vWLHI6BJGgUQIjIhImtICdRBINIYlIifTu3YP77htZ/IUu06pVUyZPnuS3+4Xq10Ek3KgCIxLhhg27hV9/PciCBUUPP8yZk0SlSpWCFJX/fPnlWqpWrep0GEEx5s4xgDZ1DKTUtFRGJo1kynVTtLeRw1SBEZEipadnLYteu3Ztqlev7nA0+Z04caLYa+rWrcspp5wShGhKJjMzk4yMjIDce9b0WcyaPisg95YsiSsSWbN3DYnLE50OJeIpgRFxiSZNIDq68KNJk+DGMWzYLVx7bV8mTZpIixaNaNGiEVB46OTdd5OIj29PrVpViI2tTa9eF5Gamur1nkOHDmTAgOvyHcvMzKRFi8Y8//xkAKy1PPvsP2nTpgW1alWhc+ezePvtt3Kv37VrF9HRhjlzZnHZZRdTq1YVpk9/md9//51bbx1K48b1qFkzmjPOaM4LL/wr9/MKDiEdPnyYe+8dQdOmsdSsGU2HDm2YN29OvvfVufNZ1KgRRYsWjXn66ScpatPb3377jdtvv5mYmFOpVasKV1zRi++/35x7/s033+C006qxZMliOnVqR/Xqlfnhhy1FfQvEpXJ2mrbWMm/jPH458ovTIUU
"text/plain": [
"<Figure size 576x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"# extra code this cell generates and saves Figure 68\n",
2017-06-02 09:43:50 +02:00
"\n",
"plt.figure(figsize=(8, 4))\n",
"\n",
"axes = [-2.2, 2.4, -0.6, 0.7]\n",
"z0s, z1s = np.meshgrid(np.linspace(axes[0], axes[1], 100),\n",
" np.linspace(axes[2], axes[3], 100))\n",
"X_iris_pca_all = np.c_[z0s.ravel(), z1s.ravel()]\n",
"y_pred = tree_clf_pca.predict(X_iris_pca_all).reshape(z0s.shape)\n",
"\n",
"plt.contourf(z0s, z1s, y_pred, alpha=0.3, cmap=custom_cmap)\n",
"for idx, (name, style) in enumerate(zip(iris.target_names, (\"yo\", \"bs\", \"g^\"))):\n",
" plt.plot(X_iris_rotated[:, 0][y_iris == idx],\n",
" X_iris_rotated[:, 1][y_iris == idx],\n",
" style, label=f\"Iris {name}\")\n",
"\n",
"plt.xlabel(\"$z_1$\")\n",
"plt.ylabel(\"$z_2$\", rotation=0)\n",
"th1, th2 = tree_clf_pca.tree_.threshold[[0, 2]]\n",
"plt.plot([th1, th1], axes[2:], \"k-\", linewidth=2)\n",
"plt.plot([th2, th2], axes[2:], \"k--\", linewidth=2)\n",
"plt.text(th1 - 0.01, axes[2] + 0.05, \"Depth=0\",\n",
" horizontalalignment=\"right\", fontsize=15)\n",
"plt.text(th2 - 0.01, axes[2] + 0.05, \"Depth=1\",\n",
" horizontalalignment=\"right\", fontsize=13)\n",
"plt.axis(axes)\n",
"plt.legend(loc=(0.32, 0.67))\n",
"save_fig(\"pca_preprocessing_plot\")\n",
"\n",
"plt.show()"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Decision Trees Have High Variance"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We've seen that small changes in the dataset (such as a rotation) may produce a very different Decision Tree.\n",
"Now let's show that training the same model on the same data may produce a very different model every time, since the CART training algorithm used by Scikit-Learn is stochastic. To show this, we will set `random_state` to a different value than earlier:"
]
},
2016-09-27 23:31:21 +02:00
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"DecisionTreeClassifier(max_depth=2, random_state=40)"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"tree_clf_tweaked = DecisionTreeClassifier(max_depth=2, random_state=40)\n",
"tree_clf_tweaked.fit(X_iris, y_iris)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjAAAAEQCAYAAACutU7EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAABDuUlEQVR4nO3dd3xUVfrH8c9DgFBDDQTpIEWQXlQEVikq2BBZEMRdsaAiou66KoplbbiKIlhQbOhvFSsgCnbsSAcLIEiTHoqL9FByfn/MJCZhMiVMyUy+79frvpi55dznXtA8Ofc855pzDhEREZF4UizWAYiIiIiESgmMiIiIxB0lMCIiIhJ3lMCIiIhI3FECIyIiInFHCYyIiIjEnaglMGZWyszmmdkPZrbUzP7tYx8zs/FmtsrMfjSzttGKT0REROJH8SieKwPo5pzba2YlgG/N7EPn3Jwc+/QCGnmXU4AJ3j9FREREskWtB8Z57PV+LeFd8s6idyHwqnffOUBFM6sRrRhFREQkPkR1DIyZJZnZEmAb8Klzbm6eXWoCG3J83+hdJyIiIpItmo+QcM4dBVqbWUVgqpmd7Jz7Occu5uuwvCvMbCgwFKBM2TLtGjZtGIlwRUREJIZ+WvjTDudcqq9tUU1gsjjndpnZl8A5QM4EZiNQO8f3WsBmH8dPBCYCtGzf0s2YNyNywYqIiEhM1Emq81t+26JZhZTq7XnBzEoDPYBf8uw2HfibtxrpVOAP59yWaMUoIiIi8SGaPTA1gFfMLAlP4vSWc+4DM7sWwDn3LDAT6A2sAvYDQ6IYn4iIiMSJqCUwzrkfgTY+1j+b47MDro9WTCIiIhKfNBOviIi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s_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# extra code this cell generates and saves Figure 69\n",
2016-09-27 23:31:21 +02:00
"\n",
"plt.figure(figsize=(8, 4))\n",
"y_pred = tree_clf_tweaked.predict(X_iris_all).reshape(lengths.shape)\n",
"plt.contourf(lengths, widths, y_pred, alpha=0.3, cmap=custom_cmap)\n",
"\n",
"for idx, (name, style) in enumerate(zip(iris.target_names, (\"yo\", \"bs\", \"g^\"))):\n",
" plt.plot(X_iris[:, 0][y_iris == idx], X_iris[:, 1][y_iris == idx],\n",
" style, label=f\"Iris {name}\")\n",
"\n",
"th0, th1 = tree_clf_tweaked.tree_.threshold[[0, 2]]\n",
"plt.plot([0, 7.2], [th0, th0], \"k-\", linewidth=2)\n",
"plt.plot([0, 7.2], [th1, th1], \"k--\", linewidth=2)\n",
"plt.text(1.8, th0 + 0.05, \"Depth=0\", verticalalignment=\"bottom\", fontsize=15)\n",
"plt.text(2.3, th1 + 0.05, \"Depth=1\", verticalalignment=\"bottom\", fontsize=13)\n",
"plt.xlabel(\"Petal length (cm)\")\n",
"plt.ylabel(\"Petal width (cm)\")\n",
"plt.axis([0, 7.2, 0, 3])\n",
"plt.legend()\n",
"save_fig(\"decision_tree_high_variance_plot\")\n",
2016-09-27 23:31:21 +02:00
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
2016-09-27 23:31:21 +02:00
"source": [
"# Extra Material Accessing the tree structure"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
2016-09-27 23:31:21 +02:00
"source": [
"A trained `DecisionTreeClassifier` has a `tree_` attribute that stores the tree's structure:"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"<sklearn.tree._tree.Tree at 0x7fbfa8b563b0>"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"tree = tree_clf.tree_\n",
"tree"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
2016-09-27 23:31:21 +02:00
"source": [
"You can get the total number of nodes in the tree:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.node_count"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
2016-09-27 23:31:21 +02:00
"source": [
"And other self-explanatory attributes are available:"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"tree.max_depth"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.max_n_classes"
]
},
2016-09-27 23:31:21 +02:00
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"tree.n_features"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.n_outputs"
]
},
2016-09-27 23:31:21 +02:00
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"tree.n_leaves"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All the information about the nodes is stored in NumPy arrays. For example, the impurity of each node:"
]
},
2016-09-27 23:31:21 +02:00
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([0.66666667, 0. , 0.5 , 0.16803841, 0.04253308])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"tree.impurity"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The root node is at index 0. The left and right children nodes of node _i_ are `tree.children_left[i]` and `tree.children_right[i]`. For example, the children of the root node are:"
]
},
2016-09-27 23:31:21 +02:00
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"(1, 2)"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"tree.children_left[0], tree.children_right[0]"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
2016-09-27 23:31:21 +02:00
"source": [
"When the left and right nodes are equal, it means this is a leaf node (and the children node ids are arbitrary):"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"(-1, -1)"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.children_left[3], tree.children_right[3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So you can get the leaf node ids like this:"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([1, 3, 4])"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"is_leaf = (tree.children_left == tree.children_right)\n",
"np.arange(tree.node_count)[is_leaf]"
2017-06-02 09:43:50 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Non-leaf nodes are called _split nodes_. The feature they split is available via the `feature` array. Values for leaf nodes should be ignored:"
]
},
2017-06-02 09:43:50 +02:00
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, -2, 1, -2, -2], dtype=int64)"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
2017-06-02 09:43:50 +02:00
"source": [
"tree.feature"
2017-06-02 09:43:50 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And the corresponding thresholds are:"
]
},
2017-06-02 09:43:50 +02:00
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 2.44999999, -2. , 1.75 , -2. , -2. ])"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
2017-06-02 09:43:50 +02:00
"source": [
"tree.threshold"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And the number of instances per class that reached each node is available too:"
]
},
2016-09-27 23:31:21 +02:00
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([[[50., 50., 50.]],\n",
"\n",
" [[50., 0., 0.]],\n",
"\n",
" [[ 0., 50., 50.]],\n",
"\n",
" [[ 0., 49., 5.]],\n",
"\n",
" [[ 0., 1., 45.]]])"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"tree.value"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([150, 50, 100, 54, 46], dtype=int64)"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree.n_node_samples"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.all(tree.value.sum(axis=(1, 2)) == tree.n_node_samples)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's how you can compute the depth of each node:"
]
},
2016-09-27 23:31:21 +02:00
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([0., 1., 1., 2., 2.])"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
2016-09-27 23:31:21 +02:00
"source": [
"def compute_depth(tree_clf):\n",
" tree = tree_clf.tree_\n",
" depth = np.zeros(tree.node_count)\n",
" stack = [(0, 0)]\n",
" while stack:\n",
" node, node_depth = stack.pop()\n",
" depth[node] = node_depth\n",
" if tree.children_left[node] != tree.children_right[node]:\n",
" stack.append((tree.children_left[node], node_depth + 1))\n",
" stack.append((tree.children_right[node], node_depth + 1))\n",
" return depth\n",
2016-09-27 23:31:21 +02:00
"\n",
"depth = compute_depth(tree_clf)\n",
"depth"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's how to get the thresholds of all split nodes at depth 1:"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([1], dtype=int64)"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree_clf.tree_.feature[(depth == 1) & (~is_leaf)]"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"array([1.75])"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree_clf.tree_.threshold[(depth == 1) & (~is_leaf)]"
2016-09-27 23:31:21 +02:00
]
},
{
"cell_type": "markdown",
2020-04-06 09:13:12 +02:00
"metadata": {},
2016-09-27 23:31:21 +02:00
"source": [
"# Exercise solutions"
]
},
{
"cell_type": "markdown",
"metadata": {},
2016-09-27 23:31:21 +02:00
"source": [
2017-06-02 09:43:50 +02:00
"## 1. to 6."
2016-09-27 23:31:21 +02:00
]
},
{
2017-06-02 09:43:50 +02:00
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"1. The depth of a well-balanced binary tree containing _m_ leaves is equal to log₂(_m_), rounded up. log₂ is the binary log; log₂(_m_) = log(_m_) / log(2). A binary Decision Tree (one that makes only binary decisions, as is the case with all trees in Scikit-Learn) will end up more or less well balanced at the end of training, with one leaf per training instance if it is trained without restrictions. Thus, if the training set contains one million instances, the Decision Tree will have a depth of log₂(10<sup>6</sup>) ≈ 20 (actually a bit more since the tree will generally not be perfectly well balanced).\n",
"2. A node's Gini impurity is generally lower than its parent's. This is due to the CART training algorithm's cost function, which splits each node in a way that minimizes the weighted sum of its children's Gini impurities. However, it is possible for a node to have a higher Gini impurity than its parent, as long as this increase is more than compensated for by a decrease in the other child's impurity. For example, consider a node containing four instances of class A and one of class B. Its Gini impurity is 1 (1/5)² (4/5)² = 0.32. Now suppose the dataset is one-dimensional and the instances are lined up in the following order: A, B, A, A, A. You can verify that the algorithm will split this node after the second instance, producing one child node with instances A, B, and the other child node with instances A, A, A. The first child node's Gini impurity is 1 (1/2)² (1/2)² = 0.5, which is higher than its parent's. This is compensated for by the fact that the other node is pure, so its overall weighted Gini impurity is 2/5 × 0.5 + 3/5 × 0 = 0.2, which is lower than the parent's Gini impurity.\n",
"3. If a Decision Tree is overfitting the training set, it may be a good idea to decrease `max_depth`, since this will constrain the model, regularizing it.\n",
"4. Decision Trees don't care whether or not the training data is scaled or centered; that's one of the nice things about them. So if a Decision Tree underfits the training set, scaling the input features will just be a waste of time.\n",
"5. The computational complexity of training a Decision Tree is _O_(_n_ × _m_ log₂(_m_)). So if you multiply the training set size by 10, the training time will be multiplied by _K_ = (_n_ × 10 _m_ × log₂(10 _m_)) / (_n_ × _m_ × log₂(_m_)) = 10 × log₂(10 _m_) / log₂(_m_). If _m_ = 10<sup>6</sup>, then _K_ ≈ 11.7, so you can expect the training time to be roughly 11.7 hours.\n",
"6. If the number of features doubles, then the training time will also roughly double."
2017-06-02 09:43:50 +02:00
]
},
{
"cell_type": "markdown",
2020-04-06 09:13:12 +02:00
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"## 7."
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"_Exercise: train and fine-tune a Decision Tree for the moons dataset._"
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"a. Generate a moons dataset using `make_moons(n_samples=10000, noise=0.4)`."
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"Adding `random_state=42` to make this notebook's output constant:"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
2017-06-02 09:43:50 +02:00
"outputs": [],
"source": [
"from sklearn.datasets import make_moons\n",
"\n",
"X_moons, y_moons = make_moons(n_samples=10000, noise=0.4, random_state=42)"
2017-06-02 09:43:50 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"b. Split it into a training set and a test set using `train_test_split()`."
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
2017-06-02 09:43:50 +02:00
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X_moons, y_moons,\n",
" test_size=0.2,\n",
" random_state=42)"
2017-06-02 09:43:50 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"c. Use grid search with cross-validation (with the help of the `GridSearchCV` class) to find good hyperparameter values for a `DecisionTreeClassifier`. Hint: try various values for `max_leaf_nodes`."
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"GridSearchCV(cv=3, estimator=DecisionTreeClassifier(random_state=42),\n",
" param_grid={'max_depth': [1, 2, 3, 4, 5, 6],\n",
" 'max_leaf_nodes': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,\n",
" 13, 14, 15, 16, 17, 18, 19, 20, 21,\n",
" 22, 23, 24, 25, 26, 27, 28, 29, 30,\n",
" 31, ...],\n",
" 'min_samples_split': [2, 3, 4]})"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
2017-06-02 09:43:50 +02:00
"source": [
"from sklearn.model_selection import GridSearchCV\n",
"\n",
"params = {\n",
" 'max_leaf_nodes': list(range(2, 100)),\n",
" 'max_depth': list(range(1, 7)),\n",
" 'min_samples_split': [2, 3, 4]\n",
"}\n",
"grid_search_cv = GridSearchCV(DecisionTreeClassifier(random_state=42),\n",
" params,\n",
" cv=3)\n",
2017-06-02 09:43:50 +02:00
"\n",
"grid_search_cv.fit(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"DecisionTreeClassifier(max_depth=6, max_leaf_nodes=17, random_state=42)"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
2017-06-02 09:43:50 +02:00
"source": [
"grid_search_cv.best_estimator_"
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"d. Train it on the full training set using these hyperparameters, and measure your model's performance on the test set. You should get roughly 85% to 87% accuracy."
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"By default, `GridSearchCV` trains the best model found on the whole training set (you can change this by setting `refit=False`), so we don't need to do it again. We can simply evaluate the model's accuracy:"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"0.8595"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
2017-06-02 09:43:50 +02:00
"source": [
"from sklearn.metrics import accuracy_score\n",
"\n",
"y_pred = grid_search_cv.predict(X_test)\n",
"accuracy_score(y_test, y_pred)"
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"## 8."
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"_Exercise: Grow a forest._"
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"a. Continuing the previous exercise, generate 1,000 subsets of the training set, each containing 100 instances selected randomly. Hint: you can use Scikit-Learn's `ShuffleSplit` class for this."
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
2017-06-02 09:43:50 +02:00
"outputs": [],
"source": [
"from sklearn.model_selection import ShuffleSplit\n",
"\n",
"n_trees = 1000\n",
"n_instances = 100\n",
"\n",
"mini_sets = []\n",
"\n",
"rs = ShuffleSplit(n_splits=n_trees, test_size=len(X_train) - n_instances,\n",
" random_state=42)\n",
"\n",
2017-06-02 09:43:50 +02:00
"for mini_train_index, mini_test_index in rs.split(X_train):\n",
" X_mini_train = X_train[mini_train_index]\n",
" y_mini_train = y_train[mini_train_index]\n",
" mini_sets.append((X_mini_train, y_mini_train))"
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"b. Train one Decision Tree on each subset, using the best hyperparameter values found above. Evaluate these 1,000 Decision Trees on the test set. Since they were trained on smaller sets, these Decision Trees will likely perform worse than the first Decision Tree, achieving only about 80% accuracy."
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"0.805671"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
2017-06-02 09:43:50 +02:00
"source": [
"from sklearn.base import clone\n",
"\n",
"forest = [clone(grid_search_cv.best_estimator_) for _ in range(n_trees)]\n",
"\n",
"accuracy_scores = []\n",
"\n",
"for tree, (X_mini_train, y_mini_train) in zip(forest, mini_sets):\n",
" tree.fit(X_mini_train, y_mini_train)\n",
" \n",
" y_pred = tree.predict(X_test)\n",
" accuracy_scores.append(accuracy_score(y_test, y_pred))\n",
"\n",
"np.mean(accuracy_scores)"
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"c. Now comes the magic. For each test set instance, generate the predictions of the 1,000 Decision Trees, and keep only the most frequent prediction (you can use SciPy's `mode()` function for this). This gives you _majority-vote predictions_ over the test set."
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
2017-06-02 09:43:50 +02:00
"outputs": [],
"source": [
"Y_pred = np.empty([n_trees, len(X_test)], dtype=np.uint8)\n",
"\n",
"for tree_index, tree in enumerate(forest):\n",
" Y_pred[tree_index] = tree.predict(X_test)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
2017-06-02 09:43:50 +02:00
"outputs": [],
"source": [
"from scipy.stats import mode\n",
"\n",
"y_pred_majority_votes, n_votes = mode(Y_pred, axis=0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
2017-06-02 09:43:50 +02:00
"source": [
"d. Evaluate these predictions on the test set: you should obtain a slightly higher accuracy than your first model (about 0.5 to 1.5% higher). Congratulations, you have trained a Random Forest classifier!"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
2022-02-19 10:24:54 +01:00
"outputs": [
{
"data": {
"text/plain": [
"0.873"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
2017-06-02 09:43:50 +02:00
"source": [
"accuracy_score(y_test, y_pred_majority_votes.reshape([-1]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
2016-09-27 23:31:21 +02:00
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
2016-09-27 23:31:21 +02:00
"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.10.6"
2016-09-27 23:31:21 +02:00
},
"nav_menu": {
"height": "309px",
"width": "468px"
},
"toc": {
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 6,
"toc_cell": false,
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
2020-04-06 09:13:12 +02:00
"nbformat_minor": 4
2016-09-27 23:31:21 +02:00
}