0
0
NumPydata~10 mins

Understanding ufunc methods (reduce, accumulate) in NumPy - Interactive Quiz & Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the sum of all elements in the array using a ufunc method.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
result = np.add.[1](arr)
print(result)
Drag options to blanks, or click blank then click option'
Areduce
Baccumulate
Couter
Dreduceat
Attempts:
3 left
💡 Hint
Common Mistakes
Using accumulate instead of reduce returns intermediate sums, not the total sum.
Using outer or reduceat methods which have different purposes.
2fill in blank
medium

Complete the code to get the cumulative sum of elements in the array using a ufunc method.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
cum_sum = np.add.[1](arr)
print(cum_sum)
Drag options to blanks, or click blank then click option'
Aouter
Breduceat
Caccumulate
Dreduce
Attempts:
3 left
💡 Hint
Common Mistakes
Using reduce returns only the final sum, not intermediate sums.
Using outer or reduceat which do not produce cumulative sums.
3fill in blank
hard

Fix the error in the code to correctly compute the cumulative product of the array elements.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
cum_prod = np.multiply.[1](arr)
print(cum_prod)
Drag options to blanks, or click blank then click option'
Aaccumulate
Breduce
Creduceat
Douter
Attempts:
3 left
💡 Hint
Common Mistakes
Using reduce returns only the final product, not intermediate products.
Using reduceat or outer which are not for cumulative operations.
4fill in blank
hard

Fill both blanks to create a dictionary of cumulative sums for each number greater than 2 in the list.

NumPy
import numpy as np
numbers = [1, 3, 5, 2, 4]
cum_sums = {n: np.add.[1](np.arange(1, n+1)) for n in numbers if n [2] 2}
print(cum_sums)
Drag options to blanks, or click blank then click option'
Aaccumulate
B>
C>=
Dreduce
Attempts:
3 left
💡 Hint
Common Mistakes
Using reduce instead of accumulate returns only the total sum, not cumulative sums.
Using '>=' includes numbers equal to 2, which is not required.
5fill in blank
hard

Fill all three blanks to create a dictionary of final products for numbers less than or equal to 4 using a ufunc method.

NumPy
import numpy as np
numbers = [1, 3, 5, 2, 4]
final_products = {n: np.multiply.[1](np.arange(1, n+1)) for n in numbers if n [2] 4 and n [3] 0}
print(final_products)
Drag options to blanks, or click blank then click option'
Aaccumulate
B<=
C!=
Dreduce
Attempts:
3 left
💡 Hint
Common Mistakes
Using accumulate returns intermediate products, not the final one.
Using '<' instead of '<=' excludes 4, which should be included.
Using '==' instead of '!=' includes zero, which should be excluded.