Finding Minimum and Maximum Indices with np.argmin() and np.argmax()
📖 Scenario: Imagine you have a list of daily temperatures for a week. You want to find which day was the coldest and which day was the hottest.
🎯 Goal: You will create a NumPy array of temperatures, set a threshold temperature, find the indices of the coldest and hottest days using np.argmin() and np.argmax(), and then print those indices.
📋 What You'll Learn
Create a NumPy array called temperatures with exact values: [22, 19, 25, 21, 20, 23, 18]
Create a variable called threshold and set it to 20
Use np.argmin(temperatures) to find the index of the coldest day and store it in coldest_day
Use np.argmax(temperatures) to find the index of the hottest day and store it in hottest_day
Print the values of coldest_day and hottest_day
💡 Why This Matters
🌍 Real World
Finding minimum and maximum values and their positions is common in weather data analysis, stock prices, and many other fields.
💼 Career
Data scientists often need to quickly identify key points in data sets, such as the lowest or highest values, to make decisions or create reports.
Progress0 / 4 steps
1
Create the temperatures array
Import NumPy as np and create a NumPy array called temperatures with these exact values: [22, 19, 25, 21, 20, 23, 18]
NumPy
Hint
Use import numpy as np to import NumPy. Then use np.array([...]) to create the array.
2
Set the threshold temperature
Create a variable called threshold and set it to the integer 20
NumPy
Hint
Just assign the number 20 to a variable named threshold.
3
Find indices of coldest and hottest days
Use np.argmin(temperatures) to find the index of the coldest day and store it in coldest_day. Use np.argmax(temperatures) to find the index of the hottest day and store it in hottest_day
NumPy
Hint
Use np.argmin() and np.argmax() functions on the temperatures array.
4
Print the results
Print the values of coldest_day and hottest_day on separate lines
NumPy
Hint
Use two print() statements, one for each variable.
Practice
(1/5)
1. What does the function np.argmin() do in NumPy?
easy
A. Finds the index of the smallest value in an array
B. Finds the index of the largest value in an array
C. Returns the smallest value in an array
D. Returns the largest value in an array
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 A
Quick Check:
np.argmin() = index of smallest value [OK]
Hint: Remember: argmin = position of minimum value [OK]
Common Mistakes:
Confusing np.argmin() with np.min()
Thinking it returns the smallest value, not the index
Mixing up np.argmin() with np.argmax()
2. Which of the following is the correct NumPy function syntax to find the index of the largest value in a NumPy array arr?
easy
A. np.argmin(arr)
B. arr.argmax()
C. np.argmax(arr)
D. arr.argmin()
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 C
Quick Check:
np.argmax(arr) = index of max value [OK]
Hint: Use np.argmax() to get largest value index [OK]
Common Mistakes:
Using np.argmin() instead of np.argmax()
Confusing method call arr.argmax() with function np.argmax()
Mixing up largest and smallest value functions
3. Given the array arr = np.array([3, 1, 4, 1, 5]), what is the output of np.argmin(arr)?
medium
A. 3
B. 1
C. 0
D. 4
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 B
Quick Check:
np.argmin([3,1,4,1,5]) = 1 [OK]
Hint: np.argmin() returns first smallest value index [OK]
Common Mistakes:
Choosing the last occurrence index instead of first
Confusing values with indices
Using np.argmax() by mistake
4. Consider the code:
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?
medium
A. [1 2]
B. [0 2]
C. [2 1]
D. [2 2]
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 A
Quick Check:
np.argmax(arr, axis=1) = [1 2] [OK]
Hint: axis=1 finds max index per row [OK]
Common Mistakes:
Mixing up axis parameter
Confusing row and column indices
Assuming global max index instead of per row
5. You have a 2D NumPy array representing exam scores of 4 students in 3 subjects: