Skip to content

NumPy Universal Functions (ufuncs)

A ufunc (universal function) is a function that performs element-wise operations on data in ndarrays. Think of them as fast, vectorized wrappers around simple functions that would otherwise take one scalar in and return one scalar out.

Examples:

  • np.sqrt, np.exp, np.log
  • np.sin, np.cos
  • np.maximum, np.minimum

They are faster and cleaner than Python loops.

sqrt
import numpy as np
 
arr = np.array([1, 4, 9, 16])
print(np.sqrt(arr))
diagram Unary vs binary ufuncs mermaid
Unary ufuncs transform one array; binary ufuncs combine two arrays element-wise into one result.

np.sqrt and np.exp are unary ufuncs — they take one array. Functions like np.add or np.maximum are binary — they take two arrays and return one result.

math
import numpy as np
 
x = np.array([1.0, 2.0, 3.0])
print(np.exp(x))
print(np.log(x))
print(np.log10(x))
trig
import numpy as np
 
angles = np.array([0, np.pi/2, np.pi])
print(np.sin(angles))
print(np.cos(angles))
max-min
import numpy as np
 
a = np.array([1, 10, 3])
b = np.array([2, 5, 4])
 
print(np.maximum(a, b))
print(np.minimum(a, b))

Most ufuncs return one array, but a few — like np.modf — return more than one. It splits a float array into fractional and whole-number parts:

modf
import numpy as np
 
arr = np.array([4.5, -8.1, 2.25])
remainder, whole_part = np.modf(arr)
print("remainder:", remainder)
print("whole:", whole_part)

Ufuncs accept an optional out argument to write results in place instead of allocating a new array — handy for saving memory on large arrays.

out
import numpy as np
 
arr = np.array([1.0, 2.0, 3.0])
result = np.zeros_like(arr)
np.add(arr, 1, out=result)
print(result)

Some ufuncs have NaN-safe variants.

nan
import numpy as np
 
arr = np.array([1.0, np.nan, 3.0])
print(np.nanmean(arr))
print(np.nansum(arr))

Ufuncs naturally work with broadcasting.

broadcast
import numpy as np
 
mat = np.array([[1, 2, 3], [4, 5, 6]])
print(np.sqrt(mat))

Continue to: Stacking and Splitting Arrays to combine and break arrays along different axes.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading