All sample questions / NumPy

NumPy Reductions and the axis Argument, what does print(b.argmax()) print after b = np.array([[1, 2], [3, 4], [5, 6]])?

Block 5, Reductions and the axis Argument, question 13 of the NumPy sample

import numpy as np
b = np.array([[1, 2], [3, 4], [5, 6]])
print(b.argmax())

What does this print?

  1. A. 5
  2. B. 6
  3. C. (2, 1)
  4. D. 1
Show the answer and why each option is right or wrong

Answer, A. 5

A. 5
Correct. Flattened in row major order the values are 1, 2, 3, 4, 5, 6, and the largest one, 6, sits at the last position, index 5.
B. 6
Confuses the index of the maximum with the maximum value itself.
C. (2, 1)
Assumes argmax returns the row and column coordinates as a tuple instead of a single flat index.
D. 1
Confuses the index of the maximum with the row it happens to sit in.

argmax without an axis always flattens the array first, so its shape does not matter, only how many elements it has and their order. The returned index is a position into that flattened sequence, not a pair of coordinates.

The answer above is what NumPy 2.4.4 printed when the snippet was run, not what someone expected it to do.

This question comes from a bank of 279 NumPy interview questions, all built the same way, sold as one PDF with the full answer key.

Get the 279 NumPy questions, 9 euros

Or SQL, Python, pandas and NumPy together, 1,636 questions, 19 euros

Or play the free NumPy sample, twenty questions in the browser, also as a free PDF.

Previous question Next question

Did this save you a lookup? Buy me a coffee on Ko-fi.