mirror of
https://github.com/ArthurDanjou/handson-ml3.git
synced 2026-01-14 12:14:36 +01:00
Replace handson-ml2 with handson-ml3, and fix figure chapter numbers
This commit is contained in:
@@ -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 8–1\n",
|
||||
"# not in the book – this code generates and saves Figure 9–1\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 8–2\n",
|
||||
"# not in the book – this code generates and saves Figure 9–2\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 8–3\n",
|
||||
"# not in the book – this code generates and saves Figure 9–3\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 8–4\n",
|
||||
"# not in the book – this code generates and saves Figure 9–4\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 8–5\n",
|
||||
"# not in the book – this code generates and saves Figure 9–5\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 8–6\n",
|
||||
"# not in the book – this code generates and saves Figure 9–6\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 8–7\n",
|
||||
"# not in the book – this code generates and saves Figure 9–7\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 8–8\n",
|
||||
"# not in the book – this code generates and saves Figure 9–8\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 8–9\n",
|
||||
"# not in the book – this code generates and saves Figure 9–9\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 8–10\n",
|
||||
"# not in the book – this code generates and saves Figure 9–10\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 8–11\n",
|
||||
"# not in the book – this code generates and saves Figure 9–11\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 8–12\n",
|
||||
"# not in the book – this code generates and saves Figure 9–12\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 8–13\n",
|
||||
"# not in the book – this cell generates and saves Figure 9–13\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 8–14\n",
|
||||
"# not in the book – this cell generates and saves Figure 9–14\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 8–15\n",
|
||||
"# not in the book – this cell generates and saves Figure 9–15\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 8–16\n",
|
||||
"# not in the book – this cells generates and saves Figure 9–16\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 8–17\n",
|
||||
"# not in the book – this code generates and saves Figure 9–17\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 8–18\n",
|
||||
"# not in the book – this code generates and saves Figure 9–18\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 8–19\n",
|
||||
"# not in the book – this cell generates and saves Figure 9–19\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 8–20\n",
|
||||
"# not in the book – this cell generates and saves Figure 9–20\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 8–16\n",
|
||||
"# not in the book – this figure is almost identical to Figure 9–16\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 8–21\n",
|
||||
"# not in the book – this cell generates and saves Figure 9–21\n",
|
||||
"\n",
|
||||
"X_moons, y_moons = make_moons(n_samples=1000, noise=0.05, random_state=42)\n",
|
||||
"\n",
|
||||
|
||||
Reference in New Issue
Block a user