Search code examples
matlabplotregressionmle

Plotting a linear decision boundary


I have a set of data points (40 x 2), and I've derived the formula for the decision boundary which ends up like this :

wk*X + w0 = 0

wk is a 1 x 2 vector and X is a 2 x 1 point from the data point set; essentially X = (xi,yi), where i = 1,2,...,40. I have the values for wk and w0.

I'm trying to plot the line wk*X + w0 = 0 but I have no idea how to plot the actual line. In the past, I've done this by finding the minimum and maximum of the data points and just connecting them together but that's definitely not the right approach.


Solution

  • wk*X is simply the dot product between two vectors, and so the equation becomes:

    w1*x + w2*y + w0 = 0
    

    ... assuming a general point (x,y). If we rearrange this equation and solve for y, we get:

    y = -(w1/w2)*x - (w0/w2)
    

    As such, this defines an equation of the line where the slope is -(w1/w2) with an intercept -(w0/w2). All you have to do is define a bunch of linearly spaced points within a certain range, take each point and substitute this into the above equation and get an output. You'd plot all of these output points in the figure as well as the actual points themselves. You make the space or resolution between points small enough so that we are visualizing a line when we connect all of the points together.

    To determine the range or limits of this line, figure out what the smallest and largest x value is in your data, define a set of linearly spaced points between these and plot your line using the equation of the line we just talked about.

    Something like this could work assuming that you have a matrix of points stored in X as you have mentioned, and w1 and w2 are defined in the vector wk and w0 is defined separately:

    x = linspace(min(X(:,1)), max(X(:,1)));
    y = -(wk(1)/wk(2))*x - (w0/wk(2));
    plot(X(:,1), X(:,2), 'b.', x, y);
    

    linspace determines a linearly spaced array of points from a beginning to an end, and by default 100 points are generated. We then create the output values of the line given these points and we plot the individual points in blue as well as the line itself on top of these points.