Linear Algebra, Vector, Matrix and Data Science

Linear algebra deals with vectors and matrices. What is a vector? Moreover, what is a Matrix? Above all how linear algebra as a discipline of mathematics relates to them?

We’ll discuss the topic in this section. However, before we have discussed what a Vector or Matrix is.

Not only that, we will also see how we can use data science tools like NumPy and Matplotlib to visualize linear algebra.

Firstly, a beginner in data science or machine learning sometimes may worry that I am not good at mathematics. And then certainly a fear pops up, can I learn data science or can I tackle machine learning algorithms?

Secondly, when she starts thinking this way, she might start learning, but finding mathematics difficult to understand, the fear becomes bigger than before.

Finally she withdraws.

However, let me categorically emphasize one point here.

There is no such thing as mathematics-brain. 

Certainly you may like a subject or not. If everybody loves and does mathematics, who will read history, or philosophy? 🙂

That’s not the point. 

The point is while learning mathematics at the school level, rarely do we visualize mathematics.

Mathematics is not about numbers, symbols and formulas. 

In this world, mathematics is everywhere. When we throw a ball, there is mathematics. When we drive a car at a certain speed towards a certain direction, there is mathematics.

Data science helps us to visualise the internal beauty of mathematics.

In this discussion, we’ll see that.

Scalar and Vector in Linear Algebra

When we see a number, we call it a “scalar”. 

For example, a car travels at 6 miles per hour. However it will remain scalar unless we declare the direction. 

Suppose it goes towards the east. That means we can plot the value on a Graph paper. 

As a result, the car starts from the intersection of x axis and y axis and goes horizontally. Therefore the arrow stops at the point 6 on the x axis.

Now, we call it a vector value. It no longer remains a scalar value.

Why?

Because we have added a new value with the speed of the car. And we can write the vector value as (6, 0) now.

Here 6 represents the value on the x axis. And 0 represents the value of y axis.

Certainly, we can change the value and plot the graph with the Matplotlib.

import matplotlib.pyplot as plt

# a Vector can originate anywhere, yet we can set location 
X = [0]
Y = [0]

# Directional vectors 

U = [6]
V = [3]

# Creating plot
plt.quiver(X, Y, U, V, color='b', units='xy', scale=1)
plt.title('Single Vector')

# x axis limit and y axis limit
plt.xlim(-10, 10)
plt.ylim(-10, 10)

# Show plot with grid just like a graph paper
plt.grid()
plt.show()

Let’s decipher the code first. We have instructed Matplotlib to draw a graph that will generate at (0, 0). 

After that, we give it a value and direction. The Matplotlib will plot the first value of the vector which is 6 on the x axis.

But in which direction will it move?

So we give a value of the direction also.

Magnitude, Direction of a single Vector

The value on the x axis is magnitude. And the value on the y axis is direction.

As a result, we get an output as follows.

Matplotlib plots a single vector, when value is 6 on the x axis and 3 on the y axis
Matplotlib plots a single vector, when value is 6 on the x axis and 3 on the y axis

Firstly, here both the values are positive. Secondly, the magnitude and direction originates at (0, 0). 

Finally the arrow indicates a point which is (6, 3). Now we can easily change the value of location from where it originates. Let’s make it (4, 4), instead of (0, 0).

Let’s see how Matplotlib plots the single vector now.

Matplotlib plots a single vector, when value is 6 on the x axis and 3 on the y axis, however it starts from (4, 4)
Matplotlib plots a single vector, when value is 6 on the x axis and 3 on the y axis, however it starts from (4, 4)

In short, if the values of magnitude and direction remain the same, it doesn’t matter from where it starts. 

On the Cartesian Coordinate system, a vector can start anywhere.

However, if we want to make the direction opposite, we can make the value negative. That’s all it needs.

Matplotlib plots a single vector, when value is -6 on the x axis and -3 on the y axis
Matplotlib plots a single vector, when value is -6 on the x axis and -3 on the y axis

Certainly, we need to change the code a little bit.

import matplotlib.pyplot as plt

# a Vector can originate anywhere, yet we can set location 
X = [-4]
Y = [-4]

# Directional vectors 
U = [-6]
V = [-3]

# Creating plot
plt.quiver(X, Y, U, V, color='b', units='xy', scale=1)
plt.title('Single Vector')

# x axis limit and y axis limit
plt.xlim(-10, 10)
plt.ylim(-10, 10)

# Show plot with grid just like a graph paper
plt.grid()
plt.show()

We always express a vector or a matrix using coordinates.

As a result, when the magnitude and direction are the same, a single vector can exist on the two dimensional coordinate system.

Matplotlib plots a single vector, anywhere on the plain
Matplotlib plots a single vector, anywhere on the plain

We can add vectors or matrices any time. In the next section we will see how we can add two vectors and plot the new value. 

By the way, previously we have discussed the product of two matrices.

Be always happy.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Flutter, Dart and Algorithm

Twitter

Comments

3 responses to “Linear Algebra, Vector, Matrix and Data Science”

  1. […] In our previous section, we have seen how we can plot a single vector using the Matplotlib library. […]

  2. […] We have seen before that we can plot any vector with the help of NumPy, Pandas and Matplotlib. […]

  3. […] Basically we will create three dimensional arrays. […]

Leave a Reply