Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
numpy.zeros() in Python
Next article icon

Vectorization in Python

Last Updated : 04 Oct, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

We know that most of the application has to deal with a large number of datasets. Hence, a non-computationally-optimal function can become a huge bottleneck in your algorithm and can take result in a model that takes ages to run. To make sure that the code is computationally efficient, we will use vectorization.

Time complexity in the execution of any algorithm is very crucial deciding whether an application is reliable or not. To run a large algorithm in as much as optimal time possible is very important when it comes to real-time application of output. To do so, Python has some standard mathematical functions for fast operations on entire arrays of data without having to write loops. One of such library which contains such function is numpy. Let’s see how can we use this standard function in case of vectorization.

What is Vectorization ?
Vectorization is used to speed up the Python code without using loop. Using such a function can help in minimizing the running time of code efficiently. Various operations are being performed over vector such as dot product of vectors which is also known as scalar product as it produces single output, outer products which results in square matrix of dimension equal to length X length of the vectors, Element wise multiplication which products the element of same indexes and dimension of the matrix remain unchanged.

We will see how the classic methods are more time consuming than using some standard function by calculating their processing time.

outer(a, b): Compute the outer product of two vectors.
multiply(a, b): Matrix product of two arrays.
dot(a, b): Dot product of two arrays.
zeros((n, m)): Return a matrix of given shape and type, filled with zeros.
process_time(): Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep.

Dot Product:
Dot product is an algebraic operation in which two equal length vectors are being multiplied such that it produces a single number. Dot Product often called as inner product. This product results in a scalar number. Let’s consider two matrix a and b of same length, the dot product is done by taking the transpose of first matrix and then mathematical matrix multiplication of a’(transpose of a) and b is followed as shown in the figure below.

Pictorial representation of dot product –

Below is the Python code:




# Dot product
import time
import numpy
import array
  
# 8 bytes size int
a = array.array('q')
for i in range(100000):
    a.append(i);
  
b = array.array('q')
for i in range(100000, 200000):
    b.append(i)
  
# classic dot product of vectors implementation 
tic = time.process_time()
dot = 0.0;
  
for i in range(len(a)):
      dot += a[i] * b[i]
  
toc = time.process_time()
  
print("dot_product = "+ str(dot));
print("Computation time = " + str(1000*(toc - tic )) + "ms")
   
  
n_tic = time.process_time()
n_dot_product = numpy.dot(a, b)
n_toc = time.process_time()
  
print("\nn_dot_product = "+str(n_dot_product))
print("Computation time = "+str(1000*(n_toc - n_tic ))+"ms")
 
 

Output:

  dot_product = 833323333350000.0  Computation time = 35.59449199999999ms    n_dot_product = 833323333350000  Computation time = 0.1559900000000225ms  

 
Outer Product:
The tensor product of two coordinate vectors is termed as Outer product. Let’s consider two vectors a and b with dimension n x 1 and m x 1 then the outer product of the vector results in a rectangular matrix of n x m. If two vectors have same dimension then the resultant matrix will be a square matrix as shown in the figure.

Pictorial representation of outer product –

Below is the Python code:




# Outer product
import time
import numpy
import array
  
a = array.array('i')
for i in range(200):
    a.append(i);
  
b = array.array('i')
for i in range(200, 400):
    b.append(i)
  
# classic outer product of vectors implementation 
tic = time.process_time()
outer_product = numpy.zeros((200, 200))
  
for i in range(len(a)):
   for j in range(len(b)):
      outer_product[i][j]= a[i]*b[j]
  
toc = time.process_time()
  
print("outer_product = "+ str(outer_product));
print("Computation time = "+str(1000*(toc - tic ))+"ms")
   
n_tic = time.process_time()
outer_product = numpy.outer(a, b)
n_toc = time.process_time()
  
print("outer_product = "+str(outer_product));
print("\nComputation time = "+str(1000*(n_toc - n_tic ))+"ms")
  
 
 

Output:

  outer_product = [[     0.      0.      0. ...,      0.      0.      0.]   [   200.    201.    202. ...,    397.    398.    399.]   [   400.    402.    404. ...,    794.    796.    798.]   ...,    [ 39400.  39597.  39794. ...,  78209.  78406.  78603.]   [ 39600.  39798.  39996. ...,  78606.  78804.  79002.]   [ 39800.  39999.  40198. ...,  79003.  79202.  79401.]]    Computation time = 39.821617ms    outer_product = [[    0     0     0 ...,     0     0     0]   [  200   201   202 ...,   397   398   399]   [  400   402   404 ...,   794   796   798]   ...,    [39400 39597 39794 ..., 78209 78406 78603]   [39600 39798 39996 ..., 78606 78804 79002]   [39800 39999 40198 ..., 79003 79202 79401]]    Computation time = 0.2809480000000031ms  

 
Element wise Product:
Element-wise multiplication of two matrices is the algebraic operation in which each element of first matrix is multiplied by its corresponding element in the later matrix. Dimension of the matrices should be same.
Consider two matrices a and b, index of an element in a is i and j then a(i, j) is multiplied with b(i, j) respectively as shown in the figure below.

Pictorial representation of Element wise product –

Below is the Python code:




# Element-wise multiplication
import time
import numpy
import array
  
a = array.array('i')
for i in range(50000):
    a.append(i);
  
b = array.array('i')
for i in range(50000, 100000):
    b.append(i)
  
# classic element wise product of vectors implementation 
vector = numpy.zeros((50000))
  
tic = time.process_time()
  
for i in range(len(a)):
      vector[i]= a[i]*b[i]
  
toc = time.process_time()
  
print("Element wise Product = "+ str(vector));
print("\nComputation time = "+str(1000*(toc - tic ))+"ms")
   
  
n_tic = time.process_time()
vector = numpy.multiply(a, b)
n_toc = time.process_time()
  
print("Element wise Product = "+str(vector));
print("\nComputation time = "+str(1000*(n_toc - n_tic ))+"ms")
 
 

Output:

  Element wise Product = [  0.00000000e+00   5.00010000e+04   1.00004000e+05 ...,   4.99955001e+09     4.99970000e+09   4.99985000e+09]    Computation time = 23.516678000000013ms    Element wise Product = [        0     50001    100004 ..., 704582713 704732708 704882705]  Computation time = 0.2250640000000248ms  


Next Article
numpy.zeros() in Python
author
rakesh797
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

  • numpy.vdot() in Python
    Prerequisite - numpy.dot() in Python numpy.vdot(vector_a, vector_b) returns the dot product of vectors a and b. If first argument is complex the complex conjugate of the first argument(this is where vdot() differs working of dot() method) is used for the calculation of the dot product. It can handle
    2 min read
  • numpy.subtract() in Python
    numpy.subtract() function is used when we want to compute the difference of two array.It returns the difference of arr1 and arr2, element-wise. Syntax : numpy.subtract(arr1, arr2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj], ufunc 'subtract
    4 min read
  • numpy.zeros() in Python
    numpy.zeros() function creates a new array of specified shapes and types, filled with zeros. It is beneficial when you need a placeholder array to initialize variables or store intermediate results. We can create 1D array using numpy.zeros(). Let's understand with the help of an example: [GFGTABS] P
    2 min read
  • Vectorization in NumPy with Practical Examples
    Vectorization in NumPy is a method of performing operations on entire arrays without explicit loops. This approach leverages NumPy's underlying C implementation for faster and more efficient computations. By replacing iterative processes with vectorized functions, you can significantly optimize perf
    4 min read
  • NumPy Tutorial - Python Library
    NumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays. At its core it introduces the ndarray (n-dimen
    3 min read
  • Operations on Vectors in R
    Vectors are the most basic data types in R. Even a single object created is also stored in the form of a vector. Vectors are nothing but arrays as defined in other languages. Vectors contain a sequence of homogeneous types of data. If mixed values are given then it auto converts the data according t
    3 min read
  • Python List Slicing
    Python list slicing is fundamental concept that let us easily access specific elements in a list. In this article, we’ll learn the syntax and how to use both positive and negative indexing for slicing with examples. Example: Get the items from a list starting at position 1 and ending at position 4 (
    5 min read
  • List of Vectors in R
    Vectors are a sequence of elements belonging to the same data type. A list in R, however, comprises of elements, vectors, variables or lists which may belong to different data types. In this article, we will study how to create a list consisting of vectors as elements and how to access, append and d
    5 min read
  • Unpacking a Tuple in Python
    Tuple unpacking is a powerful feature in Python that allows you to assign the values of a tuple to multiple variables in a single line. This technique makes your code more readable and efficient. In other words, It is a process where we extract values from a tuple and assign them to variables in a s
    2 min read
  • numpy.take() in Python
    The numpy.take() function returns elements from array along the mentioned axis and indices. Syntax: numpy.take(array, indices, axis = None, out = None, mode ='raise') Parameters : array : array_like, input array indices : index of the values to be fetched axis : [int, optional] axis over which we ne
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences