From a5baa85363a048c8075b40fe3d1bc96fbdb855bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Tue, 14 Nov 2023 21:34:06 +1300 Subject: [PATCH] Set dual=True in LinearSVC and LinearSVR to avoid warning --- 05_support_vector_machines.ipynb | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/05_support_vector_machines.ipynb b/05_support_vector_machines.ipynb index 421a7d2..31cd016 100644 --- a/05_support_vector_machines.ipynb +++ b/05_support_vector_machines.ipynb @@ -358,6 +358,13 @@ "plt.show()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note: the default value for the `dual` hyperparameter of the `LinearSVC` and `LinearSVR` estimators will change from `True` to `\"auto\"` in Scikit-Learn 1.4, so I set `dual=True` throughout this notebook to ensure the output of this notebook remains unchanged." + ] + }, { "cell_type": "code", "execution_count": 8, @@ -387,7 +394,7 @@ "y = (iris.target == 2) # Iris virginica\n", "\n", "svm_clf = make_pipeline(StandardScaler(),\n", - " LinearSVC(C=1, random_state=42))\n", + " LinearSVC(C=1, dual=True, random_state=42))\n", "svm_clf.fit(X, y)" ] }, @@ -454,8 +461,8 @@ "# 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", - "svm_clf2 = LinearSVC(C=100, max_iter=10_000, random_state=42)\n", + "svm_clf1 = LinearSVC(C=1, max_iter=10_000, dual=True, random_state=42)\n", + "svm_clf2 = LinearSVC(C=100, max_iter=10_000, dual=True, random_state=42)\n", "\n", "scaled_svm_clf1 = make_pipeline(scaler, svm_clf1)\n", "scaled_svm_clf2 = make_pipeline(scaler, svm_clf2)\n", @@ -595,7 +602,7 @@ "polynomial_svm_clf = make_pipeline(\n", " PolynomialFeatures(degree=3),\n", " StandardScaler(),\n", - " LinearSVC(C=10, max_iter=10_000, random_state=42)\n", + " LinearSVC(C=10, max_iter=10_000, dual=True, random_state=42)\n", ")\n", "polynomial_svm_clf.fit(X, y)" ] @@ -932,7 +939,7 @@ "y = 4 + 3 * X[:, 0] + np.random.randn(50)\n", "\n", "svm_reg = make_pipeline(StandardScaler(),\n", - " LinearSVR(epsilon=0.5, random_state=42))\n", + " LinearSVR(epsilon=0.5, dual=True, random_state=42))\n", "svm_reg.fit(X, y)" ] }, @@ -978,7 +985,7 @@ " plt.axis(axes)\n", "\n", "svm_reg2 = make_pipeline(StandardScaler(),\n", - " LinearSVR(epsilon=1.2, random_state=42))\n", + " LinearSVR(epsilon=1.2, dual=True, random_state=42))\n", "svm_reg2.fit(X, y)\n", "\n", "svm_reg._support = find_support_vectors(svm_reg, X, y)\n", @@ -1566,7 +1573,7 @@ "scaler = StandardScaler()\n", "X_scaled = scaler.fit_transform(X)\n", "\n", - "lin_clf = LinearSVC(loss=\"hinge\", C=C, random_state=42).fit(X_scaled, y)\n", + "lin_clf = LinearSVC(loss=\"hinge\", C=C, dual=True, random_state=42).fit(X_scaled, y)\n", "svc_clf = SVC(kernel=\"linear\", C=C).fit(X_scaled, y)\n", "sgd_clf = SGDClassifier(alpha=alpha, random_state=42).fit(X_scaled, y)" ] @@ -1975,7 +1982,7 @@ } ], "source": [ - "lin_clf = LinearSVC(random_state=42)\n", + "lin_clf = LinearSVC(dual=True, random_state=42)\n", "lin_clf.fit(X_train, y_train)" ] }, @@ -2011,7 +2018,7 @@ } ], "source": [ - "lin_clf = LinearSVC(max_iter=1_000_000, random_state=42)\n", + "lin_clf = LinearSVC(max_iter=1_000_000, dual=True, random_state=42)\n", "lin_clf.fit(X_train, y_train)" ] }, @@ -2090,7 +2097,7 @@ ], "source": [ "lin_clf = make_pipeline(StandardScaler(),\n", - " LinearSVC(random_state=42))\n", + " LinearSVC(dual=True, random_state=42))\n", "lin_clf.fit(X_train, y_train)" ] }, @@ -2347,7 +2354,7 @@ "source": [ "from sklearn.svm import LinearSVR\n", "\n", - "lin_svr = make_pipeline(StandardScaler(), LinearSVR(random_state=42))\n", + "lin_svr = make_pipeline(StandardScaler(), LinearSVR(dual=True, random_state=42))\n", "lin_svr.fit(X_train, y_train)" ] }, @@ -2377,7 +2384,7 @@ ], "source": [ "lin_svr = make_pipeline(StandardScaler(),\n", - " LinearSVR(max_iter=5000, random_state=42))\n", + " LinearSVR(max_iter=5000, dual=True, random_state=42))\n", "lin_svr.fit(X_train, y_train)" ] },