Bird
0
0

The following code throws an error. What is the mistake?

medium📝 Debug Q14 of 15
NumPy - Aggregation Functions
The following code throws an error. What is the mistake?
import numpy as np
arr = np.array([1, 2, 3])
result = np.prod(arr, axis=1)
print(result)
AAxis 1 does not exist for 1D arrays
Bnp.prod() cannot be used on 1D arrays
CMissing parentheses in np.prod
DArray elements must be floats
Step-by-Step Solution
Solution:
  1. Step 1: Check array dimensions

    The array is 1D with shape (3,), so it has only axis 0.
  2. Step 2: Understand axis parameter

    Using axis=1 causes an error because axis 1 does not exist in 1D arrays.
  3. Final Answer:

    Axis 1 does not exist for 1D arrays -> Option A
  4. Quick Check:

    1D array has only axis 0 [OK]
Quick Trick: Check array shape before using axis parameter [OK]
Common Mistakes:
  • Using invalid axis for array dimension
  • Thinking np.prod() can't work on 1D arrays
  • Ignoring error messages about axis

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes