Machine learning SVM对偶性:不支持超参数集

Machine learning SVM对偶性:不支持超参数集,machine-learning,scikit-learn,svm,Machine Learning,Scikit Learn,Svm,我试图在Iris数据集上训练SVM模型。目的是将鸢尾花与其他类型的花进行分类。代码如下: import numpy as np from sklearn import datasets from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.svm import LinearSVC iris = datasets.load_iris() X = iri

我试图在Iris数据集上训练SVM模型。目的是将鸢尾花与其他类型的花进行分类。代码如下:

import numpy as np
from sklearn import datasets
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC

iris = datasets.load_iris()
X = iris["data"][:, (2,3)] # petal length, petal width
y = (iris["target"]==2).astype(np.float64) # Iris virginica

svm_clf = Pipeline([
    ("scaler", StandardScaler()),
    ("linear_svc", LinearSVC(C=1, loss="hinge", dual=False))
])

svm_clf.fit(X,y)
我的书是Aurelian Geron的《使用Scikit Learn、Keras和TensorFlow进行机器学习的实践》,第二版,第156页,内容如下:

为了获得更好的性能,应将
dual
超参数设置为
False
,除非功能多于训练实例

但是如果我将
dual
hyperparameter设置为False,则会出现以下错误:

ValueError: Unsupported set of arguments: The combination of penalty='l2' and loss='hinge' are not supported when dual=False, Parameters: penalty='l2', loss='hinge', dual=False
相反,如果我将
dual
hyperparameter设置为True,它就会工作

为什么不支持这组超参数?

具有L1损失(铰链)的L2 SVM无法以原始形式求解。只有它的对偶形式才能得到有效的解决。这是由于sklearn使用的库的限制。如果你想解决L2支持向量机的原始形式,你将不得不使用L2损失(平方铰链)

LinearSVC(C=1, loss='squared_hinge', dual=False).fit(X,y)
有关模式详细信息: