Universal Functions (ufuncs)
Apply built-in element-wise functions such as np.sqrt, np.abs, np.exp, and np.log to arrays without writing loops.
Universal Functions (ufuncs) is a free Pandas & NumPy Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Pandas & NumPy Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Are ufuncs?
Universal functions (ufuncs) run a math operation on every element of an array at once, in fast compiled C — far quicker than a Python loop. ⚡
import numpy as np
a = np.array([1.0, 4.0, 9.0, 16.0])
print(np.sqrt(a)) # [1. 2. 3. 4.]
# Equivalent Python loop would be hundreds of times slowerMath ufuncs: sqrt, exp, log
All your favourite math functions are ufuncs: np.sqrt, np.exp, np.log, and more. They apply to whole arrays at once and handle edge cases gracefully.
import numpy as np
a = np.array([0.0, 1.0, 2.0, 3.0])
print(np.exp(a)) # [1. 2.718 7.389 20.086]
print(np.log(np.exp(a))) # [0. 1. 2. 3.]
print(np.log2(np.array([1, 2, 4, 8]))) # [0. 1. 2. 3.]Trigonometric ufuncs
NumPy's trig ufuncs like np.sin and np.cos expect angles in radians. Use np.deg2rad to convert degrees first.
import numpy as np
angles = np.array([0, 30, 60, 90])
rad = np.deg2rad(angles)
print(np.sin(rad).round(4)) # [0. 0.5 0.866 1. ]
print(np.cos(rad).round(4)) # [1. 0.866 0.5 0. ]np.abs, np.floor, np.ceil, np.round
For rounding, reach for np.floor (down), np.ceil (up), and np.round (nearest). They clean up float values across the whole array in one go.
import numpy as np
a = np.array([-2.7, 0.4, 1.5, 3.9])
print(np.abs(a)) # [2.7 0.4 1.5 3.9]
print(np.floor(a)) # [-3. 0. 1. 3.]
print(np.ceil(a)) # [-2. 1. 2. 4.]
print(np.round(a)) # [-3. 0. 2. 4.]np.maximum and np.minimum
np.maximum(a, b) takes the larger value at each position from two arrays — different from np.max, which returns one number. np.maximum(a, 0) is ReLU!
import numpy as np
a = np.array([-3, 1, 5, -2, 4])
b = np.array([0, 0, 3, 3, 3])
print(np.maximum(a, b)) # [0 1 5 3 4]
print(np.minimum(a, b)) # [-3 0 3 -2 3]
# ReLU
print(np.maximum(a, 0)) # [0 1 5 0 4]np.clip for Bounding Values
np.clip keeps every element inside a range: anything too low jumps up to the minimum, anything too high drops to the maximum. Great for bounding values.
import numpy as np
a = np.array([0.1, 0.5, -0.2, 1.3, 0.9])
clipped = np.clip(a, 0.0, 1.0)
print(clipped) # [0.1 0.5 0. 1. 0.9]ufuncs on Multi-Dimensional Arrays
ufuncs work on arrays of any shape. The math hits each element on its own, and the result keeps the same shape — no reshaping required.
import numpy as np
m = np.array([[1.0, 4.0], [9.0, 16.0]])
print(np.sqrt(m))
# [[1. 2.]
# [3. 4.]]out Parameter for Memory Efficiency
Every ufunc takes an out= argument to write results into an array you already have. That skips making a new array — handy in tight, memory-sensitive loops.
import numpy as np
a = np.array([1.0, 4.0, 9.0])
result = np.empty(3)
np.sqrt(a, out=result) # write directly into result
print(result) # [1. 2. 3.]np.add.reduce and .accumulate
Each ufunc has a .reduce() method that combines all elements (np.add.reduce is just sum) and .accumulate() for running totals.
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(np.add.reduce(a)) # 15
print(np.add.accumulate(a)) # [ 1 3 6 10 15]
print(np.multiply.reduce(a)) # 120np.frompyfunc for Custom ufuncs
np.frompyfunc turns any Python function into a ufunc you can apply to arrays. It's slower than built-ins, so save it for quick prototyping.
import numpy as np
def clamp_positive(x):
return x if x > 0 else 0.0
vfunc = np.frompyfunc(clamp_positive, 1, 1)
a = np.array([-2, 3, -1, 5])
print(vfunc(a)) # [0.0 3 0.0 5] (object dtype)Comparing ufuncs to Python Loops
The speed gap is huge: on a million elements, a ufunc can be around 200 times faster than a Python loop. In number work, always reach for ufuncs first. 🚀
import numpy as np
import time
a = np.random.rand(1_000_000)
start = time.perf_counter()
np.sqrt(a)
print('ufunc:', round(time.perf_counter() - start, 4), 's')
start = time.perf_counter()
[x ** 0.5 for x in a]
print('loop:', round(time.perf_counter() - start, 4), 's')Quick Check
Test your understanding of NumPy ufuncs from this lesson.
Lesson Recap
Nice work! ufuncs do element-wise math in fast C, cover sqrt, exp, log, and trig, and the out= argument saves memory. Next up: aggregation functions.
Frequently asked questions
Is the “Universal Functions (ufuncs)” lesson free?
Yes — the full text of “Universal Functions (ufuncs)” is free to read here on the web, and the Pandas & NumPy Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Pandas & NumPy Academy course, upgrade to CoddyKit PRO.
What will I learn in “Universal Functions (ufuncs)”?
Apply built-in element-wise functions such as np.sqrt, np.abs, np.exp, and np.log to arrays without writing loops. You practise Pandas & NumPy Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Pandas & NumPy Academy?
No prior experience is required. Pandas & NumPy Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Universal Functions (ufuncs)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Pandas & NumPy Academy lesson?
Yes. Every Pandas & NumPy Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Universal Functions (ufuncs)
- Aggregation Functions
- Broadcasting Rules
- Boolean Masking and Fancy Indexing