Aggregation functions & Universal functions

NumPy - The Basics

1 min read

Published Sep 22 2025


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 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




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 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]

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact