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.
Perceptron Learning Rule. The idea is that, for each observation passed through the network, the fitted value is compared to the actual value and, if they do not match, weights are updated until the error, computed as (actual-fitted), is 0.
A perceptron works by taking in some numerical inputs along with what is known as weights and a bias. It then multiplies these inputs with the respective weights(this is known as the weighted sum). These products are then added together along with the bias.
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
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With