Recall & Review
beginner
What is a universal function (ufunc) in NumPy?
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?
✗ Incorrect
Ufuncs operate element-wise on arrays and are implemented in C, making them very fast.
Which of these is NOT a NumPy ufunc?
✗ 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?
✗ 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?
✗ Incorrect
np.add(array1, array2) adds elements from both arrays element-wise.
Which statement about ufuncs is true?
✗ 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.