Vectorization in Python

| | | | | | | | | | | | | | | | | | | | | | |

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 

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

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

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 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

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

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