What is Numpy?

Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. Learn basic numpy here. Now we will learn about how the numpy arrays used in linear algebra to solve machine learning problems.

What is a Tensor?

We call the numpy arrays as tensor. Tensor is the central unit of data in in deep learning framework like TensorFlow, Theano. A tensor consists of a set of primitive values shaped into an array of any number of dimensions. A tensor’s rank is its number of dimensions. Here are some examples of various tensors:

“On Tensorflow & Theano all scalars, vectors, matrixs & tensors are tensor. In Numpy they all are arrays.”

Scalar

Any number with rank 0
10
It’s a rank 0 tensor. This is a scalar with shape ()

# importing numpy
import numpy as np

# its a rank 0 tensor or 0 dimentional tensor with shape ()
scalar = np.array(10)
print ('The scalar is: ', scalar)
print (scalar.shape)
print (type(scalar))

('The scalar is: ', array(10))
()
<type 'numpy.ndarray'>

Vector

Vectors are 1 dimentional array with rank 1
[1. ,2., 3., 9.]
It’s a rank 1 tensor. This is a vector with shape (4)

# its a rank 1 tensor or 1 dimentional tensor with shape (4)
vector = np.array([1. ,2., 3., 9.])
print ('The vector is:')
print (vector)
print (vector.shape)
print (type(vector))
The vector is:
[ 1.  2.  3.  9.]
(4,)
<type 'numpy.ndarray'>

Matrix

Matrix are two dimentional array with rank 2
[[1., 2., 3.], [4., 5., 6.]]
It’s a rank 2 tensor. This a matrix with shape (2, 3)

# its a rank 2 tensor or 2 dimentional tensor with shape (2,3)
matrix = np.array([[1., 2., 3.], [4., 5., 6.]])
print ('The matrix is:')
print (matrix)
print (matrix.shape)
print (type(matrix))
The matrix is:
[[ 1.  2.  3.]
 [ 4.  5.  6.]]
(2, 3)
<type 'numpy.ndarray'>

Tensor

Tensors are n dimentional(n>3) array with rank n
[[[1., 2., 3.]], [[7., 8., 9.]]]
It’s a rank 3 tensor with shape (2, 1, 3)

# its a rank 3 tensor or 3 dimentional tensor with shape (2, 1, 3)
tensor = np.array([[[1., 2., 3.]], [[7., 8., 9.]]])
print ('The tensor is:')
print (tensor)
print (tensor.shape)
print (type(tensor))
The tensor is:
[[[ 1.  2.  3.]]

 [[ 7.  8.  9.]]]
(2, 1, 3)
<type 'numpy.ndarray'>

References & Read more:

1: Deep Learning Book tutorial

1: Datacamp Numpy tutorial

2: Scipy Numpy tutorial

3: Tensorflow official Getting Started