NbShare
  • Nbshare Notebooks

  • Table of Contents

  • Python Utilities

    • How To Install Jupyter Notebook
    • How to Upgrade Python Pip
    • How To Use Python Pip
  • Python

    • Python Datetime
    • Python Dictionary
    • Python Generators
    • Python Iterators and Generators
    • Python Lambda
    • Python Sort List
    • String And Literal In Python 3
    • Strftime and Strptime In Python
    • Python Tkinter
    • Python Underscore
    • Python Yield
  • Pandas

    • Aggregating and Grouping
    • DataFrame to CSV
    • DF to Numpy Array
    • Drop Columns of DF
    • Handle Json Data
    • Iterate Over Rows of DataFrame
    • Merge and Join DataFrame
    • Pivot Tables
    • Python List to DataFrame
    • Rename Columns of DataFrame
    • Select Rows and Columns Using iloc, loc and ix
    • Sort DataFrame
  • PySpark

    • Data Analysis With Pyspark
    • Read CSV
    • RDD Basics
  • Data Science

    • Confusion Matrix
    • Decision Tree Regression
    • Logistic Regression
    • Regularization Techniques
    • SVM Sklearn
    • Time Series Analysis Using ARIMA
  • Machine Learning

    • How To Code RNN and LSTM Neural Networks in Python
    • PyTorch Beginner Tutorial Tensors
    • Rectified Linear Unit For Artificial Neural Networks Part 1 Regression
    • Stock Sentiment Analysis Using Autoencoders
  • Natural Language
    Processing

    • Opinion Mining Aspect Level Sentiment Analysis
    • Sentiment Analysis using Autoencoders
    • Understanding Autoencoders With Examples
    • Word Embeddings Transformers In SVM Classifier
  • R

    • DataFrame to CSV
    • How to Create DataFrame in R
    • How To Use Grep In R
    • How To Use R Dplyr Package
    • Introduction To R DataFrames
    • Tidy Data In R
  • A.I. News
NbShare Notebooks
  • Publish Your Post On nbshare.io

  • R Python Pandas Data Science Excel NLP Numpy Pyspark Finance

Understand Tensors With Numpy

In this notebook, I will explain tensors with Python Numpy. For numpy refresher please checkout following resource...
https://www.nbshare.io/notebook/692176011/Numpy-Basics/

In [1]:
import numpy as np

Before we delve in to tensors. We need to understand first what is Scalars, Vectors, Arrays and Matrices.

Scalar - Scalar is just a number. Example number 55 is a scalar. Scalar has no dimensions.
Vector - Vector is one dimensional array or scalar with orientation (i.e. horizontal or vertical). Vector can be 1,2 or n dimensional.
Array - Array is a computer language term used in data structures. Array is a collection of similar data elements.
Matrix - Matrix is a mathematical term. It is a 2D array of numbers represented in rows and columns.

What is Vector

Example of 3 dimensional Vector is shown below.

In [2]:
threeDVector = np.array([1,2,3])
In [3]:
threeDVector
Out[3]:
array([1, 2, 3])
In [4]:
threeDVector.shape
Out[4]:
(3,)

What is Matrix

Matrix is 2 dimensional array.

In [5]:
np.array([[1,2,3],[4,5,6]])
Out[5]:
array([[1, 2, 3],
       [4, 5, 6]])

What is Tensor

Tensor is a mathematical object with more than two dimensions. The number of dimensions of an array defines the order of a Tensor. For example a scalar is 0 order tensor. Similarly vector is a 1 order tensor. A 2D matrix is 2nd order Tensor so on and so forth.

Below is an example of vector which is 1 order tensor.

In [6]:
np.array([1,2,3])
Out[6]:
array([1, 2, 3])

Let us define a two dimensional tensor.

In [7]:
twodTensor = np.arange(36).reshape(6,6)
In [8]:
twodTensor
Out[8]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

Let us access the ist row of above tensor

In [9]:
twodTensor[0]
Out[9]:
array([0, 1, 2, 3, 4, 5])
In [10]:
twodTensor.shape
Out[10]:
(6, 6)

Three dimensional Tensor example is shown below...

In [11]:
threedTensor = np.arange(36).reshape(4,3,3)
In [12]:
threedTensor
Out[12]:
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]],

       [[27, 28, 29],
        [30, 31, 32],
        [33, 34, 35]]])

Note the 3 enclosing brackets - array[[[]]] which indicates it is 3 dimensional tensor.

Add new dimension or axis to a tensor.

In [13]:
t3t = np.arange(36).reshape(4,3,3)

Let us make our 3 dimension tensor to a 4 dimension tensor.

In [14]:
t4t = t3t[np.newaxis,:,:]

Note the 4 enclosing brackets in the below array.

In [15]:
t4t
Out[15]:
array([[[[ 0,  1,  2],
         [ 3,  4,  5],
         [ 6,  7,  8]],

        [[ 9, 10, 11],
         [12, 13, 14],
         [15, 16, 17]],

        [[18, 19, 20],
         [21, 22, 23],
         [24, 25, 26]],

        [[27, 28, 29],
         [30, 31, 32],
         [33, 34, 35]]]])

Note our 4th dimension has only 1 element (the 3x3 array). Let us see that using tensor.shape

In [16]:
t4t.shape
Out[16]:
(1, 4, 3, 3)
In [17]:
t4t[0] #4th dimension
Out[17]:
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]],

       [[27, 28, 29],
        [30, 31, 32],
        [33, 34, 35]]])

Arithmetic with Tensors

Tensors Arithemetic is mathematical operations on vector and matrix.

Example - Find the dot product of two 3 dimensional vectors, we can use np.dot

In [18]:
array1 = np.array([4,4,3])
array2 = np.array([2,2,3])
In [19]:
array1.shape
Out[19]:
(3,)
In [20]:
array2.shape
Out[20]:
(3,)
In [21]:
np.dot(array1,array2)
Out[21]:
25

Dot product two matrix is shown below.

In [22]:
array1 = np.array([[1,2,3],[4,4,3]])
array2 = np.array([[2,2,3],[1,2,3]])
In [23]:
print(array1)
print("\n")
print(array2)
[[1 2 3]
 [4 4 3]]


[[2 2 3]
 [1 2 3]]
In [24]:
np.dot(array1,array2) #this will throw error because row of first matrix is not equal to column of second matrix - Refer matrix multiplication fundamentals
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [24], in <cell line: 1>()
----> 1 np.dot(array1,array2)

File <__array_function__ internals>:180, in dot(*args, **kwargs)

ValueError: shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)
In [25]:
array1 = np.array([[1,2,3],[4,4,3],[1,1,1]])
array2 = np.array([[2,2,3],[1,2,3],[1,2,3]])

Dot product for above matrices will work because both the arrays are 3x3 matrices.

In [26]:
np.dot(array1,array2)
Out[26]:
array([[ 7, 12, 18],
       [15, 22, 33],
       [ 4,  6,  9]])

Related Notebooks

  • PyTorch Beginner Tutorial Tensors
  • Learn Pygame With Examples
  • Understanding Autoencoders With Examples
  • Data Cleaning With Python Pdpipe
  • Understanding Standard Deviation With Python
  • Data Analysis With Pyspark Dataframe
  • With Open Statement in Python
  • How To Install Python With Conda
  • How to Sort Pandas DataFrame with Examples

Register

User Already registered.


Login

Login

We didn't find you! Please Register

Wrong Password!


Register
    Top Notebooks:
  • Data Analysis With Pyspark Dataframe
  • Strftime and Strptime In Python
  • Python If Not
  • Python Is Integer
  • Dictionaries in Python
  • How To install Python3.9 With Conda
  • String And Literal In Python 3
  • Privacy Policy
©