What are 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.lognp.sin,np.cosnp.maximum,np.minimum
They are faster and cleaner than Python loops.
Example: sqrt
import numpy as np
arr = np.array([1, 4, 9, 16])
print(np.sqrt(arr))Unary vs binary ufuncs
flowchart LR A["Unary ufunc
one array in"] --> B["np.sqrt, np.exp, np.log
-> transformed array"] C["Binary ufunc
two arrays in"] --> D["np.add, np.maximum
-> one combined array"] E["Rare: multi-output ufunc"] --> F["np.modf
-> (fractional, integer) arrays"]
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.
Common math ufuncs
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))Trigonometric ufuncs
import numpy as np
angles = np.array([0, np.pi/2, np.pi])
print(np.sin(angles))
print(np.cos(angles))Comparison ufuncs
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))A ufunc that returns two arrays
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:
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)Writing results into an existing array
Ufuncs accept an optional out argument to write results in place instead of allocating a new array — handy for saving memory on large arrays.
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)Working with NaN values
Some ufuncs have NaN-safe variants.
import numpy as np
arr = np.array([1.0, np.nan, 3.0])
print(np.nanmean(arr))
print(np.nansum(arr))Ufuncs + broadcasting
Ufuncs naturally work with broadcasting.
import numpy as np
mat = np.array([[1, 2, 3], [4, 5, 6]])
print(np.sqrt(mat))Check your understanding
What is a ufunc?
Ufuncs like np.sqrt and np.exp are fast, vectorized functions that work element by element.
Show answer
A — A function that applies an operation element-wise to ndarray data — Ufuncs like np.sqrt and np.exp are fast, vectorized functions that work element by element.
For a = np.array([1, 10, 3]) and b = np.array([2, 5, 4]), what does np.maximum(a, b) return?
np.maximum is a binary ufunc that keeps the larger value at each position: 2, 10 and 4.
Show answer
A — [2 10 4] — np.maximum is a binary ufunc that keeps the larger value at each position: 2, 10 and 4.
What does np.nanmean(np.array([1.0, np.nan, 3.0])) return?
nanmean ignores the NaN, so it averages 1.0 and 3.0 to get 2.0. A plain mean would return nan.
Show answer
B — 2.0 — nanmean ignores the NaN, so it averages 1.0 and 3.0 to get 2.0. A plain mean would return nan.
What does the out argument do in np.add(arr, 1, out=result)?
out writes in place, which saves memory on large arrays.
Show answer
B — Writes the result into the existing array result instead of allocating a new one — out writes in place, which saves memory on large arrays.
Next
Continue to: Stacking and Splitting Arrays to combine and break arrays along different axes.
🧪 Try It Yourself
Exercise 1 – Apply a Unary ufunc
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – A Binary ufunc
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – NaN-safe Aggregation
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.