12

I understand the following:

In 2D space, each data point has 2 features: x and y. The weight vector in 2D space contains 3 values [bias, w0, w1] which can be rewritten as [w0,w1,w2]. Each datapoint needs an artificial coordinate [1, x, y] for the purposes of calculating the dot product between it and the weights vector.

The learning rule used to update the weights vector for each misclassfied point is w := w + yn * xn

My question is: how do you derive two points from the weight vector w = [A, B, C] in order to graph the decision boundary?

I understand A + Bx + Cy = 0 is the linear equation in general form (A, B, C are can be taken from the weights vector) but I don't know how to plot it.

Thanks in advance.

Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
user1337603
  • 235
  • 2
  • 4
  • 8
  • This question was asked before, e.g. http://stats.stackexchange.com/questions/71335/decision-boundary-plot-for-a-perceptron – runDOSrun Jul 08 '15 at 14:16
  • 4
    I'm voting to close this question as off-topic because it doesn't seem directly-programming related and would better suit on [stats.SE]. – double-beep Jul 21 '19 at 10:21

3 Answers3

19

Plug your weights into the general form (w0 + w1x + w2y = 0) and solve for x, x=0, y, y=0:

x = -(w0 - w2y)/w1  
x = 0 when y = -w0/w2  
y = -(w0 - w1x)/w2  
y = 0 when x = -w0/w1  

Now we have two points that lie on the line: (0, -w0/w2) and (-w0/w1, 0)

slope = -(w0/w2)/(w0/w1)  
intercept = -w0/w2
  • This exactly worked for me. I was designing a simple perceptron with two inputs and one input for bias, so after training i have got 3 weights, w0, w1, w2, and w0 is nothing but the bias. I plug in the values in the slope, intercept formula above, and it nicely drawn the decision boundary for my sample data points. Thanks. – Rubanraj Ravichandran Dec 29 '17 at 12:25
6

Recently I was trying to implement the same thing, but too confused how to draw the decision boundary plot with three weights $w_0,w_1,w_2$. And based on @Joshu solution mentioned above, I have written matplotlib code to draw boundary line.

def plot_data(self,inputs,targets,weights):
    # fig config
    plt.figure(figsize=(10,6))
    plt.grid(True)

    #plot input samples(2D data points) and i have two classes. 
    #one is +1 and second one is -1, so it red color for +1 and blue color for -1
    for input,target in zip(inputs,targets):
        plt.plot(input[0],input[1],'ro' if (target == 1.0) else 'bo')

    # Here i am calculating slope and intercept with given three weights
    for i in np.linspace(np.amin(inputs[:,:1]),np.amax(inputs[:,:1])):
        slope = -(weights[0]/weights[2])/(weights[0]/weights[1])  
        intercept = -weights[0]/weights[2]

        #y =mx+c, m is slope and c is intercept
        y = (slope*i) + intercept
        plt.plot(i, y,'ko')

simple perceptron classifies two different class

Rubanraj Ravichandran
  • 1,033
  • 2
  • 15
  • 23
3

The best way to draw a line is to find the minimum x value and maximum x value on your display axis. calculate y values using the known line equation (-(A+BX)/C). This result in two points use inbuilt plot command to draw a line.

curious
  • 69
  • 7