Vectorization in Python

| | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

The time complexity of any algorithm is very important in deciding whether an application is reliable or not. Running a large algorithm as quickly as possible is very important when it comes to applying real-time output. To do this, Python has several standard math functions for quickly performing operations on entire arrays of data without having to write loops. One such library that contains such a function is numpy . Let’s see how we can use this standard function in the case of vectorization.

What is vectorization?
Vectorization is used to speed up Python code without using a loop. Using such a function can help minimize your code execution time. Various operations are performed on a vector, such as the dot product of vectors, which is also known as the dot product, because it produces a single output, outer products that result in a square measurement matrix. equal to the length X of the length of vectors, multiplication by an element, which produces the product The element of the indices and the dimension of the matrix remain unchanged.

We will see how classical methods take longer than ordinary functions, 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 c urrent process. It does not include time elapsed during sleep.

Dot product:
Dot product — it is an algebraic operation in which two vectors of equal length are multiplied so that it produces one number. The Dot Product is often referred to as a Inner Product . This product results in a scalar number. Let’s consider two matrices a and b of the same length, the dot product is performed by transposing the first matrix, and then is performed the multiplication of the mathematical matrix by a & # 39; (transpose a) and b, as shown in the image below.

Visual representation of the dot product —

Below here is the Python code:

# Dot product

import time

import numpy

import array


# 8 bytes 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 implementation vectors

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 ( "n_dot_product =" + str (n_dot_product))

print ( "Computation time =" + str ( 1000 * (n_toc - n_tic)) + "ms" )

Exit:

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

Outdoor product:
Tensor product of two coordinate vectors is called External work . Consider two vectors a and b with dimensions nx 1 and mx 1 then the outer product of the vector leads to a rectangular matrix nxm . If two vectors have the same dimension, then the resulting matrix will be a square matrix, as shown in the figure.

Visual representation of the external product —

Below is the Python code:

# Outdoor 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 external product vector implementations

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 ( "Computation time =" + str ( 1000 * (n_toc - n_tic)) + "ms" )

Exit:

 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. ..., 79202. 79401.]] Computation time = 39.821617ms outer_product = [[0 0 0 ..., 0 0 0] [200 201 202 ..., 397 398 399] [400 402 404 ..., 794 79 6 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:
Elementwise multiplication of two matrices — it is an algebraic operation in which each element of the first matrix is ‚Äã‚Äãmultiplied by the corresponding element in the later matrix. The dimensions of the matrices must be the same.
Consider two matrices a and b , the element index in a — these are i and j, then a (i, j) is multiplied by b (i, j) , respectively, as shown in the picture below.

Visual representation of the wise product Element —

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 item-by-item product vector implementations

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 ( "Computation 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 ( "Computation time =" + str ( 1000 * (n_toc - n_tic)) + "ms" )

Exit:

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 

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Vectorization in Python, check other Arrays-related topics.

Want to excel in Python? See our review of the best Python online courses 2022. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Carlo OConnell

Moscow | 2022-12-08

Maybe there are another answers? What Vectorization in Python exactly means?. Checked yesterday, it works!

Cornwall Williams

San Francisco | 2022-12-08

Thanks for explaining! I was stuck with Vectorization in Python for some hours, finally got it done 🤗. Checked yesterday, it works!

Angelo Krasiko

New York | 2022-12-08

I was preparing for my coding interview, thanks for clarifying this - Vectorization in Python in Python is not the simplest one. Checked yesterday, it works!

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically