我正在嘗試使用 iris 資料集繪制 SVM 分類器的決策邊界。雖然我設定了類標簽,但圖例上沒有出現label=y.
代碼:
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.inspection import DecisionBoundaryDisplay
iris = datasets.load_iris()
X = iris.data[:, :2]
y = iris.target
linear = svm.LinearSVC()
linear.fit(X,y)
X0, X1 = X[:, 0], X[:, 1]
fig, ax = plt.subplots(figsize=(10, 6))
disp = DecisionBoundaryDisplay.from_estimator(linear, X,
response_method='predict',cmap=plt.cm.coolwarm, alpha=.8,ax=ax,
xlabel=iris.feature_names[0],ylabel=iris.feature_names[1],label=y)
ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
ax.set_xticks(())
ax.set_yticks(())
ax.set_title('Some title')
ax.legend()
plt.show()
數字:

uj5u.com熱心網友回復:
你可以使用
uj5u.com熱心網友回復:
運行您的代碼,我收到此警告:
UserWarning: The following kwargs were not used by contour: 'label'
這是由于傳遞label=y給disp = DecisionBoundaryDisplay.from_estimator
如果你想顯示一個圖例,我建議使用散點圖,如下所示:
disp = DecisionBoundaryDisplay.from_estimator(linear, X,
response_method='predict',cmap=plt.cm.coolwarm, alpha=.8,ax=ax,
xlabel=iris.feature_names[0],ylabel=iris.feature_names[1])
classes = sorted(list(set(y)))
for c in classes:
ax.scatter(X0[y == c], X1[y == c], color=plt.cm.coolwarm(c / max(classes)), s=20, edgecolors='k', label=c)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/478861.html
標籤:Python matplotlib 机器学习
