Understanding Line using Linear Algebra

Sahil -
6 min readOct 24, 2019

--

Hi Guys!

In this blog, let us understand How the Geometry Shape (like Line, Plane) with concept of Basic Linear Algebra are useful for Data Science/ML. If you not study Basic Linear Algebra (Points and Vector), I recommend to please revise it.

So, Let Get Started!

Suppose we have a line.

Equation of the line (which we studied in secondary school) is:

But, the generalize form of equation of line is:

If you compare equation 1 and 2, you will observe that

Let take another way, Suppose for this graph,

For this generalize form of equation of this line is:

Can we rephrase for this graph? Yes! of course. It doesn’t matter whatever axis you name them, the generalize for of this equation will remain same. Same for the case coefficient term, we can write in the form of other way around.

Why? Because we want to use this concept in Data Science/ML field. So, we are rephrasing into other way around which we can apply in Data Science/ML.

For what about 3D space? Well, we can write the generalize form of equation of plane (which contain 3 axis means we need to create 3 variables).

Note: If your points are in 2D space, then its a line. If your points are in 3D space, then its a plane. And if your points are in higher dimension (let say, n)-nD, then we called hyperplane

So, for the higher nD space, we have ’n’ axis which means ’n’ variables. So, the generalize form of equation of line:

See Remember? From nD space equation, we transform it into vectors.

Let consider some notation.

Note: If it is not mention any specific whether is column vector or row vector, then by default, consider as column vector.

So, we get the equation by replacing notation,

Now, let understand what ‘w0’ means?

So, for the graph below, we know that,

Rearrange and find ‘x2’

If a line passing through origin then,

From the above above conclusion, we can say that for a line/plane/hyperplane passing through the origin, then

If not passing through origin, just add ‘w0’ term.

Now, we understood the role of ‘w0’.

Now, if you remember dot product of two vectors?

Then apply these concept there. So, For any hyperplane passing through origin, we got:

So, its means vector ‘W’ and ‘X’ are perpendicular to each other.

Overall review is:

If you remember the concept of unit vector, we can written for vector ‘W’ as:

Even then,

Remember? Either you perform operations(like addition, subtraction, dot product, etc) with two vectors with presence of unit vector then the result after the operation will remain same not matter what!

Let me illustrate this meaning.

It looks like different vector representation (either with vector or vector in presence of unit vector). But the operation on these vector like Add, Subtract, Dot product, etc will give you the same result.

A+B = [1,7]

(Please see the blog again, I have also solve this same example in the concept ‘Unit Vector’. Basic Linear Algebra (Points and Vector) )

Now, enough with theory. Let do Coding!! Yea…. :D

(Note: I have written in comment also in the coding!! So, you will understand very well. Hope you like it)

Import Necessary Files

Let define equation of the line (y=3x) which means this line is passing through origin.

Let plot the line of the equation.

Now, let proceed with vectors and see what are the results.

(Red color represent vector ‘W’ and blue color represent point vector)

So, you see the point I have taken lie on the line and multiply with vectors give zero.

Let see with the dot product and see the result.

Bravo! Still give same result!.

Lastly, with the unit vector of W and see the result.

(Red color represent unit vector ‘W’_hat and blue color represent point vector)

WoW!! So, you see, we observed with everything and gave same result which means that vector W is perpendicular to that points which lie in the line (π).

This time let take another point which doesn’t lie in line.

These result doesn’t give equal to zero. That has to be expected!!.

Observation

  1. For this point, vector W is not perpendicular to that point because these point doesn’t not lie in the line (π).
  2. Green color represent unit vector ‘W_hat’ and Red color represent vector ‘W’ are moving in the same direction. Of course, unit vector will be small because its magnitude is 1.

Below graph is the same graph as above, I have increase the magnitude of unit vector (green color) just to clear the 2nd point of a observation mentioned in above.

Let do one more interesting thing, I have chose point which is in opposite side of the line. Let find out

Did you observe anything?

So, you see…

Let consider a line equation (y=mx+c) which divides the region into two parts: grey region and yellow region. And I have taken three points: P1 (on grey region side), P2 (lie in the line) and P3 (on the yellow region side) and let be normal of line is vector ‘W’.

Then,

Mystery, Huh?? Remember these above observation points. It will used in next upcoming.

I hope you enjoy it.

Keep Reading, Have Learning and Most Important Part, Have Fun!

--

--

Sahil -
Sahil -

Written by Sahil -

Techie with a Passion for Learning and Sharing

No responses yet