Linear Algebra, Python, NumPy and Data Science

There is a relation between Linear Algebra, Python, NumPy and Data Science. In this section, we’ll try to understand this relation.

However, we will keep this introduction as simple as possible keeping in mind that a beginner might understand.

Firstly, linear algebra starts with Vectors. 

We have seen before the difference between scalar and vector.

A scalar number is any real number. Like the speed of a car. 

For example it is 5 miles per hour.

However, it is a scalar value.

Why?

Because we don’t have any direction. 

However, once we get the direction, such as the car is moving towards east, it becomes vector.

Not only that, we can now visualize the vector on a Cartesian coordinate system, that is, a graph.

Now we can print scalar and vectors quite easily.

Let’s see the code.

import numpy as np

scalar = 15.2678
print(scalar)
# 15.2678

vector = np.array([51, 28, 2583, 4])
print(vector)

# [51, 28, 2583, 4]

For example we can move forward with our initial linear algebra knowledge and create matrices as follows.

import numpy as np

a = np.array([
    [1, 2], 
    [0, 1]
])

b = np.array([
    [2, 0], 
    [3, 4]
])

Now we can multiply these two matrices. Since we have discussed this part in detail before, we’re not delving into deep.

However, we want to emphasize one thing.

Firstly, we can transport these linear algebraic concepts into the programming domain. 

As a result, we get this image which we have seen before.

Machine learning algorithm and data science
Machine learning algorithm and data science

What do we see?

Linear algebra, python and NumPy, which is actually Numerical Python, lead us to Machine learning algorithms and Data science.

Therefore we can apply this knowledge and get most of it in the real world. Right?

How we can apply Linear Algebra in Data Science and Machine Learning

We’ve done it in many ways. 

However, as we just said before, let’s make it simple enough so a beginner can understand.

Firstly, we have seen the usage of linear algebra in Google’s ranking algorithm which is one of the most complex machine learning algorithms. It uses a lot of linear algebra.

Secondly, data optimization. Certainly which relates to data science and machine learning. And we’ll see a lot of examples in the coming sections. By applying linear algebra a data scientist can optimize the budget, diet and what not.

By the way, finding the shortest route for your cab is another good example.

A few minutes ago, we were talking about the Cartesian Coordinate system. Right? 

A Matrix is a two dimensional concept. However, when we see a movie on a television screen we need to plot the three dimensional objects onto a two dimensional screen.

In NumPy we know a three dimensional vector as a tensor.

Let’s see an example.

import numpy as np
import matplotlib.pyplot as plt

tensor = np.array([
    [[111, 52, 3], [74, 5, 666], [7, 88, 19]],
    [[11, 912, 813], [414, 158, 16], [517, 718, 19]],
    [[21, 228, 123], [242, 25, 126], [217, 280, 29]],
])

print(tensor)

'''
[[[111  52   3]
  [ 74   5 666]
  [  7  88  19]]

 [[ 11 912 813]
  [414 158  16]
  [517 718  19]]

 [[ 21 228 123]
  [242  25 126]
  [217 280  29]]]
'''

plt.imshow(tensor, interpolation='nearest')
plt.show()

We know that tensors represent three dimensional graphs.

However, we have used Matplotlib and represent the three dimensional objects onto a two dimensional screen as follows.

Application of linear algebra using NumPy, Matplotlib
Application of linear algebra using NumPy, Matplotlib

There are more to come.

However, before closing down this discussion let’s remember one of the key features of Linear Algebra. Because we most often use it in data science and machine learning.

That’s Prediction. These are linear models. We use linear algebra and make the prediction.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Flutter, Dart and Algorithm

Twitter


Posted

in

, , ,

by

Tags:

Comments

Leave a Reply