Numpy math functions


ceilceil value of the input elements
floorfloor value of the input elements
radiansConvert angels degree to radians
rad2degConvert radians to degree
roundRounding off to nearest decimal point

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.
Numpy eye() bincount() arange() linspace()
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



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-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer