Replace handson-ml2 with handson-ml3, and fix figure chapter numbers

This commit is contained in:
Aurélien Geron
2021-11-23 15:42:16 +13:00
parent e38983d595
commit 5bb0366125
23 changed files with 137 additions and 137 deletions

View File

@@ -4,14 +4,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Chapter 8 Unsupervised Learning**"
"**Chapter 9 Unsupervised Learning**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"_This notebook contains all the sample code and solutions to the exercises in chapter 8._"
"_This notebook contains all the sample code and solutions to the exercises in chapter 9._"
]
},
{
@@ -20,10 +20,10 @@
"source": [
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/ageron/handson-ml2/blob/master/09_unsupervised_learning.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
" <a href=\"https://colab.research.google.com/github/ageron/handson-ml3/blob/main/09_unsupervised_learning.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-ml2/blob/master/09_unsupervised_learning.ipynb\"><img src=\"https://kaggle.com/static/images/open-in-kaggle.svg\" /></a>\n",
" <a target=\"_blank\" href=\"https://kaggle.com/kernels/welcome?src=https://github.com/ageron/handson-ml3/blob/main/09_unsupervised_learning.ipynb\"><img src=\"https://kaggle.com/static/images/open-in-kaggle.svg\" /></a>\n",
" </td>\n",
"</table>"
]
@@ -145,7 +145,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 81\n",
"# not in the book this code generates and saves Figure 91\n",
"\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.datasets import load_iris\n",
@@ -286,7 +286,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 82\n",
"# not in the book this code generates and saves Figure 92\n",
"\n",
"def plot_clusters(X, y=None):\n",
" plt.scatter(X[:, 0], X[:, 1], c=y, s=1)\n",
@@ -397,7 +397,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 83\n",
"# not in the book this code generates and saves Figure 93\n",
"\n",
"def plot_data(X):\n",
" plt.plot(X[:, 0], X[:, 1], 'k.', markersize=2)\n",
@@ -530,7 +530,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 84\n",
"# not in the book this code generates and saves Figure 94\n",
"\n",
"kmeans_iter1 = KMeans(n_clusters=5, init=\"random\", n_init=1, max_iter=1,\n",
" random_state=5)\n",
@@ -598,7 +598,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 85\n",
"# not in the book this code generates and saves Figure 95\n",
"\n",
"def plot_clusterer_comparison(clusterer1, clusterer2, X, title1=None,\n",
" title2=None):\n",
@@ -962,7 +962,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 86\n",
"# not in the book this code generates and saves Figure 96\n",
"\n",
"from timeit import timeit\n",
"\n",
@@ -1022,7 +1022,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 87\n",
"# not in the book this code generates and saves Figure 97\n",
"\n",
"kmeans_k3 = KMeans(n_clusters=3, random_state=42)\n",
"kmeans_k8 = KMeans(n_clusters=8, random_state=42)\n",
@@ -1070,7 +1070,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 88\n",
"# not in the book this code generates and saves Figure 98\n",
"\n",
"kmeans_per_k = [KMeans(n_clusters=k, random_state=42).fit(X)\n",
" for k in range(1, 10)]\n",
@@ -1145,7 +1145,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 89\n",
"# not in the book this code generates and saves Figure 99\n",
"\n",
"silhouette_scores = [silhouette_score(X, model.labels_)\n",
" for model in kmeans_per_k[1:]]\n",
@@ -1180,7 +1180,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 810\n",
"# not in the book this code generates and saves Figure 910\n",
"\n",
"from sklearn.metrics import silhouette_samples\n",
"from matplotlib.ticker import FixedLocator, FixedFormatter\n",
@@ -1251,7 +1251,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 811\n",
"# not in the book this code generates and saves Figure 911\n",
"\n",
"X1, y1 = make_blobs(n_samples=1000, centers=((4, -4), (0, 0)), random_state=42)\n",
"X1 = X1.dot(np.array([[0.374, 0.95], [0.732, 0.598]]))\n",
@@ -1303,7 +1303,7 @@
"source": [
"# not in the book\n",
"\n",
"root = \"https://raw.githubusercontent.com/ageron/handson-ml2/master/\"\n",
"root = \"https://raw.githubusercontent.com/ageron/handson-ml3/main/\"\n",
"filename = \"ladybug.png\"\n",
"filepath = IMAGES_PATH / filename\n",
"if not filepath.is_file():\n",
@@ -1342,7 +1342,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 812\n",
"# not in the book this code generates and saves Figure 912\n",
"\n",
"segmented_imgs = []\n",
"n_colors = (10, 8, 6, 4, 2)\n",
@@ -1477,7 +1477,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 813\n",
"# not in the book this cell generates and saves Figure 913\n",
"\n",
"plt.figure(figsize=(8, 2))\n",
"for index, X_representative_digit in enumerate(X_representative_digits):\n",
@@ -1692,7 +1692,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 814\n",
"# not in the book this cell generates and saves Figure 914\n",
"\n",
"def plot_dbscan(dbscan, X, size, show_xlabels=True, show_ylabels=True):\n",
" core_mask = np.zeros_like(dbscan.labels_, dtype=bool)\n",
@@ -1785,7 +1785,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 815\n",
"# not in the book this cell generates and saves Figure 915\n",
"\n",
"plt.figure(figsize=(6, 3))\n",
"plot_decision_boundaries(knn, X, show_centroids=False)\n",
@@ -2195,7 +2195,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cells generates and saves Figure 816\n",
"# not in the book this cells generates and saves Figure 916\n",
"\n",
"from matplotlib.colors import LogNorm\n",
"\n",
@@ -2254,7 +2254,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 817\n",
"# not in the book this code generates and saves Figure 917\n",
"\n",
"gm_full = GaussianMixture(n_components=3, n_init=10,\n",
" covariance_type=\"full\", random_state=42)\n",
@@ -2329,7 +2329,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this code generates and saves Figure 818\n",
"# not in the book this code generates and saves Figure 918\n",
"\n",
"plt.figure(figsize=(8, 4))\n",
"\n",
@@ -2371,7 +2371,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 819\n",
"# not in the book this cell generates and saves Figure 919\n",
"\n",
"from scipy.stats import norm\n",
"\n",
@@ -2512,7 +2512,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 820\n",
"# not in the book this cell generates and saves Figure 920\n",
"\n",
"gms_per_k = [GaussianMixture(n_components=k, n_init=10, random_state=42).fit(X)\n",
" for k in range(1, 11)]\n",
@@ -2574,7 +2574,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this figure is almost identical to Figure 816\n",
"# not in the book this figure is almost identical to Figure 916\n",
"plt.figure(figsize=(8, 5))\n",
"plot_gaussian_mixture(bgm, X)\n",
"plt.show()"
@@ -2586,7 +2586,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 821\n",
"# not in the book this cell generates and saves Figure 921\n",
"\n",
"X_moons, y_moons = make_moons(n_samples=1000, noise=0.05, random_state=42)\n",
"\n",