Universal Functions, or ufuncs, are NumPy functions that quickly apply a single function to every element in an array. Examples include np.sqrt(), np.round(), np.sin(), np.exp(), np.log(), and more.
import numpy as np
values = np.array([1, 4, 9, 16])
print("Square root:", np.sqrt(values))
print("Power 2:", np.power(values, 2))
print("Rounded average:", np.round(values.mean(), 2))np.sqrt(values) computes the square root of every element. np.power(values, 2) raises every element to the power of 2. values.mean() computes the average, and np.round(..., 2) rounds it to 2 decimal places.
Square root: [1. 2. 3. 4.] Power 2: [ 1 16 81 256] Rounded average: 7.5Info
Using ufuncs tends to give you shorter code and better performance than writing a loop. That difference becomes more noticeable as your data grows.