6️⃣ NumPy ufunc (Universal Functions) – Perform Fast Vectorized Computation
Introduction – Why Learn NumPy ufunc?
NumPy’s ufuncs (universal functions) are fast, element-wise operations built in C and optimized for performance. Whether you’re working on arrays, matrices, or scientific datasets, ufuncs allow seamless arithmetic, logical, trigonometric, and set operations without writing slow loops.
In this guide, you’ll learn:
- What ufuncs are and how to use them efficiently
- How to create custom ufuncs with
frompyfunc - How to apply vectorized math like sum, diff, log, trig, and rounding
- Use cases of ufuncs in data processing and scientific computing
Topics Covered
| Topic | Description |
|---|---|
| NumPy ufunc Intro | What are universal functions and why use them |
| NumPy ufunc Create Function | Creating custom ufuncs using frompyfunc() |
| NumPy ufunc Simple Arithmetic | Element-wise operations: add, subtract, etc. |
| NumPy ufunc Rounding Decimals | Rounding: around, fix, floor, ceil |
| NumPy ufunc Logs | Logarithmic functions like log, log2, log10 |
| NumPy ufunc Summations | sum, cumsum, axis-based operations |
| ✖️ NumPy ufunc Products | prod, cumprod across arrays |
| NumPy ufunc Differences | diff() for adjacent differences |
| NumPy ufunc Finding LCM | Element-wise LCM calculation |
| NumPy ufunc Finding GCD | Element-wise GCD calculation |
| NumPy ufunc Trigonometric | sin, cos, tan and angle conversions |
| NumPy ufunc Hyperbolic | sinh, cosh, tanh etc. |
| NumPy ufunc Set Operations | unique, intersect1d, union1d, etc. |
NumPy ufunc Intro
Universal functions (ufunc) operate element-wise on arrays, enabling faster, more efficient code compared to loops.
import numpy as np
arr = np.array([1, 2, 3])
print(np.add(arr, 5)) # Output: [6 7 8]
Create Custom ufunc
Use np.frompyfunc() to turn any Python function into a ufunc.
def custom_add(x, y):
return x + y
ufunc_add = np.frompyfunc(custom_add, 2, 1)
print(ufunc_add([1, 2], [3, 4])) # Output: [4 6]
Simple Arithmetic
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.add(a, b))
print(np.subtract(a, b))
print(np.multiply(a, b))
print(np.divide(b, a))
Rounding Decimals
x = np.array([1.7, 2.5, -3.2])
print(np.around(x)) # Nearest integer
print(np.floor(x)) # Round down
print(np.ceil(x)) # Round up
Logarithmic Functions
x = np.array([1, 2, 10, 100])
print(np.log(x)) # Natural log
print(np.log2(x)) # Base 2 log
print(np.log10(x)) # Base 10 log
Summations
x = np.array([1, 2, 3, 4])
print(np.sum(x)) # 10
print(np.cumsum(x)) # [1 3 6 10]
✖️ Products
x = np.array([1, 2, 3, 4])
print(np.prod(x)) # 24
print(np.cumprod(x)) # [1 2 6 24]
Differences
x = np.array([10, 15, 25, 35])
print(np.diff(x)) # [5 10 10]
Finding LCM
print(np.lcm.reduce([12, 18])) # 36
Finding GCD
print(np.gcd.reduce([12, 18])) # 6
Trigonometric Functions
x = np.array([0, 30, 60, 90])
radians = np.deg2rad(x)
print(np.sin(radians))
print(np.cos(radians))
print(np.tan(radians))
Hyperbolic Functions
x = np.array([0, 1, 2])
print(np.sinh(x))
print(np.cosh(x))
print(np.tanh(x))
Set Operations
a = np.array([1, 2, 3, 4])
b = np.array([3, 4, 5, 6])
print(np.union1d(a, b)) # [1 2 3 4 5 6]
print(np.intersect1d(a, b)) # [3 4]
print(np.setdiff1d(a, b)) # [1 2]
print(np.setxor1d(a, b)) # [1 2 5 6]
Summary – Recap & Next Steps
NumPy ufuncs are essential tools for high-performance numerical operations. With built-in support for element-wise math, logical processing, and custom functions, they replace slow loops and speed up workflows significantly.
Key Takeaways:
ufuncsoperate element-wise on NumPy arrays with C-speed performance.- Functions like
add,log,diff,sin, andgcdare built-in and optimized. - Set operations and trigonometric/hyperbolic tools broaden their usage in science and engineering.
Real-World Relevance:
From scientific simulations to ML preprocessing and sensor data analysis, NumPy ufuncs help streamline operations in a fraction of the time.
FAQ – NumPy ufunc Basics
What is a ufunc in NumPy?
A ufunc is a fast element-wise function written in C that applies operations to arrays.
How is ufunc different from normal Python functions?
Ufuncs avoid explicit loops and use vectorized operations, making them significantly faster.
Can I create custom ufuncs?
Yes, using np.frompyfunc(func, nin, nout).
What is the use of np.diff()?
It calculates the difference between consecutive elements in an array.
Are ufuncs usable on multi-dimensional arrays?
Yes, all ufuncs support broadcasting and N-dimensional arrays.
Share Now :
