NumPy ufuncs | Universal functions
Last Updated :
10 Dec, 2025
NumPy ufuncs (universal functions) are fast, vectorized functions that perform element-wise operations on NumPy arrays. They are highly optimized and support features like broadcasting and automatic type handling. NumPy includes many ufuncs for arithmetic, trigonometry, statistics, etc, and they execute operations very fast because they are implemented in optimized C code.
Why use ufuncs?
- Fast vectorized operations: Ufuncs apply calculations to entire arrays at once, making them much faster than Python loops.
- Automatic broadcasting and type handling: They adjust array shapes automatically and handle datatype conversion internally, reducing errors.
- Cleaner and more efficient code: They simplify complex numerical tasks into short, readable code that scales well on large datasets.
Basic Universal Functions (ufunc) in NumPy
1. Trigonometric functions
NumPy provides several trigonometric functions that operate element-wise on arrays. Angles should be in radians, so degree values must be converted using np.deg2rad(). These functions include standard, inverse, and hyperbolic trigonometric operations.
Common Trigonometric ufuncs in NumPy |
|---|
| Function | Description |
| np.sin, np.cos, np.tan | Compute sine, cosine and tangent of angles |
| np.arcsin, np.arccos, np.arctan | Calculate inverse sine, cosine and tangent |
| np.sinh, np.cosh, np.tanh | Compute hyperbolic sine, cosine and tangent |
| np.deg2rad | Convert degrees to radians |
| np.rad2deg | Convert radians to degrees |
| np.hypot | Calculate hypotenuse of a right triangle |
Example: This example demonstrates sine, inverse sine, hyperbolic sine, and hypotenuse calculation using NumPy arrays.
Python
import numpy as np
angles = np.array([0, 30, 45, 60, 90])
rad = np.deg2rad(angles) # convert degrees to radians
# Sine of angles
sin_vals = np.sin(rad)
print("Sine values:", sin_vals)
# Inverse sine in degrees
inv_sin = np.rad2deg(np.arcsin(sin_vals))
print("Inverse sine (degrees):", inv_sin)
# Hyperbolic sine
sinh_vals = np.sinh(rad)
print("Hyperbolic sine:", sinh_vals)
# Hypotenuse of a right triangle
hyp = np.hypot(3, 4)
print("Hypotenuse:", hyp)
Output
Sine values: [0. 0.5 0.70710678 0.8660254 1. ]
Inverse sine (degrees): [ 0. 30. 45. 60. 90.]
Hyperbolic sine: [0. 0.54785347 0.86867096 1.24936705 2.3012989 ]
Hypotenuse: 5.0
Explanation:
- np.deg2rad() converts degrees to radians.
- np.sin() computes sine for each element.
- np.arcsin() calculates inverse sine; np.rad2deg() converts it back to degrees.
- np.sinh() applies the hyperbolic sine function element-wise.
- np.hypot(a, b) computes the hypotenuse of a right triangle with sides a and b.
2. Statistical functions
NumPy provides several statistical functions to calculate properties like mean, median, variance, and range. These functions operate element-wise and along specified axes, making analysis of arrays fast and efficient.
Common Statistical ufuncs in NumPy |
|---|
| Function | Description |
| np.amin, np.amax | Minimum or maximum of an array or along a specific axis |
| np.ptp | Range (max − min) of array values |
| np.percentile(a, p) | p-th percentile of array values |
| np.mean | Compute mean of data |
| np.median | Compute median of data |
| np.std | Compute standard deviation |
| np.var | Compute variance |
| np.average | Compute average value |
Example: This example demonstrates common statistical calculations on an array of student weights.
Python
import numpy as np
weights = np.array([50.7, 52.5, 50, 58, 55.63, 73.25, 49.5, 45])
# Min and Max
print("Min and Max:", np.amin(weights), np.amax(weights))
# Range
print("Range:", np.ptp(weights))
# 70th Percentile
print("70th Percentile:", np.percentile(weights, 70))
# Mean
print("Mean:", np.mean(weights))
# Median
print("Median:", np.median(weights))
# Standard Deviation
print("Std Dev:", np.std(weights))
# Variance
print("Variance:", np.var(weights))
# Average
print("Average:", np.average(weights))
Output
Min and Max: 45.0 73.25
Range: 28.25
70th Percentile: 55.317
Mean: 54.3225
Median: 51.6
Std Dev: 8.052773978574091
Variance: 64.84716875
Average: 54.3225
Explanation:
- np.amin() and np.amax() find the smallest and largest values.
- np.ptp() calculates the range (max − min).
- np.percentile(weights, 70) finds the value below which 70% of weights fall.
- np.mean() and np.average() compute the average of the array.
- np.median() returns the middle value.
- np.std() and np.var() measure dispersion via standard deviation and variance.
3. Bitwise Functions
NumPy provides bitwise functions to perform operations on the binary representation of integers. These allow element-wise manipulation of arrays at the bit level.
Common Bitwise ufuncs in NumPy |
|---|
| Function | Description |
| np.bitwise_and | Element-wise AND operation |
| np.bitwise_or | Element-wise OR operation |
| np.bitwise_xor | Element-wise XOR operation |
| np.invert | Bitwise NOT (invert) of elements |
| np.left_shift | Shift bits of elements to the left |
| np.right_shift | Shift bits of elements to the right |
Example: This example demonstrates basic bitwise operations on arrays of integers.
Python
import numpy as np
even = np.array([0, 2, 4, 6, 8, 16, 32])
odd = np.array([1, 3, 5, 7, 9, 17, 33])
# Bitwise AND, OR, XOR
print("AND:", np.bitwise_and(even, odd))
print("OR :", np.bitwise_or(even, odd))
print("XOR:", np.bitwise_xor(even, odd))
# Bitwise NOT
print("Invert:", np.invert(even))
# Bit shifts
print("Left shift :", np.left_shift(even, 1))
print("Right shift:", np.right_shift(even, 1))
OutputAND: [ 0 2 4 6 8 16 32]
OR : [ 1 3 5 7 9 17 33]
XOR: [1 1 1 1 1 1 1]
Invert: [ -1 -3 -5 -7 -9 -17 -33]
Left shift : [ 0 4 8 12 16 32 64]
Right shift: [ 0 1 2 3 4 8 16]
Explanation:
- np.bitwise_and(), np.bitwise_or(), np.bitwise_xor() perform element-wise logical operations at the bit level.
- np.invert() flips all bits of each element.
- np.left_shift() and np.right_shift() move the bits of each element left or right, effectively multiplying or dividing by powers of 2.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice