here i will train perceptron and plot decision boundaries (target is generated so I am sure that it is lineary separable). For sake of the example, there is no bias.

 net=newp([-1 1; -1 1], 1); %2 inputs between -1 and 1, 1 neuron
 hold on;

This is the result: enter image description here

P has following values:

-0.2592   -0.0364    0.3828    0.0246   -0.0691
-0.2451    0.5912    0.6996    0.7101   -0.4703

And weights are:

-0.3462   -0.7939

I understand the idea behind, but how, with those weights, the decision line is plotted?

Let's say I want plot the chart by hand, how I caculate the line given the weights?

I know, elementary math, I just cannot see it now.


With no intercept and two weights ($\theta_1$ and $\theta_2$) as in the OP, the decision boundary line is at:

$0 = \theta_1 x_1 + \theta_2 x_2$

Hence, since the plot is of $x_2$ versus $x_1$, we choose two extreme points along $x_1$ included in the plot (plus some margin) and calculate the expected $x_2$ at the decision boundary:

$\large x_2 = \frac{- \theta_1 x_1} {\theta_2}$

In the OP, the two values along $P(1)$ could be arbitrarily chosen to be $-.7$ and $+.7$, yielding corresponding $P(2)$ values at the decision boundary line of $0.3052526$ and $-0.3052526$, respectively. Now it's just a matter of drawing a line between points $(-.7,.305)$ and $(.7,-.305)$.

I guess in Matlab it would only remain to run x=[-.7,.7]; y=[.305,-.305]; plot(x,y), but in R, the whole process would be:

enter image description here

    data = cbind(data,c(1,0,0,0,1))
    colnames(data) = c("P(1)","P(2)","classifier")
    rownames(data) = NULL

    plot(data, xlim=c(-.6,.65), ylim=c(-1,1.5),
         pch = c(1,3)[as.factor(data[,3])],
         col = c("blue","red")[as.factor(data[,3])],
         main="Vectors to be Classified")


    x1 = -.7
    x2 = .7
    w1 = -0.3462
    w2 = -0.7939
    y1 = (1/w2) * (- x1 * w1)
    y2 = (1/w2) * (- x2 * w1)

    lines(c(x1,x2),c(y1,y2), lwd = 3, col="darkorange4")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.