A universal function (ufunc) is a function that operates element-wise on arrays. It is fast and vectorized, allowing operations on entire arrays without explicit loops.
Click to reveal answer
beginner
How do ufuncs improve performance compared to Python loops?
Ufuncs are implemented in C and operate directly on arrays, avoiding Python loops. This makes them much faster and more efficient for large data.
Click to reveal answer
beginner
Name three common NumPy ufuncs.
Examples include np.add (addition), np.multiply (multiplication), and np.sqrt (square root). These apply the operation element-wise on arrays.
Click to reveal answer
intermediate
What does it mean that ufuncs support broadcasting?
Broadcasting means ufuncs can operate on arrays of different shapes by automatically expanding smaller arrays to match larger ones, without copying data.
Click to reveal answer
beginner
How can you apply a ufunc to multiple arrays at once?
You can pass multiple arrays as arguments to a ufunc, like np.add(array1, array2), which adds elements from both arrays element-wise.
Click to reveal answer
What is the main advantage of using a NumPy ufunc?
AThey only work on single numbers
BThey operate element-wise and are very fast
CThey slow down calculations
DThey require explicit Python loops
✗ Incorrect
Ufuncs operate element-wise on arrays and are implemented in C, making them very fast.
Which of these is NOT a NumPy ufunc?
Anp.loop
Bnp.add
Cnp.sqrt
Dnp.multiply
✗ Incorrect
np.loop is not a NumPy ufunc; np.add, np.sqrt, and np.multiply are common ufuncs.
What does broadcasting allow ufuncs to do?
AOperate on arrays of different shapes
BOnly work on arrays of the same shape
CConvert arrays to lists
DChange array data types automatically
✗ Incorrect
Broadcasting lets ufuncs operate on arrays with different shapes by expanding smaller arrays.
How do you apply a ufunc to add two arrays element-wise?
Anp.add(array1 * array2)
Bnp.add(array1)
Cnp.add(array1, array2)
Darray1 + array2 + np.add
✗ Incorrect
np.add(array1, array2) adds elements from both arrays element-wise.
Which statement about ufuncs is true?
AThey require explicit Python loops
BThey are slower than list comprehensions
CThey only work on scalar values
DThey operate element-wise on arrays
✗ Incorrect
Ufuncs operate element-wise on arrays, making operations fast and efficient.
Explain what a universal function (ufunc) is and why it is useful in NumPy.
Think about how ufuncs work on each item in an array without loops.
You got /4 concepts.
Describe how broadcasting works with ufuncs and give an example.
Imagine adding a single number to every element of an array.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of NumPy universal functions (ufuncs)?
easy
A. To convert arrays into lists
B. To perform element-wise operations on arrays efficiently
C. To sort arrays in ascending order
D. To create new arrays from scratch
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 B
Quick Check:
Ufuncs = element-wise fast operations [OK]
Hint: Remember: ufuncs work element-wise on arrays fast [OK]
Common Mistakes:
Confusing ufuncs with array creation functions
Thinking ufuncs sort arrays
Assuming ufuncs convert arrays to lists
2. Which of the following is the correct syntax to use the NumPy universal function for square root on array arr?
easy
A. np.sqrt(arr)
B. np.square(arr)
C. np.root(arr)
D. np.sqrt_of(arr)
Solution
Step 1: Identify the correct ufunc for square root
The NumPy function for square root is np.sqrt().
Step 2: Check the syntax with the array
Calling np.sqrt(arr) applies square root element-wise on arr.
Final Answer:
np.sqrt(arr) -> Option A
Quick Check:
Square root ufunc = np.sqrt() [OK]
Hint: Square root ufunc is always np.sqrt(array) [OK]
Common Mistakes:
Using np.square instead of np.sqrt
Trying non-existent functions like np.root
Adding extra words in function name
3. What is the output of the following code?
import numpy as np
arr = np.array([1, 4, 9, 16])
result = np.sqrt(arr)
print(result)
medium
A. [1. 2. 3. 4.]
B. [1 2 3 4]
C. [1. 4. 9. 16.]
D. [0. 2. 3. 4.]
Solution
Step 1: Understand np.sqrt on array elements
Applying np.sqrt on 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.].
4. The following code throws an error. What is the problem?
import numpy as np
arr = np.array([1, 2, 3])
result = np.add(arr, 5, 10)
print(result)
medium
A. np.add cannot add a scalar to an array
B. The array must be of float type for np.add
C. np.add takes only two arguments, but three were given
D. The print statement is incorrect
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 C
Quick Check:
np.add = 2 args only [OK]
Hint: np.add needs exactly two inputs, no extras [OK]
Common Mistakes:
Passing more than two arguments to np.add
Thinking np.add requires float arrays
Blaming print instead of function call
5. You have two arrays:
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?
hard
A. np.multiply(a, b) + np.sqrt(a)
B. np.multiply(np.sqrt(a), np.sqrt(b))
C. np.sqrt(a) * np.sqrt(b)
D. np.sqrt(np.multiply(a, b))
Solution
Step 1: Understand element-wise product and sqrt
First multiply elements of a and b, then apply square root on the result.
Step 2: Check each option
np.sqrt(np.multiply(a, b)) does np.multiply(a, b) then np.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 D
Quick Check:
Multiply then sqrt = np.sqrt(np.multiply(a,b)) [OK]
Hint: Multiply first, then sqrt for correct order [OK]
Common Mistakes:
Applying sqrt before multiplication
Adding instead of multiplying
Mixing order of operations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.