Universal Functions (ufunc)
A universal function (ufunc) is a function that operates on ndarrays. A ufunc takes a fixed number of specific inputs and produces a fixed number of specific outputs. ufuncs can also take optional arguments: where, dtype, and out (output array where the return value should be copied). There are currently more than 60 universal functions defined in numpy.
Math operations
You can use np.add(), np.subtract(), np.multiply(), np.divide(), np.power(), np.remainder(), np.divmod(), and the np.absolute() functions. You can use the same syntax for all functions. Let's see some of them:
add()
The np.add() function sums the content of two arrays, and returns the results in a new array.
import numpy as np
arr_1 = np.array([9, 9, 8, 1, 8])
arr_2 = np.array([7, 10, 8, 3, 1])
arr_3 = np.add(arr_1, arr_2)
print(arr_3)
[16 19 16 4 9]
power()
The first array elements are raised to powers from the second array elements in the np.power() method.
x = [1, 2, 3, 4]
y = [2, 4, 1, 3]
k = np.power(x,y)
print(k)
[ 1 16 3 64]
You need to calculate [12 24 31 43] for the result above.
remainder()
The np.remainder() returns the element-wise remainder of the division.
arr_1 = np.array([9, 6, 8, 7, 5])
arr_2 = np.array([7, 3, 2, 3, 4])
arr_3 = np.remainder(arr_1, arr_2)
print(arr_3)
[2 0 0 1 1]
You can get the same result with the mod() function.
divmod()
The np.divmod() returns the element-wise quotient and remainder simultaneously.
arr_1 = np.array([9, 6, 8, 7, 5])
arr_2 = np.array([7, 3, 2, 3, 4])
arr_3 = np.divmod(arr_1, arr_2)
print(arr_3)
(array([1, 2, 4, 2, 1]), array([2, 0, 0, 1, 1]))
absolute()
The np.abs() method calculates the absolute value element-wise.
arr_1 = np.array([-9, -6, -8, -7, -5])
new_arr = np.abs(arr_1)
print(new_arr)
[9 6 8 7 5]
Rounding decimals
You can use truncation, fix, round, floor, and ceil functions for rounding decimals.
trunc()
numpy.trunc() rounds to the nearest integer towards zero.
arr_1 = np.array([1.3, 2.5, 4.6])
new_arr = np.trunc(arr_1)
print(new_arr)
[1. 2. 4.]
You can get the same result with the numpy.fix() function.
floor()
The np.floor() function rounds off decimals to the nearest lower integer.
arr_1 = np.array([-1.3, -2.5, -4.6])
new_arr = np.floor(arr_1)
print(new_arr)
[-2. -3. -5.]
round()
The np.round() evenly rounds to the given number of decimals.
arr_1 = np.array([-1.34685, -2.54232, -4.64242])
new_arr = np.round(arr_1, 2)
print(new_arr)
[-1.35 -2.54 -4.64]
ceil()
The np.ceil() returns the ceiling of the input, element-wise:
arr_1 = np.array([-1.3, -2.5, -4.6])
new_arr = np.ceil(arr_1)
print(new_arr)
[-1. -2. -4.]
Logs
You can use log base 2 or 10 of the given array:
import numpy as np
arr = np.arange(1, 8)
print(np.log2(arr))
*numpy.arange returns evenly spaced values within a given interval.
[0. 1. 1.5849625 2. 2.32192809 2.5849625
2.80735492]
log base 10:
import numpy as np
arr = np.array([10, 100, 1000])
print(np.log10(arr))
[1. 2. 3.]
The natural logarithm is the inverse of the exponential function.
Summation
sum()
The np.sum() calculates the sum of array elements over a given axis:
arr_1 = np.array([3, 2, 4])
new_arr = np.sum(arr_1)
print(new_arr)
The result is 9. Please note that np.sum() and np.add() functions are different.
You can also sum over an axis:
arr_1 = np.array([3, 2, 4])
arr_2 = np.array([3, 2, 4])
new_arr = np.sum([arr_1, arr_2], axis=1)
print(new_arr)
[9 9]
cumsum()
The np.cumsum() returns the cumulative sum of the elements along a given axis.
arr = np.array([2, 4, 6])
new_arr = np.cumsum(arr)
print(new_arr)
[ 2 6 12]
Products
prod()
The np.prod() returns the product of array elements over a given axis.
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([1, 2, 3, 4])
x = np.prod([arr1, arr2])
print(x)
The result (1 * 2 * 3 * 4 * 1 * 2 * 3 * 4) is 576.
Differences
diff()
The np.diff() method calculates the n-th discrete difference along the given axis.
arr1 = np.array([1, 2, 3, 4])
x = np.diff(arr1)
print(x)
The result is [1 1 1] because 2-1=1, 3-2=1, 4-3=1.
Finding Lowest Common Multiple (LCM) and Greatest Common Denominator (GCD)
Finding Lowest Common Multiple (LCM)
The np.lcm() returns the lowest common multiple of the array:
arr = np.array([4, 5, 20])
x = np.lcm.reduce(arr)
print(x)
The result is 20.
Greatest Common Denominator (GCD)
The numpy.gcd() returns the greatest common divisor of two elements:
arr = np.array([4, 8, 12])
x = np.gcd.reduce(arr)
print(x)
The result is 4.
Set operations
A set in mathematics is a collection of unique elements. You can use unique method to find unique elements from an array:
arr = np.array([4, 8, 12, 4, 6, 8])
x = np.unique(arr)
print(x)
[ 4 6 8 12]
The set arrays can have only one dimension.
Finding union union1d()
numpy.union1d() finds the union of two arrays.
arr = np.array([4, 8, 12])
arr2 = np.array([4,9,15])
x = np.union1d(arr, arr2)
print(x)
[ 4 8 9 12 15]
np.union1d joins the two arrays. As it's a set array, common elements don't repeat.
Finding intersection intersect1d()
np.intersect1d() finds the intersection of two arrays.
arr = np.array([4, 8, 12])
arr2 = np.array([4,9,15])
x = np.intersect1d(arr, arr2)
print(x)
[4]
Finding Difference setdiff1d()
np.setdiff1d() finds the set difference of two arrays. It returns the unique values in the first array that are not in the second array.
arr = np.array([4, 8, 12])
arr2 = np.array([4,9,15])
x = np.setdiff1d(arr, arr2)
print(x)
[ 8 12]
Finding Symmetric Difference setxor1d()
np.setxor1d() finds the set exclusive-or of two arrays.
arr = np.array([4, 8, 12])
arr2 = np.array([4,9,15])
x = np.setxor1d(arr, arr2)
print(x)
[ 8 9 12 15]
setxor1d returns the unique values of two arrays.
Please note that functions always return sorted, unique values.
Create your own ufunc
frompyfunc()
The numpy.frompyfunc helps you create your ufunc. It takes 3 arguments: the name of the function, the number of input array(s) and the number of output array(s).
def subt(x, y):
return x-y
subt = np.frompyfunc(subt, 2, 1)
print(subt([8, 7, 6], [3, 2, 1]))
[5 5 5]