0
0
NumPydata~5 mins

Universal functions (ufuncs) in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThey only work on single numbers
BThey operate element-wise and are very fast
CThey slow down calculations
DThey require explicit Python loops
Which of these is NOT a NumPy ufunc?
Anp.loop
Bnp.add
Cnp.sqrt
Dnp.multiply
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
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
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
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.