Sklearn SVM 给出了错误的决策边界
Sklearn SVM gives wrong decision boundary
我在代码中使用了 Sklearn 的 SVC,并使用 mlxtend plot_decision_regions 函数绘制了它。请看下面的代码,我的数据是简单的二维点。决策函数的图没有意义,因为边界比另一个更接近 class。我 doing/interpreting 有错吗?
import numpy
from sklearn.svm import SVC
import matplotlib.pyplot as plt
from mlxtend.plotting import plot_decision_regions
f = np.array([[1, 1], [0, 0]])
labels = np.array([1, 0])
model = SVC(kernel='linear')
model.fit(f, labels)
plot_decision_regions(X=f, y=labels, clf=model, legend=2)
plt.ylim([-1, 2])
plt.xlim([-1, 2])
plt.xlabel('feature 1')
plt.ylabel('feature 2')
plt.show()
此数据的结果看起来是四四方方的,如下所示:
with data from case 1
如果我将数据 f 更改为:np.array([[1, 1], [0, 0], [1, 0], [0, 1]])
标签为:np.array([1, 0, 0, 1])
结果如下:
With data from case 2
是不是我用的绘图库的问题?
plot_decision_regions
中似乎有一些错误。
让我们使用plot_svc_decision_boundary的handson-ml
import numpy as np
from sklearn.svm import SVC
import matplotlib.pyplot as plt
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.scatter(svs[:, 0], svs[:, 1], s=180, facecolors='#FFAAAA')
plt.plot(x0, decision_boundary, "k-", linewidth=2)
plt.plot(x0, gutter_up, "k--", linewidth=2)
plt.plot(x0, gutter_down, "k--", linewidth=2)
f = np.array([[1, 1], [0, 0]])
labels = np.array([1, 0])
svm_clf = SVC(kernel='linear')
svm_clf.fit(f, labels)
plot_svc_decision_boundary(svm_clf, -1, 2.0)
plt.ylim([-1, 2])
plt.xlim([-1, 2])
plt.xlabel('feature 1')
plt.ylabel('feature 2')
plt.scatter(f[0, 0], f[0, 1], marker='^', s=80)
plt.scatter(f[1, 0], f[1, 1], marker='s', s=80)
plt.show()
我在代码中使用了 Sklearn 的 SVC,并使用 mlxtend plot_decision_regions 函数绘制了它。请看下面的代码,我的数据是简单的二维点。决策函数的图没有意义,因为边界比另一个更接近 class。我 doing/interpreting 有错吗?
import numpy
from sklearn.svm import SVC
import matplotlib.pyplot as plt
from mlxtend.plotting import plot_decision_regions
f = np.array([[1, 1], [0, 0]])
labels = np.array([1, 0])
model = SVC(kernel='linear')
model.fit(f, labels)
plot_decision_regions(X=f, y=labels, clf=model, legend=2)
plt.ylim([-1, 2])
plt.xlim([-1, 2])
plt.xlabel('feature 1')
plt.ylabel('feature 2')
plt.show()
此数据的结果看起来是四四方方的,如下所示: with data from case 1
如果我将数据 f 更改为:np.array([[1, 1], [0, 0], [1, 0], [0, 1]]) 标签为:np.array([1, 0, 0, 1])
结果如下: With data from case 2
是不是我用的绘图库的问题?
plot_decision_regions
中似乎有一些错误。
让我们使用plot_svc_decision_boundary的handson-ml
import numpy as np
from sklearn.svm import SVC
import matplotlib.pyplot as plt
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.scatter(svs[:, 0], svs[:, 1], s=180, facecolors='#FFAAAA')
plt.plot(x0, decision_boundary, "k-", linewidth=2)
plt.plot(x0, gutter_up, "k--", linewidth=2)
plt.plot(x0, gutter_down, "k--", linewidth=2)
f = np.array([[1, 1], [0, 0]])
labels = np.array([1, 0])
svm_clf = SVC(kernel='linear')
svm_clf.fit(f, labels)
plot_svc_decision_boundary(svm_clf, -1, 2.0)
plt.ylim([-1, 2])
plt.xlim([-1, 2])
plt.xlabel('feature 1')
plt.ylabel('feature 2')
plt.scatter(f[0, 0], f[0, 1], marker='^', s=80)
plt.scatter(f[1, 0], f[1, 1], marker='s', s=80)
plt.show()