C2_W3_Lab 2_Query

def plot_decision_boundary(X, Y, parameters):
    W = parameters["W"]
    b = parameters["b"]

    fig, ax = plt.subplots()
    plt.scatter(X[0, :], X[1, :], c=Y, cmap=colors.ListedColormap(['blue', 'red']));
    
    x_line = np.arange(np.min(X[0,:]),np.max(X[0,:])*1.1, 0.1)
    ax.plot(x_line, - W[0,0] / W[0,1] * x_line + -b[0,0] / W[0,1] , color="black")
    plt.plot()
    plt.show()
    
plot_decision_boundary(X, Y, parameters)

Kindly explain this part:

plt.scatter(X[0, :], X[1, :], c=Y, cmap=colors.ListedColormap(['blue', 'red']));
    
    x_line = np.arange(np.min(X[0,:]),np.max(X[0,:])*1.1, 0.1)
    ax.plot(x_line, - W[0,0] / W[0,1] * x_line + -b[0,0] / W[0,1] , color="black")
  1. What does the arguments in ax.plot mean? Equation of a line it seems.

Hey @Debatreyo_Roy,

image

 plt.scatter(X[0, :], X[1, :], c=Y, cmap=colors.ListedColormap(['blue', 'red']));

In section 2.2 - Dataset, we can see that X is a 2-dimensional dataset, i.e., it has 30 samples, each sample containing 2 features. So, to plot X in 2-dimensions, we have used plt.scatter(), which requires the x and y co-ordinates for all the 30 samples in our dataset. So, X[0, :] represents the x-coordinates of the 30 samples, and X[1, :] represents the y-coordinates of the 30 samples in our dataset. However, as you can see in the above plot, there are only 4 points visible. This is because the dataset doesn’t have 30 unique samples, instead the dataset has 4 unique samples only.


x_line = np.arange(np.min(X[0,:]),np.max(X[0,:])*1.1, 0.1)
ax.plot(x_line, - W[0,0] / W[0,1] * x_line + -b[0,0] / W[0,1] , color="black")

Now, let’s consider the above lines of code. These plot a straight line in the 2-dimensional space, using the learnt parameters. In the lab, we have considered the following equation of line: W_1 x_1 + W_2 x_2 + b = 0, so, if we try to find out x_2 (which is plotted on the Y dimension), in terms of x_1 (which is plotted on the X dimension), we will get:

x_2 = \frac{-W_1}{W_2} x_1 + \frac{-b}{W_2}

Now, we are trying to plot the decision boundary based on the learnt parameters. So, we sample a number of points on the x-axis, specifically between the min and max value of the x-coordinates of our samples, represented by x_line. For these sampled x-coordinates, we compute the y-coordinates using the above equation, and we plot the line, i.e., the decision boundary.

Voila, we are done :partying_face: Let us know if this helps.

Cheers,
Elemento