codingstreets
Search
Close this search box.
numpy-universal-function

Introduction to NumPy Universal Function

In This Article, You Will Learn About Numpy Universal Function.

Numpy Universal Function – Before moving ahead, let’s know about Python Numpy Tutorial.

What are ufuncs?

A universal function, or ufunc, is a function that works on ndarrays element-by-element, supporting array broadcasting and typecasting, as well as several other standard features.

Why to use Numpy ufuncs?

Implementation of unfuncs in NumPy is to vectorization that is much faster than Iterating through elements.

It includes also additional arguments –

where -  Boolean array, condition that defines where operations should occur.

dtype – Represents the return type of elements.

out - Returned the array with the copied valued.

What is vectorization?

Converting Iterating statements or elements into a vector is called vectorization.

Adding the Elements of Two Lists

It is possible to do this by iterating over both lists, then adding each element.

Example – Using built-in Python function zip() to add two lists elements.

a = [11, 12, 13, 14]
b = [15, 16, 17, 18]

c = []

for x, y in zip(a, b):
    c.append(x + y)
print(c)
Output - 

[26, 28, 30, 32]

As shown above, it returned an array of shape 1×4 after adding both list’s numbers.

Now, using NumPy ufunc add() to add two lists for the same result.

Example – Using Python Numpy ufunc add() to add two lists elements.

import numpy as np

a = [11, 12, 13, 14]
b = [15, 16, 17, 18]

c = np.add(a, b)

print(c)
numpy-universal-function

As shown above, it returned an array of shape 1×4 after adding both list’s numbers.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on