Make notebooks 1 to 9 runnable in Colab without changes

This commit is contained in:
Aurélien Geron
2019-11-05 22:26:52 +08:00
parent 7e35fdc3c4
commit a55720e9e4
9 changed files with 400 additions and 254 deletions

View File

@@ -9,6 +9,17 @@
"_This notebook contains all the sample code in chapter 9._"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/ageron/handson-ml2/blob/master/09_unsupervised_learning.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
@@ -1439,9 +1450,14 @@
"metadata": {},
"outputs": [],
"source": [
"from matplotlib.image import imread\n",
"image = imread(os.path.join(\"images\",\"unsupervised_learning\",\"ladybug.png\"))\n",
"image.shape"
"# Download the ladybug image\n",
"images_path = os.path.join(PROJECT_ROOT_DIR, \"images\", \"unsupervised_learning\")\n",
"os.makedirs(images_path, exist_ok=True)\n",
"DOWNLOAD_ROOT = \"https://raw.githubusercontent.com/ageron/handson-ml2/master/\"\n",
"filename = \"ladybug.png\"\n",
"print(\"Downloading\", filename)\n",
"url = DOWNLOAD_ROOT + \"images/unsupervised_learning/\" + filename\n",
"urllib.request.urlretrieve(url, os.path.join(images_path, filename))"
]
},
{
@@ -1449,6 +1465,17 @@
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib.image import imread\n",
"image = imread(os.path.join(images_path, filename))\n",
"image.shape"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"X = image.reshape(-1, 3)\n",
"kmeans = KMeans(n_clusters=8, random_state=42).fit(X)\n",
@@ -1458,7 +1485,7 @@
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
@@ -1472,7 +1499,7 @@
},
{
"cell_type": "code",
"execution_count": 75,
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
@@ -1510,7 +1537,7 @@
},
{
"cell_type": "code",
"execution_count": 76,
"execution_count": 77,
"metadata": {},
"outputs": [],
"source": [
@@ -1519,7 +1546,7 @@
},
{
"cell_type": "code",
"execution_count": 77,
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
@@ -1535,7 +1562,7 @@
},
{
"cell_type": "code",
"execution_count": 78,
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
@@ -1544,7 +1571,7 @@
},
{
"cell_type": "code",
"execution_count": 79,
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
@@ -1560,7 +1587,7 @@
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
@@ -1569,7 +1596,7 @@
},
{
"cell_type": "code",
"execution_count": 81,
"execution_count": 82,
"metadata": {},
"outputs": [],
"source": [
@@ -1579,7 +1606,7 @@
},
{
"cell_type": "code",
"execution_count": 82,
"execution_count": 83,
"metadata": {},
"outputs": [],
"source": [
@@ -1595,7 +1622,7 @@
},
{
"cell_type": "code",
"execution_count": 83,
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
@@ -1604,7 +1631,7 @@
},
{
"cell_type": "code",
"execution_count": 84,
"execution_count": 85,
"metadata": {},
"outputs": [],
"source": [
@@ -1617,7 +1644,7 @@
},
{
"cell_type": "code",
"execution_count": 85,
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
@@ -1626,7 +1653,7 @@
},
{
"cell_type": "code",
"execution_count": 86,
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
@@ -1642,7 +1669,7 @@
},
{
"cell_type": "code",
"execution_count": 87,
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
@@ -1651,7 +1678,7 @@
},
{
"cell_type": "code",
"execution_count": 88,
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
@@ -1662,7 +1689,7 @@
},
{
"cell_type": "code",
"execution_count": 89,
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
@@ -1671,7 +1698,7 @@
},
{
"cell_type": "code",
"execution_count": 90,
"execution_count": 91,
"metadata": {
"scrolled": false
},
@@ -1710,7 +1737,7 @@
},
{
"cell_type": "code",
"execution_count": 91,
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
@@ -1719,7 +1746,7 @@
},
{
"cell_type": "code",
"execution_count": 92,
"execution_count": 93,
"metadata": {},
"outputs": [],
"source": [
@@ -1737,7 +1764,7 @@
},
{
"cell_type": "code",
"execution_count": 93,
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
@@ -1746,7 +1773,7 @@
},
{
"cell_type": "code",
"execution_count": 94,
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
@@ -1765,7 +1792,7 @@
},
{
"cell_type": "code",
"execution_count": 95,
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
@@ -1781,7 +1808,7 @@
},
{
"cell_type": "code",
"execution_count": 96,
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
@@ -1802,7 +1829,7 @@
},
{
"cell_type": "code",
"execution_count": 97,
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
@@ -1827,7 +1854,7 @@
},
{
"cell_type": "code",
"execution_count": 98,
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
@@ -1838,7 +1865,7 @@
},
{
"cell_type": "code",
"execution_count": 99,
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
@@ -1848,7 +1875,7 @@
},
{
"cell_type": "code",
"execution_count": 100,
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
@@ -1864,7 +1891,7 @@
},
{
"cell_type": "code",
"execution_count": 101,
"execution_count": 102,
"metadata": {},
"outputs": [],
"source": [
@@ -1881,7 +1908,7 @@
},
{
"cell_type": "code",
"execution_count": 102,
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
@@ -1892,7 +1919,7 @@
},
{
"cell_type": "code",
"execution_count": 103,
"execution_count": 104,
"metadata": {},
"outputs": [],
"source": [
@@ -1902,7 +1929,7 @@
},
{
"cell_type": "code",
"execution_count": 104,
"execution_count": 105,
"metadata": {},
"outputs": [],
"source": [
@@ -1925,7 +1952,7 @@
},
{
"cell_type": "code",
"execution_count": 105,
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
@@ -1950,7 +1977,7 @@
},
{
"cell_type": "code",
"execution_count": 106,
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
@@ -1959,7 +1986,7 @@
},
{
"cell_type": "code",
"execution_count": 107,
"execution_count": 108,
"metadata": {},
"outputs": [],
"source": [
@@ -1968,7 +1995,7 @@
},
{
"cell_type": "code",
"execution_count": 108,
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
@@ -1977,7 +2004,7 @@
},
{
"cell_type": "code",
"execution_count": 109,
"execution_count": 110,
"metadata": {},
"outputs": [],
"source": [
@@ -1987,7 +2014,7 @@
},
{
"cell_type": "code",
"execution_count": 110,
"execution_count": 111,
"metadata": {},
"outputs": [],
"source": [
@@ -1996,7 +2023,7 @@
},
{
"cell_type": "code",
"execution_count": 111,
"execution_count": 112,
"metadata": {},
"outputs": [],
"source": [
@@ -2005,7 +2032,7 @@
},
{
"cell_type": "code",
"execution_count": 112,
"execution_count": 113,
"metadata": {},
"outputs": [],
"source": [
@@ -2014,7 +2041,7 @@
},
{
"cell_type": "code",
"execution_count": 113,
"execution_count": 114,
"metadata": {},
"outputs": [],
"source": [
@@ -2023,7 +2050,7 @@
},
{
"cell_type": "code",
"execution_count": 114,
"execution_count": 115,
"metadata": {},
"outputs": [],
"source": [
@@ -2032,7 +2059,7 @@
},
{
"cell_type": "code",
"execution_count": 115,
"execution_count": 116,
"metadata": {},
"outputs": [],
"source": [
@@ -2042,7 +2069,7 @@
},
{
"cell_type": "code",
"execution_count": 116,
"execution_count": 117,
"metadata": {},
"outputs": [],
"source": [
@@ -2075,7 +2102,7 @@
},
{
"cell_type": "code",
"execution_count": 117,
"execution_count": 118,
"metadata": {},
"outputs": [],
"source": [
@@ -2093,7 +2120,7 @@
},
{
"cell_type": "code",
"execution_count": 118,
"execution_count": 119,
"metadata": {},
"outputs": [],
"source": [
@@ -2102,7 +2129,7 @@
},
{
"cell_type": "code",
"execution_count": 119,
"execution_count": 120,
"metadata": {},
"outputs": [],
"source": [
@@ -2111,7 +2138,7 @@
},
{
"cell_type": "code",
"execution_count": 120,
"execution_count": 121,
"metadata": {},
"outputs": [],
"source": [
@@ -2121,7 +2148,7 @@
},
{
"cell_type": "code",
"execution_count": 121,
"execution_count": 122,
"metadata": {},
"outputs": [],
"source": [
@@ -2131,7 +2158,7 @@
},
{
"cell_type": "code",
"execution_count": 122,
"execution_count": 123,
"metadata": {},
"outputs": [],
"source": [
@@ -2140,7 +2167,7 @@
},
{
"cell_type": "code",
"execution_count": 123,
"execution_count": 124,
"metadata": {},
"outputs": [],
"source": [
@@ -2153,7 +2180,7 @@
},
{
"cell_type": "code",
"execution_count": 124,
"execution_count": 125,
"metadata": {},
"outputs": [],
"source": [
@@ -2179,7 +2206,7 @@
},
{
"cell_type": "code",
"execution_count": 125,
"execution_count": 126,
"metadata": {},
"outputs": [],
"source": [
@@ -2188,7 +2215,7 @@
},
{
"cell_type": "code",
"execution_count": 126,
"execution_count": 127,
"metadata": {},
"outputs": [],
"source": [
@@ -2198,7 +2225,7 @@
},
{
"cell_type": "code",
"execution_count": 127,
"execution_count": 128,
"metadata": {},
"outputs": [],
"source": [
@@ -2208,7 +2235,7 @@
},
{
"cell_type": "code",
"execution_count": 128,
"execution_count": 129,
"metadata": {},
"outputs": [],
"source": [
@@ -2217,7 +2244,7 @@
},
{
"cell_type": "code",
"execution_count": 129,
"execution_count": 130,
"metadata": {},
"outputs": [],
"source": [
@@ -2239,7 +2266,7 @@
},
{
"cell_type": "code",
"execution_count": 130,
"execution_count": 131,
"metadata": {},
"outputs": [],
"source": [
@@ -2263,7 +2290,7 @@
},
{
"cell_type": "code",
"execution_count": 131,
"execution_count": 132,
"metadata": {},
"outputs": [],
"source": [
@@ -2272,7 +2299,7 @@
},
{
"cell_type": "code",
"execution_count": 132,
"execution_count": 133,
"metadata": {},
"outputs": [],
"source": [
@@ -2282,7 +2309,7 @@
},
{
"cell_type": "code",
"execution_count": 133,
"execution_count": 134,
"metadata": {},
"outputs": [],
"source": [
@@ -2293,7 +2320,7 @@
},
{
"cell_type": "code",
"execution_count": 134,
"execution_count": 135,
"metadata": {},
"outputs": [],
"source": [
@@ -2302,7 +2329,7 @@
},
{
"cell_type": "code",
"execution_count": 135,
"execution_count": 136,
"metadata": {
"scrolled": true
},
@@ -2320,7 +2347,7 @@
},
{
"cell_type": "code",
"execution_count": 136,
"execution_count": 137,
"metadata": {},
"outputs": [],
"source": [
@@ -2341,7 +2368,7 @@
},
{
"cell_type": "code",
"execution_count": 137,
"execution_count": 138,
"metadata": {},
"outputs": [],
"source": [
@@ -2350,7 +2377,7 @@
},
{
"cell_type": "code",
"execution_count": 138,
"execution_count": 139,
"metadata": {},
"outputs": [],
"source": [
@@ -2367,7 +2394,7 @@
},
{
"cell_type": "code",
"execution_count": 139,
"execution_count": 140,
"metadata": {},
"outputs": [],
"source": [
@@ -2376,7 +2403,7 @@
},
{
"cell_type": "code",
"execution_count": 140,
"execution_count": 141,
"metadata": {},
"outputs": [],
"source": [
@@ -2385,7 +2412,7 @@
},
{
"cell_type": "code",
"execution_count": 141,
"execution_count": 142,
"metadata": {},
"outputs": [],
"source": [
@@ -2401,7 +2428,7 @@
},
{
"cell_type": "code",
"execution_count": 142,
"execution_count": 143,
"metadata": {},
"outputs": [],
"source": [
@@ -2417,7 +2444,7 @@
},
{
"cell_type": "code",
"execution_count": 143,
"execution_count": 144,
"metadata": {},
"outputs": [],
"source": [
@@ -2433,7 +2460,7 @@
},
{
"cell_type": "code",
"execution_count": 144,
"execution_count": 145,
"metadata": {},
"outputs": [],
"source": [
@@ -2442,7 +2469,7 @@
},
{
"cell_type": "code",
"execution_count": 145,
"execution_count": 146,
"metadata": {},
"outputs": [],
"source": [
@@ -2458,7 +2485,7 @@
},
{
"cell_type": "code",
"execution_count": 146,
"execution_count": 147,
"metadata": {},
"outputs": [],
"source": [
@@ -2468,7 +2495,7 @@
},
{
"cell_type": "code",
"execution_count": 147,
"execution_count": 148,
"metadata": {},
"outputs": [],
"source": [
@@ -2491,7 +2518,7 @@
},
{
"cell_type": "code",
"execution_count": 148,
"execution_count": 149,
"metadata": {},
"outputs": [],
"source": [
@@ -2507,7 +2534,7 @@
},
{
"cell_type": "code",
"execution_count": 149,
"execution_count": 150,
"metadata": {},
"outputs": [],
"source": [
@@ -2530,7 +2557,7 @@
},
{
"cell_type": "code",
"execution_count": 150,
"execution_count": 151,
"metadata": {},
"outputs": [],
"source": [
@@ -2569,7 +2596,7 @@
},
{
"cell_type": "code",
"execution_count": 151,
"execution_count": 152,
"metadata": {},
"outputs": [],
"source": [
@@ -2594,7 +2621,7 @@
},
{
"cell_type": "code",
"execution_count": 152,
"execution_count": 153,
"metadata": {},
"outputs": [],
"source": [
@@ -2610,7 +2637,7 @@
},
{
"cell_type": "code",
"execution_count": 153,
"execution_count": 154,
"metadata": {},
"outputs": [],
"source": [
@@ -2628,7 +2655,7 @@
},
{
"cell_type": "code",
"execution_count": 154,
"execution_count": 155,
"metadata": {},
"outputs": [],
"source": [
@@ -2640,7 +2667,7 @@
},
{
"cell_type": "code",
"execution_count": 155,
"execution_count": 156,
"metadata": {},
"outputs": [],
"source": [
@@ -2665,7 +2692,7 @@
},
{
"cell_type": "code",
"execution_count": 156,
"execution_count": 157,
"metadata": {},
"outputs": [],
"source": [
@@ -2676,7 +2703,7 @@
},
{
"cell_type": "code",
"execution_count": 157,
"execution_count": 158,
"metadata": {},
"outputs": [],
"source": [
@@ -2716,7 +2743,7 @@
},
{
"cell_type": "code",
"execution_count": 158,
"execution_count": 159,
"metadata": {},
"outputs": [],
"source": [
@@ -2725,7 +2752,7 @@
},
{
"cell_type": "code",
"execution_count": 159,
"execution_count": 160,
"metadata": {},
"outputs": [],
"source": [
@@ -2741,7 +2768,7 @@
},
{
"cell_type": "code",
"execution_count": 160,
"execution_count": 161,
"metadata": {},
"outputs": [],
"source": [
@@ -2758,7 +2785,7 @@
},
{
"cell_type": "code",
"execution_count": 161,
"execution_count": 162,
"metadata": {},
"outputs": [],
"source": [
@@ -2767,7 +2794,7 @@
},
{
"cell_type": "code",
"execution_count": 162,
"execution_count": 163,
"metadata": {},
"outputs": [],
"source": [
@@ -2790,7 +2817,7 @@
},
{
"cell_type": "code",
"execution_count": 163,
"execution_count": 164,
"metadata": {},
"outputs": [],
"source": [
@@ -2800,7 +2827,7 @@
},
{
"cell_type": "code",
"execution_count": 164,
"execution_count": 165,
"metadata": {},
"outputs": [],
"source": [
@@ -2810,7 +2837,7 @@
},
{
"cell_type": "code",
"execution_count": 165,
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
@@ -2841,7 +2868,7 @@
},
{
"cell_type": "code",
"execution_count": 166,
"execution_count": 167,
"metadata": {},
"outputs": [],
"source": [
@@ -2860,7 +2887,7 @@
},
{
"cell_type": "code",
"execution_count": 167,
"execution_count": 168,
"metadata": {},
"outputs": [],
"source": [
@@ -2869,7 +2896,7 @@
},
{
"cell_type": "code",
"execution_count": 168,
"execution_count": 169,
"metadata": {},
"outputs": [],
"source": [
@@ -2892,7 +2919,7 @@
},
{
"cell_type": "code",
"execution_count": 169,
"execution_count": 170,
"metadata": {},
"outputs": [],
"source": [
@@ -2901,7 +2928,7 @@
},
{
"cell_type": "code",
"execution_count": 170,
"execution_count": 171,
"metadata": {},
"outputs": [],
"source": [
@@ -2918,7 +2945,7 @@
},
{
"cell_type": "code",
"execution_count": 171,
"execution_count": 172,
"metadata": {},
"outputs": [],
"source": [
@@ -2927,7 +2954,7 @@
},
{
"cell_type": "code",
"execution_count": 172,
"execution_count": 173,
"metadata": {},
"outputs": [],
"source": [
@@ -2938,7 +2965,7 @@
},
{
"cell_type": "code",
"execution_count": 173,
"execution_count": 174,
"metadata": {},
"outputs": [],
"source": [
@@ -2953,7 +2980,7 @@
},
{
"cell_type": "code",
"execution_count": 174,
"execution_count": 175,
"metadata": {},
"outputs": [],
"source": [
@@ -2962,7 +2989,7 @@
},
{
"cell_type": "code",
"execution_count": 175,
"execution_count": 176,
"metadata": {},
"outputs": [],
"source": [
@@ -2971,7 +2998,7 @@
},
{
"cell_type": "code",
"execution_count": 176,
"execution_count": 177,
"metadata": {},
"outputs": [],
"source": [
@@ -2998,7 +3025,7 @@
},
{
"cell_type": "code",
"execution_count": 177,
"execution_count": 178,
"metadata": {},
"outputs": [],
"source": [
@@ -3007,7 +3034,7 @@
},
{
"cell_type": "code",
"execution_count": 178,
"execution_count": 179,
"metadata": {
"scrolled": true
},
@@ -3019,7 +3046,7 @@
},
{
"cell_type": "code",
"execution_count": 179,
"execution_count": 180,
"metadata": {},
"outputs": [],
"source": [
@@ -3053,7 +3080,7 @@
},
{
"cell_type": "code",
"execution_count": 180,
"execution_count": 181,
"metadata": {},
"outputs": [],
"source": [
@@ -3062,7 +3089,7 @@
},
{
"cell_type": "code",
"execution_count": 181,
"execution_count": 182,
"metadata": {},
"outputs": [],
"source": [
@@ -3075,7 +3102,7 @@
},
{
"cell_type": "code",
"execution_count": 182,
"execution_count": 183,
"metadata": {},
"outputs": [],
"source": [
@@ -3163,7 +3190,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.7.3"
}
},
"nbformat": 4,