Sync notebook with book's code examples, and better identify extra code

This commit is contained in:
Aurélien Geron
2022-02-19 18:17:36 +13:00
parent 1c2421fc88
commit b63019fd28
9 changed files with 318 additions and 301 deletions

View File

@@ -193,7 +193,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book\n",
"# extra code\n",
"!dot -Tpng {IMAGES_PATH / \"iris_tree.dot\"} -o {IMAGES_PATH / \"iris_tree.png\"}"
]
},
@@ -213,7 +213,7 @@
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# not in the book just formatting details\n",
"# extra code just formatting details\n",
"from matplotlib.colors import ListedColormap\n",
"custom_cmap = ListedColormap(['#fafab0', '#9898ff', '#a0faa0'])\n",
"plt.figure(figsize=(8, 4))\n",
@@ -226,7 +226,7 @@
" plt.plot(X_iris[:, 0][y_iris == idx], X_iris[:, 1][y_iris == idx],\n",
" style, label=f\"Iris {name}\")\n",
"\n",
"# not in the book this section beautifies and saves Figure 62\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",
@@ -341,7 +341,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 63\n",
"# 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",
@@ -437,7 +437,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book we've already seen how to use export_graphviz()\n",
"# 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",
@@ -482,7 +482,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 65\n",
"# 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",
@@ -526,7 +526,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 66\n",
"# extra code this cell generates and saves Figure 66\n",
"\n",
"tree_reg1 = DecisionTreeRegressor(random_state=42)\n",
"tree_reg2 = DecisionTreeRegressor(random_state=42, min_samples_leaf=10)\n",
@@ -579,7 +579,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 67\n",
"# 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",
@@ -630,7 +630,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 68\n",
"# extra code this cell generates and saves Figure 68\n",
"\n",
"plt.figure(figsize=(8, 4))\n",
"\n",
@@ -693,7 +693,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 69\n",
"# extra code this cell generates and saves Figure 69\n",
"\n",
"plt.figure(figsize=(8, 4))\n",
"y_pred = tree_clf_tweaked.predict(X_iris_all).reshape(lengths.shape)\n",