Bird
0
0

Consider the code:

medium📝 Debug Q14 of 15
NumPy - Aggregation Functions
Consider the code:
import numpy as np
arr = np.array([[2, 5, 1], [7, 3, 9]])
index = np.argmax(arr, axis=1)

What is the correct output of index?
A[1 2]
B[0 2]
C[2 1]
D[2 2]
Step-by-Step Solution
Solution:
  1. Step 1: Understand np.argmax with axis=1

    axis=1 means find the index of the largest value in each row.
  2. Step 2: Find largest values per row and their indices

    Row 0: [2, 5, 1], max is 5 at index 1; Row 1: [7, 3, 9], max is 9 at index 2.
  3. Step 3: Check options carefully

    [2 2] shows [2 2], which is incorrect because first row max index is 1, not 2. [1 2] is [1 2], which matches the correct indices.
  4. Final Answer:

    [1 2] -> Option A
  5. Quick Check:

    np.argmax(arr, axis=1) = [1 2] [OK]
Quick Trick: axis=1 finds max index per row [OK]
Common Mistakes:
  • Mixing up axis parameter
  • Confusing row and column indices
  • Assuming global max index instead of per row

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes