ageron / ageron/handson-ml3

[BUG] Running cell 5 of 05_support_vector_machines.ipynb notebook gives error

未关闭
#67 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Jupyter Notebook
星标
14.1k
派生
5.3k
PR 合并指标
30 天内没有已合并 PR

描述

I am running cells from top till cell 5 and when running cell 5 with this code:

```
# extra code – this cell generates and saves Figure 5–1

import matplotlib.pyplot as plt
import numpy as np
from sklearn.svm import SVC
from sklearn import datasets

iris = datasets.load_iris(as_frame=True)
X = iris.data[["petal length (cm)", "petal width (cm)"]].values
y = iris.target

setosa_or_versicolor = (y == 0) | (y == 1)
X = X[setosa_or_versicolor]
y = y[setosa_or_versicolor]

# SVM Classifier model
svm_clf = SVC(kernel="linear", C=float("inf"))
svm_clf.fit(X, y)

# Bad models
x0 = np.linspace(0, 5.5, 200)
pred_1 = 5 * x0 - 20
pred_2 = x0 - 1.8
pred_3 = 0.1 * x0 + 0.5

def plot_svc_decision_boundary(svm_clf, xmin, xmax):
w = svm_clf.coef_[0]
b = svm_clf.intercept_[0]

# At the decision boundary, w0*x0 + w1*x1 + b = 0
# => x1 = -w0/w1 * x0 - b/w1
x0 = np.linspace(xmin, xmax, 200)
decision_boundary = -w[0] / w[1] * x0 - b / w[1]

margin = 1/w[1]
gutter_up = decision_boundary + margin
gutter_down = decision_boundary - margin
svs = svm_clf.support_vectors_

plt.plot(x0, decision_boundary, "k-", linewidth=2, zorder=-2)
plt.plot(x0, gutter_up, "k--", linewidth=2, zorder=-2)
plt.plot(x0, gutter_down, "k--", linewidth=2, zorder=-2)
plt.scatter(svs[:, 0], svs[:, 1], s=180, facecolors='#AAA',
zorder=-1)

fig, axes = plt.subplots(ncols=2, figsize=(10, 2.7), sharey=True)

plt.sca(axes[0])
plt.plot(x0, pred_1, "g--", linewidth=2)
plt.plot(x0, pred_2, "m-", linewidth=2)
plt.plot(x0, pred_3, "r-", linewidth=2)
plt.plot(X[:, 0][y==1], X[:, 1][y==1], "bs", label="Iris versicolor")
plt.plot(X[:, 0][y==0], X[:, 1][y==0], "yo", label="Iris setosa")
plt.xlabel("Petal length")
plt.ylabel("Petal width")
plt.legend(loc="upper left")
plt.axis([0, 5.5, 0, 2])
plt.gca().set_aspect("equal")
plt.grid()

plt.sca(axes[1])
plot_svc_decision_boundary(svm_clf, 0, 5.5)
plt.plot(X[:, 0][y==1], X[:, 1][y==1], "bs")
plt.plot(X[:, 0][y==0], X[:, 1][y==0], "yo")
plt.xlabel("Petal length")
plt.axis([0, 5.5, 0, 2])
plt.gca().set_aspect("equal")
plt.grid()

save_fig("large_margin_classification_plot")
plt.show()
```

I am getting error:

```
---------------------------------------------------------------------------
InvalidParameterError Traceback (most recent call last)
Cell In[5], line 18
16 # SVM Classifier model
17 svm_clf = SVC(kernel="linear", C=float("inf"))
---> 18 svm_clf.fit(X, y)
20 # Bad models
21 x0 = np.linspace(0, 5.5, 200)

File ~\anaconda3\envs\ml_1\Lib\site-packages\sklearn\svm\_base.py:180, in BaseLibSVM.fit(self, X, y, sample_weight)
147 def fit(self, X, y, sample_weight=None):
148 """Fit the SVM model according to the given training data.
149
150 Parameters
(...)
178 matrices as input.
179 """
--> 180 self._validate_params()
182 rnd = check_random_state(self.random_state)
184 sparse = sp.isspmatrix(X)

File ~\anaconda3\envs\ml_1\Lib\site-packages\sklearn\base.py:581, in BaseEstimator._validate_params(self)
573 def _validate_params(self):
574 """Validate types and values of constructor parameters
575
576 The expected type and values must be defined in the `_parameter_constraints`
(...)
579 accepted constraints.
580 """
--> 581 validate_parameter_constraints(
582 self._parameter_constraints,
583 self.get_params(deep=False),
584 caller_name=self.__class__.__name__,
585 )

File ~\anaconda3\envs\ml_1\Lib\site-packages\sklearn\utils\_param_validation.py:97, in validate_parameter_constraints(parameter_constraints, params, caller_name)
91 else:
92 constraints_str = (
93 f"{', '.join([str(c) for c in constraints[:-1]])} or"
94 f" {constraints[-1]}"
95 )
---> 97 raise InvalidParameterError(
98 f"The {param_name!r} parameter of {caller_name} must be"
99 f" {constraints_str}. Got {param_val!r} instead."
100 )

InvalidParameterError: The 'C' parameter of SVC must be a float in the range (0.0, inf). Got inf instead.
```

How to fix it?

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。