Updated March 28, 2023
Introduction to NumPy Vectorize
Python provides different functions to the users. To work with vectorizing, the python library provides a numpy function. The NumPy vectorize accepts the hierarchical order of the numpy array or different objects as an input to the system and generates a single numpy array or multiple numpy arrays. After successive multiple arrays of input, the NumPy vectorize evaluates pyfunc like a python map function, and it helps to define numpy rules. We use numpy vectorization instead of a loop to increase speed. Arrays play a major role in data science, where speed matters. Basically, numpy is an open-source project. In python, numpy is faster than the list. Therefore, processing and manipulating can be done efficiently.
Syntax of NumPy Vectorize
The syntax for NumPy Vectorize is as follows:
vectorize_funcunction = np.vectorize (function, parameter 1, parameter 2….. parameter N)
Explanation
In the above syntax, vectorize_function is a function name, np.vectorize is a numpy class, and function is a user-defined function with parameters. The parameters which we are using in the numpy vector as below.
Different Parameters of Numpy vectorize are as follows.
1. pyfunc: It is used to define the function of python as well as the method, and it must be required. Therefore, it is a callable parameter.
2. otypes: The otypes mean output data type, and it is optional. In otypes, it should be specified as either a list of data types specified or a string of type code characters. For each output, there must be one data specified.
3. doc: The doc is an optional parameter to the docstring. If there is none in doc, then docstring will be pyfunc_doc_str.
4. excluded: This is an optional parameter. This parameter consists of either a set of strings or integers representing the positional or keyword arguments for the functions that will not be vectorized. A set of strings or integers will be passed directly to pyfunc unmodified.
5. cache: The cache is an optional parameter. It will cache the first function call, which generally determines the number of outputs if True and otypes are not given.
6. Signature: It is an optional parameter. It is a generalized universal function. For example, (a, b), (b) -> (a) it is used for vectorized matrix-vector function multiplication. At whatever point it is given, pyfunc will be called with (and it must returned) array with shapes given by the size of looking at focus estimations. By default, Pyfunc is expected to accept scalars as input as well as output.
How does the vectorize function work in NumPy?
- We must install Python on your system.
- We must install numpy using the pip command.
- We required basic knowledge about Python.
- We required basic knowledge about arrays.
- We can perform different operations using the numpy vectorize function.
Let’s see how we can implement a numpy vectorize function on an array. But, first, we see what is the difference between vectorizing and non-vectorize implementation.
1. Vectorize Implementation
It is mainly related to matrices. In vectorize implementation, we execute huge algorithms like machine learning algorithms and neural language algorithms.
Example
import numpy as np
import time
no = 100000
x = np.random.random(no)
y = np.random.random(no)
start = time.time()
z = np.dot(x,y)
end = time.time()
print("Vectorize :" + str((end-start)*1000)+ 'ms')
Explanation
In the above example, we implemented the numpy vectorize function using an array. In this program, we used two arrays, x, and y, with random numbers, and then we used dot product means the multiplication of x and y arrays. Also, we have calculated the total execution time of the x and y array using vectorize. Thus, the vectorize function takes minimum time for execution. Illustrate the end result of the above declaration by using the use of the following snapshot.
2. Non-Vectorize Implementation
In this implementation, we use a loop for implementation purposes non-vectorize implementation takes more time to execute as compared to vectorize implementation.
Example:
import numpy as np
import time
no= 100000
x=np.random.random(no)
y=np.random.random(no)
start = time.time()
z=0
for i in range(no):
z += x[i] + y[i]
end=time.time()
print("Loop :" + str((end-start)*1000)+ 'ms')
Explanation
In the above example, we implemented a non-vectorize numpy. In this example, we used a loop for implementation. Here we have used Loop instead of Vectorize. As a result, the non-vectorize takes more time. Illustrate the end result of the above declaration by using the use of the following snapshot.
Example: numpy vectorize function
import numpy as np
def func1(c, d):
if c > d:
return c - d
else:
return c + d
vfun = np.vectorize(func1)
z=vfun([4, 3, 5, 2], 1)
print(z)
Explanation:
In this example, we have implemented numpy vectorization. We have defined a vectorize function in which m and n are arguments. The Vectorize function used in the above example reduces the length of code. In this example, vfun directly performs the operation on arrays. Illustrate the end result of the above declaration by using the use of the following snapshot.
Example: numpy vectorize docstring
import numpy as np
def func1(p, q):
vecfunc.__doc__
vecfunc = np.vectorize(func1, doc="welcome to python")
a=vecfunc.__doc__
print(a)
Explanation:
For vectorization, the docstring is obtained from the input function unless the docstring is specified. Illustrate the end result of the above declaration by using the use of the following snapshot.
Example: Excluded
import numpy as np
def pval(x, y):
_x = list(x)
res = _x.pop(0)
while _x:
res = res*y + _x.pop(0)
return res
vect_pval = np.vectorize(pval, excluded=['x'])
z=vect_pval(x=[2, 4, 5], y=[1, 2])
print(z)
Explanation:
The excluded is used to stop vectorizing over some arguments. In this example, we implement polynomials as in polyval. Finally, illustrate the end result of the above declaration by using the use of the following snapshot.
In a similar way, we can implement remaining parameters like otype and signature and perform different operations with the help of numpy vectorize.
Conclusion
We hope from this article you have understood about the numpy vectorize function. From the above article, we have learned the basic syntax numpy vectorize function. We have also learned how we can implement them in Python with different examples of each parameter. With the help of the vectorizing function, we reduce the execution time of the algorithm. From this article, we have learned how we can handle numpy vectorize in python.
Recommended Articles
This is a guide to NumPy Vectorize. Here we discuss How does the vectorize function work in NumPy and Examples along with the Explanation. You may also look at the following articles to learn more –