Quick Guide To NumPy Ufuncs
In this article, we will discuss NumPy ufuncs in detail, including their syntax, what they are, how they work, and how to use them effectively in Python.
NumPy ufuncs – What are they?
NumPy Ufuncs are essentially functions that operate on arrays in an element-wise manner, meaning that they perform the same operation on each element of an array.
This is different from regular Python functions, which typically operate on individual elements one at a time.
It is referred to as Ufunc which stands for Universal Functions in NumPy. According to Numpy Ufunc, they are NumPy functions which perform operations on the ndarray object.
NumPy provides a wide range of ufuncs for performing mathematical and logical operations on arrays, including arithmetic functions like addition, subtraction, multiplication, and division, as well as trigonometric functions, exponential functions, and more.
Here’s a brief overview of some of the most commonly used ufuncs in NumPy:
|np.add||Adds two arrays element-wise.|
|np.subtract||Subtracts two arrays element-wise.|
|np.multiply||Multiplies two arrays element-wise.|
|np.divide||Divides two arrays element-wise.|
|np.sin||Computes the sine of each element in an array.|
|np.cos||Computes the cosine of each element in an array.|
|np.exp||Computes the exponential of each element in an array.|
|np.log||Computes the natural logarithm of each element in an array.|
Why use NumPy ufuncs?
It is important to note that ufuncs are utilized to perform vectorization in NumPy, which is much more efficient than iterating over objects.
In addition, they provide broadcasting and additional methods like reduce, accumulate, etc., that are very helpful in the computation process.
Additionally, ufuncs accepts the following arguments:
|where||is a boolean array or expression indicating where operations should occur.|
|dtype||specifies the object’s return type.|
|out||the result array to copy the outcome value to.|
What is Vectorization?
In Numpy Ufunc, the technique of changing iterative statements into vector-based actions is known as vectorization.
Due to the fact that modern CPUs are designed specifically for such operations, they are more efficient than before.
Add the Elements of Two Lists
even list: [0, 2, 4, 6, 8, 10] odd list: [1, 3, 5, 7, 9, 11]
It is possible to add every item to the Numpy Ufunc by looping over the two lists.
We can also utilize Python’s built-in zip() method to sum two lists instead of ufunc:
Above example defines two lists of numbers: even_list and odd_list, which contain even and odd integers respectively. The code then creates an empty list called list_sum.
Using a for loop, it iterates over the elements of both even_list and odd_list simultaneously using the built-in zip() function, and adds the corresponding elements together. The sum of each pair of elements is then appended to list_sum.
Finally, the code prints the values stored in list_sum. The output will be a new list of the same length as even_list and odd_list, where each element is the sum of the corresponding elements from the two original lists. Below is an image of output:
Now create two reverse lists, then add them with the zip() method:
There is a built-in NumPy function that can be utilized to do this, known as add(x, y), which will generate the same outcome.
In Numpy we can utilize the add() function in the addition of two list:
Apply the ufunc add() then sum the two reverse number lists:
The above example demonstrates how to add two lists element-wise using NumPy’s add function.
First, two lists reverse_list1 and reverse_list2 are defined with integer values.
Then, the add function from the NumPy library is used to add the two lists element-wise, resulting in a new list called list_sum.