0
0
NumPydata~10 mins

np.prod() for product in NumPy - Interactive Code Practice

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

Complete the code to calculate the product of all elements in the array.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
result = np.[1](arr)
print(result)
Drag options to blanks, or click blank then click option'
Aprod
Bsum
Cmean
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.sum() instead of np.prod()
Using np.mean() which calculates average
Using np.max() which finds the largest element
2fill in blank
medium

Complete the code to calculate the product of elements along axis 0.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
result = np.prod(arr, axis=[1])
print(result)
Drag options to blanks, or click blank then click option'
A2
B1
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=1 which calculates product across columns
Using axis=2 which is out of bounds for this array
Using negative axis incorrectly
3fill in blank
hard

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

NumPy
import numpy as np
arr = [1, 2, 3, 4]
result = np.prod([1])
print(result)
Drag options to blanks, or click blank then click option'
Anp.array(arr)
Barr
Clist(arr)
Darr.tolist()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a plain list without conversion
Using list() on an already list object
Using tolist() which converts numpy array to list
4fill in blank
hard

Complete the code to reshape the array and compute the product along axis 1.

NumPy
import numpy as np
arr = np.array([1,2,3,4,5,6])
reshaped = arr.[1]((2, 3))
result = np.[2](reshaped, axis=1)
print(result)
Drag options to blanks, or click blank then click option'
Areshape
Bprod
Csum
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.sum() instead of np.prod()
Forgetting to call reshape()
Using axis=0 instead of axis=1
5fill in blank
hard

Fill all three blanks to compute the geometric mean of the array.

NumPy
import numpy as np
arr = np.array([2, 4, 8])
geo_mean = (np.[1](arr)) ** ([2] / [3])
print(geo_mean)
Drag options to blanks, or click blank then click option'
Aprod
B1
Clen(arr)
Dsum(arr)
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.sum(arr) instead of product
Computing arithmetic mean with np.mean()
Using wrong exponent like / np.sum(arr)