NumPy Mathematics, Aggregations & Logic

NumPy shifts numerical computation away from slow Python interpreter loops into lightning-fast, C-compiled vector operations. Below is the complete reference index for mathematical transformations, universal functions (ufuncs), broadcasting alignment, multi-axis statistical aggregations, and conditional logic.

1. Mathematical & Trigonometric Functions

Element-wise rounding, boundary scaling, and degree-to-radian conversion utilities.

  • ceil() — Calculate the ceiling value of input elements.
  • floor() — Calculate the floor value of input elements.
  • round() — Round floating-point elements to the nearest decimal point.
  • radians() — Convert angles from degrees to radians.
  • rad2deg() — Convert angles from radians to degrees.

2. Universal Functions (Ufuncs) & Broadcasting

Explore vectorized arithmetic mechanics and dimension alignment rules.

  • ufuncs Introduction — Learn about universal functions operating element-by-element.
  • adding() — Perform element-wise array addition and core arithmetic.
  • broadcasting() — Operate on arrays of different shapes seamlessly.

3. Statistical Aggregations & Axis Reduction

Compute metrics across full arrays or target specific dimensions using axis controls.

  • sum() — Calculate the total value aggregation across arrays.
  • mean() — Compute the arithmetic average.
  • min() — Identify the minimum element.
  • max() — Identify the maximum element.
  • std() — Compute standard deviation.
  • axis-reduction — Control row and column statistical reductions.

4. Conditional Logic, Masking & Practice

Filter elements conditionally and test skills with practical problem sets.

  • where() — Perform conditional element selection and substitution.
  • boolean-masks — Execute fast array indexing via boolean filters.
  • exercise-a — Practice math and aggregation exercises.

Example: Handling NaN and Infinite Values

import numpy as np

arr = np.array([1.0, np.nan, np.inf, -np.inf, 4.0])

print("Original:", arr)
print("isfinite:", np.isfinite(arr))
print("nanmean (ignores nan):", np.nanmean(arr))
print("clip finite values between 0 and 10:", np.clip(arr, 0, 10))
  • We create an array with NaN and infinite values.
  • We use np.isfinite to check finite elements.
  • We compute mean ignoring NaNs with np.nanmean.
  • We use np.clip to bound values, useful in many data processing tasks.

Example: Complex Number Math

import numpy as np

z = np.array([1 + 2j, 3 - 4j, -2 + 0.5j])
print("Real parts:", np.real(z))
print("Imag parts:", np.imag(z))
print("Angles (in radians):", np.angle(z))
print("Magnitude:", np.abs(z))
  • This demonstrates operations on complex arrays.
  • Useful for users dealing with Fourier transforms, phasors, or physics applications.

Example: Vectorized vs Loop Performance

import numpy as np
import time

N = 1000000
arr = np.arange(N, dtype=np.float64)

start = time.time()
res_np = np.sqrt(arr)
print("NumPy sqrt time:", time.time() - start)

start2 = time.time()
res_loop = [x**0.5 for x in arr]
print("Python loop sqrt time:", time.time() - start2)
  • Shows how much faster NumPy vectorized functions are compared to Python loops for large arrays.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer