Bird
Raised Fist0
NumPydata~10 mins

np.argmin() and np.argmax() in NumPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the index of the smallest value in the array.

NumPy
import numpy as np
arr = np.array([3, 1, 4, 1, 5])
index_min = np.[1](arr)
print(index_min)
Drag options to blanks, or click blank then click option'
Amin
Bargmax
Cargmin
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.min() which returns the minimum value, not the index.
Using np.argmax() which finds the index of the maximum value.
2fill in blank
medium

Complete the code to find the index of the largest value in the array.

NumPy
import numpy as np
arr = np.array([10, 20, 30, 20, 10])
index_max = np.[1](arr)
print(index_max)
Drag options to blanks, or click blank then click option'
Aargmax
Bargmin
Cmin
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.max() which returns the maximum value, not the index.
Using np.argmin() which finds the index of the minimum value.
3fill in blank
hard

Fix the error in the code to correctly find the index of the smallest value.

NumPy
import numpy as np
arr = np.array([7, 2, 5, 3])
index = np.argmin[1](arr)
print(index)
Drag options to blanks, or click blank then click option'
A<
B(
C{
D[
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of parentheses for function calls.
Missing the opening parenthesis.
4fill in blank
hard

Fill both blanks to create a dictionary of indices of min and max values from the array.

NumPy
import numpy as np
arr = np.array([8, 6, 7, 5, 3, 0, 9])
result = {'min_index': np.[1](arr), 'max_index': np.[2](arr)}
print(result)
Drag options to blanks, or click blank then click option'
Aargmin
Bmin
Cargmax
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.min() or np.max() which return values, not indices.
Mixing up argmin and argmax.
5fill in blank
hard

Fill both blanks to create a dictionary with keys as uppercase words and values as their indices if the index is greater than 2.

NumPy
words = ['apple', 'banana', 'cherry', 'date', 'fig']
result = {word[1]: i for i, word in enumerate(words) if i [2] 2}
print(result)
Drag options to blanks, or click blank then click option'
A.upper()
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to call the upper() method with parentheses.
Using the wrong comparison operator in the if condition.
Trying to modify the index value unnecessarily.

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

  1. Step 1: Understand the purpose of np.argmin()

    The function np.argmin() returns the index (position) of the smallest value in a NumPy array.
  2. Step 2: Differentiate from similar functions

    Unlike np.min() which returns the smallest value itself, np.argmin() returns where that value is located.
  3. Final Answer:

    Finds the index of the smallest value in an array -> Option A
  4. 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

  1. Step 1: Recall the function to find largest value index

    np.argmax(arr) returns the index of the largest value in the array arr.
  2. 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.
  3. Final Answer:

    np.argmax(arr) -> Option C
  4. 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

  1. Step 1: Identify the smallest value in the array

    The array is [3, 1, 4, 1, 5]. The smallest value is 1.
  2. Step 2: Find the index of the first occurrence of the smallest value

    The first 1 appears at index 1 (0-based indexing).
  3. Final Answer:

    1 -> Option B
  4. 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

  1. Step 1: Understand np.argmax with axis=1

    axis=1 means find the index of the largest value in each row.
  2. 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.
  3. 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.
  4. Final Answer:

    [1 2] -> Option A
  5. 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:
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?
hard
A. np.argmax(scores, axis=0)
B. np.argmin(scores, axis=0)
C. np.argmax(scores, axis=1)
D. np.argmin(scores, axis=1)

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    np.argmin(scores, axis=1) -> Option D
  4. Quick Check:

    Lowest subject per student = np.argmin(scores, axis=1) [OK]
Hint: Use axis=1 to find min per row (student) [OK]
Common Mistakes:
  • 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard