These functions help find the position of the smallest or largest value in data. This is useful to quickly locate important points in your data.
np.argmin() and np.argmax() in NumPy
np.argmin(array, axis=None) np.argmax(array, axis=None)
array is the data you want to check.
axis lets you choose if you want the position in the whole array or along rows/columns.
np.argmin([3, 1, 2])
np.argmax([3, 1, 2])
np.argmin([[5, 2, 3], [1, 4, 6]], axis=0)
np.argmax([[5, 2, 3], [1, 4, 6]], axis=1)
This program uses np.argmin() and np.argmax() to find positions of lowest and highest scores in a 2D array of student test scores. It shows how to find these positions overall and along rows or columns.
import numpy as np # Create a 2D array representing scores of 3 students in 4 tests scores = np.array([[88, 92, 79, 93], [85, 90, 91, 89], [90, 85, 88, 92]]) # Find the index of the lowest score overall lowest_score_index = np.argmin(scores) # Find the index of the highest score overall highest_score_index = np.argmax(scores) # Find the index of the lowest score in each test (column-wise) lowest_per_test = np.argmin(scores, axis=0) # Find the index of the highest score for each student (row-wise) highest_per_student = np.argmax(scores, axis=1) print(f"Lowest score index overall: {lowest_score_index}") print(f"Highest score index overall: {highest_score_index}") print(f"Lowest score index per test (column-wise): {lowest_per_test}") print(f"Highest score index per student (row-wise): {highest_per_student}")
Indexes start at 0, so the first element is at position 0.
If you do not specify axis, the function treats the array as flat (1D).
These functions return the first occurrence if there are multiple equal min or max values.
np.argmin() finds the position of the smallest value.
np.argmax() finds the position of the largest value.
You can find positions overall or along rows/columns using the axis parameter.
Practice
np.argmin() do in NumPy?Solution
Step 1: Understand the purpose of np.argmin()
The function np.argmin() returns the index (position) of the smallest value in a NumPy array.Step 2: Differentiate from similar functions
Unlike np.min() which returns the smallest value itself, np.argmin() returns where that value is located.Final Answer:
Finds the index of the smallest value in an array -> Option AQuick Check:
np.argmin() = index of smallest value [OK]
- Confusing np.argmin() with np.min()
- Thinking it returns the smallest value, not the index
- Mixing up np.argmin() with np.argmax()
arr?Solution
Step 1: Recall the function to find largest value index
np.argmax(arr) returns the index of the largest value in the array arr.Step 2: Check other options
arr.argmax() is valid but not the function syntax asked; np.argmin() and arr.argmin() find smallest value index, not largest.Final Answer:
np.argmax(arr) -> Option CQuick Check:
np.argmax(arr) = index of max value [OK]
- Using np.argmin() instead of np.argmax()
- Confusing method call arr.argmax() with function np.argmax()
- Mixing up largest and smallest value functions
arr = np.array([3, 1, 4, 1, 5]), what is the output of np.argmin(arr)?Solution
Step 1: Identify the smallest value in the array
The array is [3, 1, 4, 1, 5]. The smallest value is 1.Step 2: Find the index of the first occurrence of the smallest value
The first 1 appears at index 1 (0-based indexing).Final Answer:
1 -> Option BQuick Check:
np.argmin([3,1,4,1,5]) = 1 [OK]
- Choosing the last occurrence index instead of first
- Confusing values with indices
- Using np.argmax() by mistake
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?Solution
Step 1: Understand np.argmax with axis=1
axis=1 means find the index of the largest value in each row.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.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.Final Answer:
[1 2] -> Option AQuick Check:
np.argmax(arr, axis=1) = [1 2] [OK]
- Mixing up axis parameter
- Confusing row and column indices
- Assuming global max index instead of per row
scores = np.array([[88, 92, 79], [95, 85, 91], [70, 78, 88], [90, 91, 85]])
How do you find the subject index where each student scored the lowest?
Solution
Step 1: Understand the array and axis meaning
Each row is a student, columns are subjects. axis=1 means operate across columns (subjects) per student.Step 2: Use np.argmin with axis=1 to find lowest score subject per student
np.argmin(scores, axis=1) returns the index of the smallest score in each student's row.Final Answer:
np.argmin(scores, axis=1) -> Option DQuick Check:
Lowest subject per student = np.argmin(scores, axis=1) [OK]
- Using axis=0 which finds min per subject, not per student
- Using np.argmax instead of np.argmin
- Confusing rows and columns in axis parameter
Ready to prove it?
Pass the test and this pattern moves unknown
