Numpy's Universal Functions
Before defining what ufunc are, we need to know why we need ufunc at first place. In Python, for loops are used to run operation on each item in the list. However, for loops are very slow as it runs on each item in the list, one-by-one.
On the other hand, we can make this iteration process faster by applying vectorized operations using NumPy universal functions ufunc, whose main purpose is to quickly execute repeated operations on values in NumPy arrays.
Vectorization is the process of converting an algorithm from operating on a single value at a time to operating on a set of values at one time.
1. UFUNC BASICS
1.1. Arithmetics
In the following examples, we will work with basic arithmetic operations like addition, subtraction, multiplication, division, square etc, on the NumPy array
import numpy as np
# creating an array first, using arange
arr = np.arange(1,11)
print(f"Array on which to perform ufunc: \n{arr}")
# addition
print(f"array + 1 is:\n{arr+1}")
# subtration
print(f"array - 1 is:\n{arr-1}")
# multiplication
print(f"array * 5 is:\n{arr*5}")
# division
print(f"array / 2 is:\n{arr/5}")
# negative * array
print(f"-array is:\n{-arr}")
# square
print(f"array sqaure is:\n{arr**2}")
# modulus operator
print(f"array % is:\n{arr%2}")We can use np.abs function to find the absolute value of NumPy array elements;
1.2. Trigonometric Functions
These trigonometric functions work on radians, so if we have an array of angles, we first need to convert it to radians by using np.deg2rad(angles) function. Once we have all array value in radians, only then we can call trigonometric functions.
Now, we will demonstrate the use of np.arcsin, np.arcos, and np.arctan functions to return the trigonometric inverse of sin, cos, and tan values of angle in the array
1.3. Statistics Functions
All of the following statistical functions can be applied, along axis of your choice using optional axes kwarg
1.4. Log and Exponent
Letβs see how to calculate exponential of elements in a Numpy array:
Now, letβs take log of each of these values -natural log, log with base 2 and 10. As we are applying these log on their corresponding exponent values, we will get our original array back in the output.
2. BEYOND BASIC UFUNC
2.1. Specifying output
We can specify the output location, to save the array
2.2. Reduce
In this section, we will discuss .reduce method which repeatedly applies a given operation (let suppose, add or multiply) to all the elements of an array and give the final result. What? Keep reading to understand reduce method and how it differs from its conceptual equivalents.
a. 1D
For taking sum, both np.add.reduce and np.sum produces the same result (for 1D array) but former is faster than later. The same logic goes for the results of np.multiply.reduce and its equivalent np.prod
b. 2D
However, for 2D array, these above mentioned equivalence isnβt there, in terms of final results.
Besides, we can use the optional axis keyword argument for all of functions above to perform the operation on a given axis. For .reduce method, the default value of axis=0
2.3. Accumulate
np.accumulate function perform a given operation (let suppose, add or multiply) and passes its accumulated resulted to the next element in the array. Therefore, we can see how, we reached at the final result in the array.
a. 1D
However, for 1D arrays, np.add.accumulate and np.multiply.accumulate are logical equivalents to np.cumsum and np.cumprod
b. 2D
However, for 2D array, these above mentioned equivalence isnβt there
Besides, we can use the optional axis keyword argument for all of functions above to perform the operation on a given axis. For .reduce method, the default value of axis=0
2.4. Outer products
.outer method can be applied to any ufunc to compute the output of all pairs of two different inputs. Examples, will make the concept clear:
For example, np.add.outer(a1,a1) has added each element of a1, on every element in the row, once in each row:
first row is
[0,1,2,3]which is0added to every element in rowsecond rows is
[1,2,3,4]which is1added to every element in the rowthird rows is
[2,3,4,5]which is2added to every element in the rowfourth rows is
[3,4,5,6]which is3added to every element in the row
Last updated
Was this helpful?