0
0
NumPydata~10 mins

np.argmin() and np.argmax() in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
NumPy
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)
This code finds the index of the smallest and largest values in the array.
Execution Table
StepActionArray StateValue FoundIndex Returned
1Start with array[4, 2, 7, 1, 9]--
2Find minimum value[4, 2, 7, 1, 9]1-
3Find index of minimum[4, 2, 7, 1, 9]13
4Find maximum value[4, 2, 7, 1, 9]9-
5Find index of maximum[4, 2, 7, 1, 9]94
6Print results--3 4
💡 All values processed; indices of min and max returned.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
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_indexundefinedundefined3333
max_indexundefinedundefinedundefinedundefined44
Key Moments - 2 Insights
Why does np.argmin() return 3 instead of the minimum value 1?
np.argmin() returns the index where the minimum value is found, not the value itself. See execution_table step 3 where index 3 corresponds to value 1.
Can np.argmax() return multiple indices if the max value appears more than once?
No, np.argmax() returns the first index of the maximum value found. This is shown in execution_table step 5 where index 4 is returned for max value 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the index returned by np.argmin()?
A3
B1
C4
D0
💡 Hint
Check the 'Index Returned' column at step 3 in the execution_table.
At which step does the code find the maximum value in the array?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table where 'Find maximum value' is listed.
If the array was [4, 9, 7, 1, 9], what would np.argmax() return?
A0
B1
C4
D3
💡 Hint
np.argmax() returns the first index of the max value; check variable_tracker for max_index behavior.
Concept Snapshot
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.
Full Transcript
This lesson shows how np.argmin() and np.argmax() work by finding the index of the smallest and largest values in a numpy array. We start with an array, find the min and max values, then locate their positions. The functions return the index, not the value itself. If there are duplicates, the first index is returned. The example array is [4, 2, 7, 1, 9]. np.argmin() returns 3 because the smallest value 1 is at index 3. np.argmax() returns 4 because the largest value 9 is at index 4. This helps us quickly find where important values are in data.