Scalar, Vector, Matrix, and Tensor in Python

What are scalar, vector, matrix and tensor in Python? They relate to TensorFlow also. And we’ll see the similarity in the next section.

But before that let’s try to understand the basics of data structures in Python.

Firstly, scalar is any number. 

Let’s see the code in python, that will explain better.

scalar = 25.23
print(scalar)
# output
25.23

In the above code what do we see?

The variable “scalar” is equal to a number. Right? 

However, we cannot limit ourselves with one single number all the time.

Therefore, the python list comes to our help.

Now we can house as many numbers as we need to place in one place.

For simplicity, we keep two numbers inside two square brackets in the following code.

vector = [4, 7]
print(vector)
# output
[4, 7]

print(vector[0])
print(vector[1])
# output
4
7

We call this new variable “vector”. 

Why?

Because it represents a vector in algebra.

How is that possible?

Well, see the second half of the code. 

The list of numbers starts with index 0, and then it moves in the right direction. As a result, the second number has the index 1.

And with the help of the indices we can get each value.

In algebra, a vector has a magnitude or size and a direction.

Here exactly that happens.

Vector to Matrix

As we proceed, we also see an interesting pattern. 

We can place a list of numbers, that is any number of vectors inside another list. Right? 

Let’s do that.

Watch the code below and see how it happens.

matrix = [[4, 7], 
          [5, 6]
          ]
print(matrix)
# output
[[4, 7], [5, 6]]

matrix = [vector, vector]
# output
print(matrix)
[[4, 7], [4, 7]]

We hope now it makes sense. 

We can place as many vectors as possible inside a list. As a result, it makes a matrix.

A vector has one dimension. Because it is a list of numbers and moves towards the right directions with its indices.

What is dimension and how it affects vector and matrix?

That is an interesting question and we need to imagine a graph where we have two axes. X and Y.

Since a vector moves horizontally towards the right direction, we can place it on the X axis.

Therefore, it’s one dimension. 

We can also call it a row or a single entry.

But we can place a matrix in the graph differently. Because it has two dimensions, we can plot it in X and Y axes.

Certainly, we will discuss it later in the next section with diagrams.

Matrix to Tensor

Since we can place two vectors inside a list, and make it a matrix.

In the same vein, we can place many matrices inside a list.

For that reason, it will add one more dimension first.

Next, we can call it by the name “tensor”. 

Actually, tensor refers to the “n” number of dimensions.

Therefore, a vector or a matrix is also a kind of tensor.

Let’s see the code.

tensor = [
          
           [
            [4, 7], [5, 6]
           ], 
           [
            [4, 7], [5, 6]
           ], 
           [
            [4, 7], [5, 6]
           ]
          
]
print(tensor)

[[[4, 7], [5, 6]], [[4, 7], [5, 6]], [[4, 7], [5, 6]]]

tensor = [matrix, 
          matrix, 
          matrix
          ]
print(tensor)

[[[4, 7], [5, 6]], [[4, 7], [5, 6]], [[4, 7], [5, 6]]]

Now when we will test the same code using TensorFlow, it will appear easy.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Flutter, Dart and Algorithm

Twitter


Posted

in

, ,

by

Comments

6 responses to “Scalar, Vector, Matrix, and Tensor in Python”

  1. […] our previous section we have seen how we can work with python data structures. Now we will do the same with […]

  2. […] We have seen how to create a tensor. Not only that, we have also seen what is the difference between a scalar, a vector and a tensor. […]

  3. […] While working with the TensorFlow, we won’t create the tensors. The Neural Network or Deep Learning does that.  […]

  4. […] can change the values of a tensor just like any scalar quantity. Although it sounds strange, it’s true. Let’s see it in Google […]

  5. […] After that, second value of the first row multiplies the second value of the first column. […]

Leave a Reply