這是我繪制 SVC 分類器決策邊界的方法。
X, y = make_classification(n_samples=100, n_features=2, n_redundant=0,
n_clusters_per_class=1, weights=[0.9], flip_y=0, random_state=1)
# fit svm model
svc_model = SVC(kernel='linear', random_state=32)
svc_model.fit(X, y)
# Constructing hyperplane using a formula.
w = svc_model.coef_[0]
b = svc_model.intercept_[0]
y_points = np.linspace(X[:, 1].min(), X[:, 1].max())
x_points = -(w[1] * y_points b) / w[0]
# Plotting our two-features-space
sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=y, s=50)
# Plotting a red hyperplane
plt.plot(x_points, y_points, c='r')
輸出:

好的,但是我想將圖例從更改為[0,1],['cat', 'dog']因為這就是標簽所代表的內容。
編輯
Usingplt.legend()產生一個以決策邊界為標簽的圖例:

uj5u.com熱心網友回復:
將最后一行替換為...
plt.plot(x_points, y_points, c='r')
plt.legend(["Cat", "Dog"])
如果您需要相同的形狀,則需要創建自定義圖例。下面顯示...
ax=plt.plot(x, y, c='r')
from matplotlib.lines import Line2D
custom = [Line2D([], [], marker='.', color='blue', linestyle='None'),
Line2D([], [], marker='.', color='orange', linestyle='None')]
plt.legend(custom, ["Cat", "Dog"])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/473854.html
