Aggregation functions & Universal functions

NumPy - The Basics

1 min read

Published Sep 22 2025, updated Aug 17 2026


11
0
0
0

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 nparr = np.array([1, 2, 3, 4, 5])print("Sum:", np.sum(arr))           # 15print("Product:", np.prod(arr))      # 120print("Mean:", np.mean(arr))         # 3.0print("Standard Deviation:", np.std(arr))  # 1.414...print("Min:", np.min(arr))           # 1print("Max:", np.max(arr))           # 5print("Index of Min:", np.argmin(arr)) # 0print("Index of Max:", np.argmax(arr)) # 4print("All > 0:", np.all(arr > 0))   # Trueprint("Any > 4:", np.any(arr > 4))   # True




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).
  • Many ufuncs also accept an optional out= argument for in-place results.

Categories of Ufuncs:

  1. Arithmetic ufuncsnp.add, np.subtract, np.multiply, np.divide, np.power, np.mod.
  2. Trigonometric functionsnp.sin, np.cos, np.tan, np.arcsin, etc.
  3. Exponential & logarithmicnp.exp, np.log, np.log10, np.expm1.
  4. Rounding functionsnp.round, np.floor, np.ceil, np.trunc.
  5. Other math functionsnp.sqrt, np.abs, np.sign, np.maximum, np.minimum.


Examples:

import numpy as nparr = np.array([1, -2, 3, -4, 5])# Arithmeticprint("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]# Trigonometricprint("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 & Logarithmarr_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.    ]# Roundingarr_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 Mathprint("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]
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact
NumPy - The Basics | Aggregation functions & Universal functions | SimpleSteps.guide