SVM二元性:不支持超参数集。

问题描述 投票:1回答:1

我正试图在Iris数据集上训练一个SVM模型。目的是将Iris virginica花和其他类型的花进行分类。下面是代码。

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)

我的书,也就是Aurelien Geron的 "Hands-On Machine Learning with Scikit-Learn , Keras and TensorFlow",第2版,在第156页说:

为了获得更好的性能,你应该设置 dual 超参数 False,除非特征比训练实例多

但如果我设置了 dual 超参数为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 超参数改为True。

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

machine-learning scikit-learn svm
1个回答
1
投票

L2 SVM与L1损失(铰链)不能以原始形式求解。只有它的二元形式才能有效地进行求解。这是因为受限于 LIBLINEAR sklearn使用的库。如果你想解决L2 SVM的原始形式,你将不得不使用L2损失(平方铰链)来代替。

LinearSVC(C=1, loss='squared_hinge', dual=False).fit(X,y)

对于模式的细节。链接1

© www.soinside.com 2019 - 2024. All rights reserved.