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

@@ -140,7 +140,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 51\n",
"# extra code this cell generates and saves Figure 51\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
@@ -219,7 +219,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 52\n",
"# extra code this cell generates and saves Figure 52\n",
"\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
@@ -269,7 +269,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 53\n",
"# extra code this cell generates and saves Figure 53\n",
"\n",
"X_outliers = np.array([[3.4, 1.3], [3.2, 0.8]])\n",
"y_outliers = np.array([0, 0])\n",
@@ -364,7 +364,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 54\n",
"# extra code this cell generates and saves Figure 54\n",
"\n",
"scaler = StandardScaler()\n",
"svm_clf1 = LinearSVC(C=1, max_iter=10_000, random_state=42)\n",
@@ -432,7 +432,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 55\n",
"# extra code this cell generates and saves Figure 55\n",
"\n",
"X1D = np.linspace(-4, 4, 9).reshape(-1, 1)\n",
"X2D = np.c_[X1D, X1D**2]\n",
@@ -492,7 +492,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 56\n",
"# extra code this cell generates and saves Figure 56\n",
"\n",
"def plot_dataset(X, y, axes):\n",
" plt.plot(X[:, 0][y==0], X[:, 1][y==0], \"bs\")\n",
@@ -545,7 +545,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 57\n",
"# extra code this cell generates and saves Figure 57\n",
"\n",
"poly100_kernel_svm_clf = make_pipeline(\n",
" StandardScaler(),\n",
@@ -585,7 +585,7 @@
},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 58\n",
"# extra code this cell generates and saves Figure 58\n",
"\n",
"def gaussian_rbf(x, landmark, gamma):\n",
" return np.exp(-gamma * np.linalg.norm(x - landmark, axis=1)**2)\n",
@@ -675,7 +675,7 @@
},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 59\n",
"# extra code this cell generates and saves Figure 59\n",
"\n",
"from sklearn.svm import SVC\n",
"\n",
@@ -724,7 +724,7 @@
"source": [
"from sklearn.svm import LinearSVR\n",
"\n",
"# not in the book these 3 lines generate a simple linear dataset\n",
"# extra code these 3 lines generate a simple linear dataset\n",
"np.random.seed(42)\n",
"X = 2 * np.random.rand(50, 1)\n",
"y = 4 + 3 * X[:, 0] + np.random.randn(50)\n",
@@ -740,7 +740,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 510\n",
"# extra code this cell generates and saves Figure 510\n",
"\n",
"def find_support_vectors(svm_reg, X, y):\n",
" y_pred = svm_reg.predict(X)\n",
@@ -800,7 +800,7 @@
"source": [
"from sklearn.svm import SVR\n",
"\n",
"# not in the book these 3 lines generate a simple quadratic dataset\n",
"# extra code these 3 lines generate a simple quadratic dataset\n",
"np.random.seed(42)\n",
"X = 2 * np.random.rand(50, 1) - 1\n",
"y = 0.2 + 0.1 * X[:, 0] + 0.5 * X[:, 0] ** 2 + np.random.randn(50) / 10\n",
@@ -816,7 +816,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 511\n",
"# extra code this cell generates and saves Figure 511\n",
"\n",
"svm_poly_reg2 = make_pipeline(StandardScaler(),\n",
" SVR(kernel=\"poly\", degree=2, C=100))\n",
@@ -857,7 +857,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 512\n",
"# extra code this cell generates and saves Figure 512\n",
"\n",
"import matplotlib.patches as patches\n",
"\n",
@@ -906,7 +906,7 @@
"metadata": {},
"outputs": [],
"source": [
"# not in the book this cell generates and saves Figure 513\n",
"# extra code this cell generates and saves Figure 513\n",
"\n",
"s = np.linspace(-2.5, 2.5, 200)\n",
"hinge_pos = np.where(1 - s < 0, 0, 1 - s) # max(0, 1 - s)\n",
@@ -1112,7 +1112,7 @@
"sgd_clf.fit(X, y)\n",
"\n",
"m = len(X)\n",
"t = np.array(y).reshape(-1, 1) * 2 - 1 # -1 if t==0, +1 if t==1\n",
"t = np.array(y).reshape(-1, 1) * 2 - 1 # -1 if y == 0, or +1 if y == 1\n",
"X_b = np.c_[np.ones((m, 1)), X] # Add bias input x0=1\n",
"X_b_t = X_b * t\n",
"sgd_theta = np.r_[sgd_clf.intercept_[0], sgd_clf.coef_[0]]\n",