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

2024-06-26 11:23:20 发布

您现在位置:Python中文网/ 问答频道 /正文

包含两个标签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
... ... ... ...         .....

Tags: 数据age绘制标签purchasedestimatedsalary
2条回答

以下是绘制数据集值的代码,最初只是为了在图形上可视化数据集点:

  • 它包含两个特征:[年龄,估计值]
  • [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

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

data.head()

data frame

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

scatter plot

相关问题 更多 >