0

Dataset containing two labels 0,1 , how to plot a scatter plot for it..?

    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
... ... ... ...         .....
DavidG
  • 24,279
  • 14
  • 89
  • 82
AbtabM
  • 129
  • 10

2 Answers2

1

Set the "c" parameter equal to the target variable

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
0

Here's the code for plotting dataset values ,initially just to visualize our dataset points on the graph:

  • it contains two features :[Age,EstimatedSalary]
  • [Purchased] is out target class,containing 0 & 1 as two labels

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()

Plot Containg Descision Boundary as well,

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 here enter image description here

AbtabM
  • 129
  • 10