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

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

包含两个标签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
... ... ... ...         .....
python matplotlib scatter-plot multilabel-classification supervised-learning
1个回答
0
投票

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

  • 它包含两个功能:[年龄,估计工资]
  • [[Purchased]在目标类别之外,包含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()

enter image description hereenter image description here

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