具有决策边界的两个特征的多标签分类散点图

Scatter plot for Multi-label classification For Two Features With Decision Boundary

包含两个标签 0,1 的数据集,如何为其绘制散点图..?

    Age EstimatedSalary Purchased
0   19  19000             0
1   35  20000             0
2   26  43000             0
3   27  57000             0
4   19  76000             0 
... ... ... ...         .....
395 46  41000             1
396 51  23000             1
397 50  20000             1
... ... ... ...         .....

这是绘制数据集值的代码,最初只是为了在图表上可视化我们的数据集点:

  • 它包含两个特征:[Age,EstimatedSalary]
  • [已购买] 超出目标 class,包含 0 和 1 作为两个标签

label1=dataset[dataset["Purchased"]==0]
label2=dataset[dataset["Purchased"]==1]
len_id_0=np.where(dataset.Purchased == 0)  #indices for label 0
len_id_1=np.where(dataset.Purchased == 1)

x_1_0=label1["Age"]                                 # 1st feature having label as 0                      
x_2_0=label1["EstimatedSalary"]                     # 2nd feature having label as 0
x_1_1=label2["Age"]                                 # 1st features having label as 1
x_2_1=label2["EstimatedSalary"]                     # 2nd features having label as 1
plt.scatter(x_1_0,x_2_0)
plt.scatter(x_1_1,x_2_1)
plt.title('(Train set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.show()

绘制包含决策边界,

from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
             alpha = 0.75, cmap = ListedColormap(('white', 'yellow')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                c = ListedColormap(('black', 'orange'))(i), label = j)
plt.title('SVM (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

将“c”参数设置为等于目标变量

data.head()

X = data.iloc[:,0:2].values
y = data.iloc[:,2:3].values
plt.scatter(X[:,0:1], X[:,1:2], c=y)
plt.show