Searching & Counting (argmax, argmin, nonzero, count_nonzero)
Locating Extrema: argmax(), argmin(), & nonzero()
Finding where extreme values occur—such as locating the maximum probability class in neural network classification, pinpointing the peak signal in a spectroscopy scan, or identifying active sensor triggers—is a standard task across numerical science.
NumPy provides fast vector routines for searching arrays:
np.argmax()/np.argmin(): Indices of global or directional extrema.np.unravel_index(): Converting flat 1D indices into multi-dimensional coordinates.np.nonzero(): Finding coordinates of non-zero elements.
1. Locating Extrema in 1D Arrays
argmax() and argmin() return the index of the first occurrence.2. Directional Extrema with the axis Parameter
In classification machine learning (such as MNIST digit recognition or image tagging), the model outputs a 2D matrix of shape (batch_size, num_classes) containing predicted probability scores. To extract the predicted class for each sample, compute argmax across class columns (axis=1):
3. Multi-Dimensional Coordinates with np.unravel_index()
When argmax() is called on a multi-dimensional array without specifying an axis, it flattens the array conceptually and returns a single integer representing the flat index.
To convert that flat integer into true $(row, col)$ or $(depth, row, col)$ coordinates, use np.unravel_index():
4. Finding Non-Zero Elements with np.nonzero()
np.nonzero() returns a tuple of coordinate arrays, one for each dimension, containing the indices where elements are non-zero:
Multiple Choice Questions
1. In the case of multiple identical maximum values in an array, which index does np.argmax() return?
A. The last occurrence B. The first occurrence C. A list of all occurrences D. Randomly selected index Answer: B Explanation: np.argmax() scans elements in index order and returns the index corresponding to the first occurrence of the maximum value.
2. In a neural network probability matrix probs of shape (100, 10) (100 samples, 10 classes), which command extracts the predicted class for each sample?
A. np.argmax(probs, axis=0) B. np.argmax(probs, axis=1) C. np.max(probs) D. np.argsort(probs) Answer: B Explanation: axis=1 collapses the 10 class columns for each row, returning a 1D array of 100 indices indicating which class had the highest probability for each sample.
3. Which NumPy function maps a flat 1D argmax index back to a tuple of multi-dimensional matrix coordinates?
A. np.reshape_index() B. np.unravel_index() C. np.ravel_multi_index() D. np.expand_dims() Answer: B Explanation: np.unravel_index(indices, shape) converts a flat index or array of flat indices into a tuple of coordinate arrays for an array of the given shape.
4. What does np.count_nonzero(arr) do?
A. Counts how many elements in arr are equal to zero B. Counts how many elements in arr are not equal to zero C. Returns the sum of all elements D. Replaces zero values with 1 Answer: B Explanation: np.count_nonzero() efficiently tallies the total number of non-zero (or True) values across an array or along a specified axis.
5. If arr = np.array([[0, 8], [3, 0]]), what is returned by np.nonzero(arr)?
A. (array([0, 1]), array([1, 0])) B. array([8, 3]) C. array([0, 1]) D. [8, 3] Answer: A Explanation: np.nonzero() returns a tuple of arrays representing coordinates for each axis. The non-zero elements are at (0, 1) and (1, 0).
Set Operations & Distinct Elements (np.unique, intersect1d, isin)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.