Aggregation functions & Universal functions
NumPy - The Basics
1 min read
This section is 1 min read, full guide is 14 min read
Published Sep 22 2025
11
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
NumPyPython
Aggregation Functions
- These collapse an array into a single value or along a specific axis.
- Examples:
np.sum,np.prod,np.mean,np.std,np.min,np.max,np.argmin,np.argmax,np.all,np.any.
Examples:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print("Sum:", np.sum(arr))
# 15
print("Product:", np.prod(arr))
# 120
print("Mean:", np.mean(arr))
# 3.0
print("Standard Deviation:", np.std(arr))
# 1.414...
print("Min:", np.min(arr))
# 1
print("Max:", np.max(arr))
# 5
print("Index of Min:", np.argmin(arr))
# 0
print("Index of Max:", np.argmax(arr))
# 4
print("All > 0:", np.all(arr > 0))
# True
print("Any > 4:", np.any(arr > 4))
# True
Copy to Clipboard
Toggle show comments
Universal Functions
- Ufuncs are vectorised functions in NumPy that operate element-wise on arrays.
- They are implemented in C → very fast compared to Python loops.
- They can take:
- Scalars (e.g.,
np.sqrt(4)) - Arrays (apply element-wise)
- Broadcasted inputs (work across compatible shapes).
- Scalars (e.g.,
- Many ufuncs also accept an optional
out=argument for in-place results.
Categories of Ufuncs:
- Arithmetic ufuncs →
np.add,np.subtract,np.multiply,np.divide,np.power,np.mod. - Trigonometric functions →
np.sin,np.cos,np.tan,np.arcsin, etc. - Exponential & logarithmic →
np.exp,np.log,np.log10,np.expm1. - Rounding functions →
np.round,np.floor,np.ceil,np.trunc. - Other math functions →
np.sqrt,np.abs,np.sign,np.maximum,np.minimum.
Examples:
import numpy as np
arr = np.array([1, -2, 3, -4, 5])
# Arithmetic
print("Add 10:", np.add(arr, 10))
# [11 8 13 6 15]
print("Multiply by 2:", np.multiply(arr, 2))
# [ 2 -4 6 -8 10]
print("Power of 2:", np.power(arr, 2))
# [ 1 4 9 16 25]
print("Modulo 3:", np.mod(arr, 3))
# [1 1 0 2 2]
# Trigonometric
print("Sine:", np.sin(arr))
# [ 0.841 -0.909 0.141 0.757 -0.959]
print("Cosine:", np.cos(arr))
# [ 0.540 -0.416 -0.990 -0.654 0.284]
print("Arctan:", np.arctan(arr))
# [ 0.785 -1.107 1.249 -1.326 1.373]
# Exponential & Logarithm
arr_pos = np.array([1, 2, 3, 10])
print("Exp:", np.exp(arr_pos))
# [2.718 7.389 20.086 22026.465]
print("Log (natural):", np.log(arr_pos))
# [0. 0.693 1.099 2.303]
print("Log base 10:", np.log10(arr_pos))
# [0. 0.301 0.477 1. ]
# Rounding
arr_float = np.array([1.2, -1.7, 2.5, -2.9])
print("Round:", np.round(arr_float))
# [ 1. -2. 2. -3.]
print("Floor:", np.floor(arr_float))
# [ 1. -2. 2. -3.]
print("Ceil:", np.ceil(arr_float))
# [ 2. -1. 3. -2.]
print("Truncate:", np.trunc(arr_float))
# [ 1. -1. 2. -2.]
# Other Math
print("Square Root:", np.sqrt([1,4,9,16]))
# [1. 2. 3. 4.]
print("Absolute:", np.abs(arr))
# [1 2 3 4 5]
print("Sign:", np.sign(arr))
# [ 1 -1 1 -1 1]
print("Maximum (arr vs 0):", np.maximum(arr, 0))
# [1 0 3 0 5]
print("Minimum (arr vs 0):", np.minimum(arr, 0))
# [ 0 -2 0 -4 0]
Copy to Clipboard
Toggle show comments














