Concept Flow - np.argmin() and np.argmax()
Start with array
Find min or max value
Locate index of min or max
Return index
End
The functions find the smallest or largest value in an array and return its index.
import numpy as np arr = np.array([4, 2, 7, 1, 9]) min_index = np.argmin(arr) max_index = np.argmax(arr) print(min_index, max_index)
| Step | Action | Array State | Value Found | Index Returned |
|---|---|---|---|---|
| 1 | Start with array | [4, 2, 7, 1, 9] | - | - |
| 2 | Find minimum value | [4, 2, 7, 1, 9] | 1 | - |
| 3 | Find index of minimum | [4, 2, 7, 1, 9] | 1 | 3 |
| 4 | Find maximum value | [4, 2, 7, 1, 9] | 9 | - |
| 5 | Find index of maximum | [4, 2, 7, 1, 9] | 9 | 4 |
| 6 | Print results | - | - | 3 4 |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | After Step 5 | Final |
|---|---|---|---|---|---|---|
| arr | [4, 2, 7, 1, 9] | [4, 2, 7, 1, 9] | [4, 2, 7, 1, 9] | [4, 2, 7, 1, 9] | [4, 2, 7, 1, 9] | [4, 2, 7, 1, 9] |
| min_index | undefined | undefined | 3 | 3 | 3 | 3 |
| max_index | undefined | undefined | undefined | undefined | 4 | 4 |
np.argmin(array) returns the index of the smallest value. np.argmax(array) returns the index of the largest value. Both return the first occurrence if duplicates exist. Useful to find positions, not values. Input is a numpy array; output is an integer index.