From 6465bc9907a474bede2f0456dc6b79702b07bff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Sun, 21 Nov 2021 18:04:07 +1300 Subject: [PATCH] Clarify the 'not in the book' comments --- 07_dimensionality_reduction.ipynb | 91 ++++++------------------------- 1 file changed, 18 insertions(+), 73 deletions(-) diff --git a/07_dimensionality_reduction.ipynb b/07_dimensionality_reduction.ipynb index ebb5ff7..a73ede3 100644 --- a/07_dimensionality_reduction.ipynb +++ b/07_dimensionality_reduction.ipynb @@ -145,7 +145,7 @@ "metadata": {}, "outputs": [], "source": [ - "# not in the book\n", + "# not in the book – we've generated plenty of datasets before with similar code\n", "\n", "import numpy as np\n", "\n", @@ -160,13 +160,6 @@ "X += [0.2, 0, 0.2]" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Code to generate Figure 8–2. A 3D dataset lying close to a 2D subspace:**" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -182,7 +175,7 @@ }, "outputs": [], "source": [ - "# not in the book\n", + "# not in the book – this code generates and saves Figure 7–2\n", "\n", "import matplotlib.pyplot as plt\n", "from mpl_toolkits.mplot3d import Axes3D\n", @@ -244,20 +237,13 @@ "plt.show()" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Code to generate Figure 8–3. The new 2D dataset after projection:**" - ] - }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "# not in the book\n", + "# not in the book – this code generates and saves Figure 7–3\n", "\n", "fig = plt.figure()\n", "ax = fig.add_subplot(1, 1, 1, aspect='equal')\n", @@ -276,13 +262,6 @@ "save_fig(\"dataset_2d_plot\")" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Code to generate Figure 8–4. Swiss roll dataset:**" - ] - }, { "cell_type": "code", "execution_count": 8, @@ -300,7 +279,7 @@ "metadata": {}, "outputs": [], "source": [ - "# not in the book\n", + "# not in the book – this code generates and saves Figure 7–4\n", "\n", "from matplotlib.colors import ListedColormap\n", "\n", @@ -318,19 +297,14 @@ "plt.show()" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Code to generate Figure 8–5. Squashing by projecting onto a plane (left) versus unrolling the Swiss roll (right):**" - ] - }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ + "# not in the book – this code generates and saves plots for Figure 7–5\n", + "\n", "plt.figure(figsize=(10, 4))\n", "\n", "plt.subplot(121)\n", @@ -350,19 +324,14 @@ "plt.show()" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Code to generate Figure 8–6. The decision boundary may not always be simpler with lower dimensions:**" - ] - }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ + "# not in the book – this code generates and saves plots for Figure 7–6\n", + " \n", "axes = [-11.5, 14, -2, 23, -12, 15]\n", "x2s = np.linspace(axes[2], axes[3], 10)\n", "x3s = np.linspace(axes[4], axes[5], 10)\n", @@ -427,20 +396,13 @@ "plt.show()" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Code to generate Figure 8–7. Selecting the subspace to project on:**" - ] - }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ - "# not in the book\n", + "# not in the book – this code generates and saves Figure 7–7\n", "\n", "angle = np.pi / 5\n", "stretch = 5\n", @@ -545,7 +507,7 @@ "metadata": {}, "outputs": [], "source": [ - "# not in the book\n", + "# not in the book – this code shows how to construct Σ from s\n", "m, n = X.shape\n", "Σ = np.zeros_like(X_centered)\n", "Σ[:n, :n] = np.diag(s)\n", @@ -827,20 +789,13 @@ "X_recovered = pca.inverse_transform(X_reduced)" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Code to generate Figure 8–9. MNIST compression that preserves 95% of the variance:**" - ] - }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ - "# not in the book\n", + "# not in the book – this cell generates and saves Figure 7–9\n", "\n", "plt.figure(figsize=(7, 4))\n", "for idx, X in enumerate((X_train[::2100], X_recovered[::2100])):\n", @@ -974,7 +929,7 @@ "metadata": {}, "outputs": [], "source": [ - "# not in the book\n", + "# not in the book – show the equation computed by johnson_lindenstrauss_min_dim\n", "d = int(4 * np.log(m) / (ε ** 2 / 2 - ε ** 3 / 3))\n", "d" ] @@ -1028,7 +983,7 @@ "metadata": {}, "outputs": [], "source": [ - "# not in the book, performance comparison between Gaussian and Sparse RP\n", + "# not in the book – performance comparison between Gaussian and Sparse RP\n", "\n", "from sklearn.random_projection import SparseRandomProjection\n", "\n", @@ -1066,19 +1021,14 @@ "X_unrolled = lle.fit_transform(X_swiss)" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Code to generate Figure 8–12. Unrolled Swiss roll using LLE:**" - ] - }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ + "# not in the book – this cell generates and saves Figure 7–10\n", + "\n", "plt.title(\"Unrolled swiss roll using LLE\")\n", "plt.scatter(X_unrolled[:, 0], X_unrolled[:, 1],\n", " c=t, cmap=darker_hot)\n", @@ -1097,7 +1047,7 @@ "metadata": {}, "outputs": [], "source": [ - "# not in the book: shows how well correlated z1 is to t: LLE worked fine\n", + "# not in the book – shows how well correlated z1 is to t: LLE worked fine\n", "plt.title(\"$z_1$ vs $t$\")\n", "plt.scatter(X_unrolled[:, 0], t, c=t, cmap=darker_hot)\n", "plt.xlabel(\"$z_1$\")\n", @@ -1143,19 +1093,14 @@ "X_reduced_tsne = tsne.fit_transform(X_swiss)" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Code to generate Figure 8–13. Using various techniques to reduce the Swill roll to 2D:**" - ] - }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ + "# not in the book – this cell generates and saves Figure 7–11\n", + "\n", "titles = [\"MDS\", \"Isomap\", \"t-SNE\"]\n", "\n", "plt.figure(figsize=(11,4))\n",