Universal functions, or ufuncs, let you do math on whole arrays quickly and easily. They work element by element, like doing math on each number in a list all at once.
Universal functions (ufuncs) in NumPy
numpy.ufunc_name(array1, array2, ...)
# Example:
np.add(array1, array2)Replace ufunc_name with the math operation you want, like add, subtract, multiply, sqrt, etc.
You can use one or more arrays as inputs, depending on the function.
import numpy as np arr = np.array([1, 2, 3]) result = np.add(arr, 5) print(result)
import numpy as np arr = np.array([4, 9, 16]) result = np.sqrt(arr) print(result)
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) result = np.multiply(arr1, arr2) print(result)
This program adds two arrays element by element, then finds the square root of each sum. It shows how ufuncs work on arrays without loops.
import numpy as np # Create two arrays array1 = np.array([10, 20, 30]) array2 = np.array([1, 2, 3]) # Add arrays element-wise added = np.add(array1, array2) # Calculate square root of the result sqrt_result = np.sqrt(added) print("Added:", added) print("Square root of added:", sqrt_result)
Ufuncs are very fast because they run compiled code internally.
They support broadcasting, so arrays of different shapes can work together if compatible.
Many common math functions in numpy are ufuncs, making array math easy.
Ufuncs apply math operations element-wise on arrays quickly.
They help avoid slow loops and make code simpler and faster.
Common ufuncs include add, subtract, multiply, divide, sqrt, sin, and more.
Practice
ufuncs)?Solution
Step 1: Understand what ufuncs do
Ufuncs apply mathematical operations element-wise on arrays, making calculations fast and simple.Step 2: Compare with other options
Creating arrays, sorting, or converting to lists are not the main roles of ufuncs.Final Answer:
To perform element-wise operations on arrays efficiently -> Option BQuick Check:
Ufuncs = element-wise fast operations [OK]
- Confusing ufuncs with array creation functions
- Thinking ufuncs sort arrays
- Assuming ufuncs convert arrays to lists
arr?Solution
Step 1: Identify the correct ufunc for square root
The NumPy function for square root isnp.sqrt().Step 2: Check the syntax with the array
Callingnp.sqrt(arr)applies square root element-wise onarr.Final Answer:
np.sqrt(arr) -> Option AQuick Check:
Square root ufunc = np.sqrt() [OK]
- Using np.square instead of np.sqrt
- Trying non-existent functions like np.root
- Adding extra words in function name
import numpy as np arr = np.array([1, 4, 9, 16]) result = np.sqrt(arr) print(result)
Solution
Step 1: Understand np.sqrt on array elements
Applyingnp.sqrton each element: sqrt(1)=1.0, sqrt(4)=2.0, sqrt(9)=3.0, sqrt(16)=4.0.Step 2: Check output format
NumPy returns floats for sqrt, so output is[1. 2. 3. 4.].Final Answer:
[1. 2. 3. 4.] -> Option AQuick Check:
np.sqrt([1,4,9,16]) = [1. 2. 3. 4.] [OK]
- Expecting integer output instead of float
- Confusing np.sqrt with np.square
- Misreading array output format
import numpy as np arr = np.array([1, 2, 3]) result = np.add(arr, 5, 10) print(result)
Solution
Step 1: Check np.add function signature
np.add takes exactly two arguments: two arrays or an array and a scalar.Step 2: Identify the error in arguments
Here, three arguments are passed:arr, 5, and 10, which is invalid.Final Answer:
np.add takes only two arguments, but three were given -> Option CQuick Check:
np.add = 2 args only [OK]
- Passing more than two arguments to np.add
- Thinking np.add requires float arrays
- Blaming print instead of function call
a = np.array([1, 2, 3]) b = np.array([4, 5, 6])
Which code correctly computes the element-wise product and then applies the square root to each product?
Solution
Step 1: Understand element-wise product and sqrt
First multiply elements ofaandb, then apply square root on the result.Step 2: Check each option
np.sqrt(np.multiply(a, b)) doesnp.multiply(a, b)thennp.sqrt(), which is correct. Options B and C apply sqrt before multiply, which is different. np.multiply(a, b) + np.sqrt(a) adds sqrt(a), which is incorrect.Final Answer:
np.sqrt(np.multiply(a, b)) -> Option DQuick Check:
Multiply then sqrt = np.sqrt(np.multiply(a,b)) [OK]
- Applying sqrt before multiplication
- Adding instead of multiplying
- Mixing order of operations
Ready to prove it?
Pass the test and this pattern moves unknown
