mirror of
https://github.com/ArthurDanjou/handson-ml3.git
synced 2026-01-14 12:14:36 +01:00
Sync notebook with book's code examples, and better identify extra code
This commit is contained in:
@@ -140,7 +140,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# not in the book – this cell generates and saves Figure 5–1\n",
|
||||
"# extra code – this cell generates and saves Figure 5–1\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 5–2\n",
|
||||
"# extra code – this cell generates and saves Figure 5–2\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 5–3\n",
|
||||
"# extra code – this cell generates and saves Figure 5–3\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 5–4\n",
|
||||
"# extra code – this cell generates and saves Figure 5–4\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 5–5\n",
|
||||
"# extra code – this cell generates and saves Figure 5–5\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 5–6\n",
|
||||
"# extra code – this cell generates and saves Figure 5–6\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 5–7\n",
|
||||
"# extra code – this cell generates and saves Figure 5–7\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 5–8\n",
|
||||
"# extra code – this cell generates and saves Figure 5–8\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 5–9\n",
|
||||
"# extra code – this cell generates and saves Figure 5–9\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 5–10\n",
|
||||
"# extra code – this cell generates and saves Figure 5–10\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 5–11\n",
|
||||
"# extra code – this cell generates and saves Figure 5–11\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 5–12\n",
|
||||
"# extra code – this cell generates and saves Figure 5–12\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 5–13\n",
|
||||
"# extra code – this cell generates and saves Figure 5–13\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",
|
||||
|
||||
Reference in New Issue
Block a user