ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)
Set your Publisher ID in src/config/ads.ts to display live ads.
NumPy Tutorial• 6 min read
Vectorized Universal Functions (ufuncs)
Element-wise arithmetic, trigonometry, exponentials, and logarithms
Universal functions (ufuncs) are compiled C functions that execute element-wise operations on ndarrays with zero Python overhead.
Fast Mathematical Transformations
Math ufuncs: np.add, np.subtract, np.multiply, np.divide, np.power, np.sqrt.
Exponential and Logarithmic: np.exp, np.log, np.log2, np.log10.
Trigonometric: np.sin, np.cos, np.tan, np.arctan2.
Comparison: np.greater, np.less, np.equal, np.isclose (safe floating point equality).
Vectorized Sigmoid and Math Pipeline
import numpy as np
# Vectorized raw logit scores
logits = np.array([-2.5, 0.0, 1.2, 4.5])
# Vectorized sigmoid function: 1 / (1 + exp(-x))
probabilities = 1.0 / (1.0 + np.exp(-logits))
print("Raw logits: ", logits)
print("Probabilities: ", np.round(probabilities, 4))np.exp executes across all 4 elements simultaneously in compiled C.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)
Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Which NumPy function should you use to test if two floating-point arrays are equal within a numerical tolerance?