LinearSVC sklearn(scikit-learn)中C的行为

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

首先创建一些玩具数据:

n_samples=20
X=np.concatenate((np.random.normal(loc=2, scale=1.0, size=n_samples),np.random.normal(loc=20.0, scale=1.0, size=n_samples),[10])).reshape(-1,1)
y=np.concatenate((np.repeat(0,n_samples),np.repeat(1,n_samples+1)))
plt.scatter(X,y)

在图形下方以可视化数据:

enter image description here

然后我用LinearSVC训练模型

from sklearn.svm import LinearSVC
svm_lin = LinearSVC(C=1)
svm_lin.fit(X,y)

我对C的理解是:

  • 如果C非常大,则不会容忍错误分类,因为惩罚会很大。
  • 如果C很小,将容忍错误分类以使边距(软边距)变大。

[使用C=1,我有下面的图(橙色线表示给定x值的预测),我们可以看到决策边界在7左右,因此C=1足够大,不会出现任何错误分类。] >

X_test_svml=np.linspace(-1, 30, 300).reshape(-1,1)
plt.scatter(X,y)
plt.scatter(X_test_svml,svm_lin.predict(X_test_svml),marker="_")
plt.axhline(.5, color='.5')

enter image description here

例如,以C=0.001,我希望决策边界移到右侧,例如大约11,但是我得到了:

enter image description here

我尝试了具有SVC功能的另一个模块:

from sklearn.svm import SVC
svc_lin = SVC(kernel = 'linear', random_state = 0,C=0.01)
svc_lin.fit(X,y)

我成功获得了期望的输出:

enter image description here

并且使用我的R代码,我得到了一些更容易理解的东西。 (我使用了svm包中的e1071函数)

enter image description here

首先,我创建一些玩具数据:n_samples = 20 X = np.concatenate((np.random.normal(loc = 2,scale = 1.0,size = n_samples),np.random.normal(loc = 20.0,scale = 1.0,size = n_samples),[10]))。reshape(-1,1)y = np ....

python scikit-learn svm
1个回答
2
投票

[LinearSVCSVC(kernel=linear)是不同的东西。

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