Bird
Raised Fist0
NumPydata~10 mins

np.argsort() for sort indices in NumPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - np.argsort() for sort indices
Input Array
↓
Compute Indices that Sort
↓
Return Sorted Indices Array
↓
Use Indices to Access Sorted Elements
np.argsort() takes an array and returns the indices that would sort the array. These indices can then be used to reorder the original array.
Execution Sample
NumPy
import numpy as np
arr = np.array([30, 10, 20])
indices = np.argsort(arr)
sorted_arr = arr[indices]
This code finds the indices that sort the array and then uses them to get the sorted array.
Execution Table
StepActionArray StateIndices ComputedResult
1Start with array[30, 10, 20]N/AN/A
2Call np.argsort(arr)[30, 10, 20][1, 2, 0]Indices that sort array
3Use indices to reorder array[30, 10, 20][1, 2, 0][10, 20, 30]
4End[30, 10, 20][1, 2, 0]Sorted array obtained
💡 All steps completed; indices correctly represent sorting order
Variable Tracker
VariableStartAfter np.argsortAfter indexingFinal
arr[30, 10, 20][30, 10, 20][30, 10, 20][30, 10, 20]
indicesN/A[1, 2, 0][1, 2, 0][1, 2, 0]
sorted_arrN/AN/A[10, 20, 30][10, 20, 30]
Key Moments - 3 Insights
Why does np.argsort return indices instead of the sorted array?
np.argsort returns indices to show the order to rearrange the original array without changing it. This is shown in execution_table step 2, where indices [1, 2, 0] tell us how to reorder arr.
How do we get the sorted array from the indices?
By using the indices to index the original array, as in execution_table step 3, arr[indices] gives the sorted array [10, 20, 30].
What if the array has duplicate values?
np.argsort will return indices that sort duplicates in their original order (stable sort). This behavior is consistent with the indices shown in the example.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what does the indices array [1, 2, 0] represent?
APositions to rearrange arr to sorted order
BSorted values of arr
COriginal array reversed
DIndices of maximum values
💡 Hint
Check the 'Indices Computed' column at step 2 in execution_table
At which step in the execution_table do we get the sorted array values?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Result' column for the sorted array values
If the original array was [5, 5, 1], what would np.argsort(arr) return?
A[2, 1, 0]
B[0, 1, 2]
C[2, 0, 1]
D[1, 0, 2]
💡 Hint
Indices should sort the array: smallest value index first, duplicates keep original order
Concept Snapshot
np.argsort(array) returns indices that sort the array.
Use these indices to reorder the original array.
It does not sort the array itself.
Useful for sorting related arrays or stable sorting.
Example: indices = np.argsort(arr); sorted_arr = arr[indices]
Full Transcript
np.argsort() is a function in numpy that returns the indices which would sort an array. Instead of returning the sorted array directly, it gives you the order to rearrange the original array. For example, if you have an array [30, 10, 20], np.argsort returns [1, 2, 0] because the element at index 1 (10) is the smallest, then index 2 (20), then index 0 (30). You can then use these indices to get the sorted array by indexing the original array with them. This method is helpful when you want to sort multiple related arrays in the same order or keep track of the original positions. The execution table shows each step: starting with the array, computing indices, using them to reorder, and obtaining the sorted array. Key points include understanding that argsort returns indices, not sorted values, and how to use those indices to get sorted data.

Practice

(1/5)
1. What does the np.argsort() function return when applied to a numpy array?
easy
A. The sum of all elements in the array
B. The sorted array itself
C. The maximum value in the array
D. An array of indices that would sort the original array

Solution

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

    This function does not sort the array directly but returns the indices that would sort the array.
  2. Step 2: Differentiate from sorting functions

    Unlike np.sort() which returns the sorted array, np.argsort() returns the order of indices to achieve that sorting.
  3. Final Answer:

    An array of indices that would sort the original array -> Option D
  4. Quick Check:

    np.argsort() = indices order [OK]
Hint: Remember: argsort returns indices, not sorted values [OK]
Common Mistakes:
  • Confusing argsort with sort and expecting sorted values
  • Thinking argsort returns the maximum or minimum value
  • Assuming argsort returns a scalar instead of an array
2. Which of the following is the correct syntax to get the indices that would sort the array arr using a NumPy function?
easy
A. arr.sort()
B. np.argsort(arr)
C. np.sort(arr)
D. arr.argsort()

Solution

  1. Step 1: Identify the numpy function for argsort

    The function np.argsort() is called with the array as argument: np.argsort(arr).
  2. Step 2: Differentiate from other methods

    arr.argsort() is an array method (not the NumPy function), while np.sort(arr) returns sorted values, and arr.sort() sorts in place.
  3. Final Answer:

    np.argsort(arr) -> Option B
  4. Quick Check:

    Correct function call = np.argsort(arr) [OK]
Hint: Use np.argsort(array), the NumPy function, to get sort indices [OK]
Common Mistakes:
  • Using arr.argsort() (array method instead of NumPy function)
  • Confusing np.sort() with np.argsort()
  • Using arr.sort() which sorts in place and returns None
3. Given the code:
import numpy as np
arr = np.array([40, 10, 30, 20])
indices = np.argsort(arr)
print(indices)

What will be the output?
medium
A. [1 3 2 0]
B. [3 2 1 0]
C. [0 1 2 3]
D. [1 2 3 0]

Solution

  1. Step 1: Understand the array and sorting order

    The array is [40, 10, 30, 20]. Sorting it ascending gives [10, 20, 30, 40].
  2. Step 2: Find indices that sort the array

    10 is at index 1, 20 at index 3, 30 at index 2, and 40 at index 0. So, indices are [1, 3, 2, 0].
  3. Final Answer:

    [1 3 2 0] -> Option A
  4. Quick Check:

    Sorted indices = [1 3 2 0] [OK]
Hint: Match sorted values to original indices for argsort output [OK]
Common Mistakes:
  • Confusing sorted values with indices
  • Reversing the order of indices
  • Using sorted array instead of indices
4. What is wrong with this code snippet?
import numpy as np
arr = np.array([3, 1, 2])
indices = arr.argsort()
print(indices)
medium
A. The code will run correctly and print the sorted indices
B. The method argsort() does not exist for numpy arrays
C. The array must be sorted before calling argsort()
D. The print statement is missing parentheses

Solution

  1. Step 1: Check if argsort() is a valid numpy array method

    In numpy, arrays do have an argsort() method, so arr.argsort() is valid.
  2. Step 2: Verify code correctness

    The code will run and print the indices that sort the array, which are [1, 2, 0].
  3. Final Answer:

    The code will run correctly and print the sorted indices -> Option A
  4. Quick Check:

    arr.argsort() is valid and works [OK]
Hint: Remember numpy arrays have argsort() method too [OK]
Common Mistakes:
  • Assuming argsort() is only in np module, not array method
  • Thinking array must be sorted before argsort()
  • Confusing Python 2 print syntax with Python 3
5. You have two related numpy arrays:
names = np.array(['apple', 'banana', 'cherry', 'date'])
prices = np.array([3.5, 2.0, 4.0, 1.5])

You want to list the fruit names sorted by their prices in ascending order. Which code snippet correctly achieves this?
hard
A. sorted_names = np.argsort(names)[prices]
B. sorted_names = np.sort(names)[np.argsort(prices)]
C. sorted_names = names[np.argsort(prices)]
D. sorted_names = names[np.sort(prices)]

Solution

  1. Step 1: Use np.argsort(prices) to get indices that sort prices

    This returns indices that sort prices ascending.
  2. Step 2: Use these indices to reorder names

    Indexing names with these indices sorts names by price.
  3. Final Answer:

    sorted_names = names[np.argsort(prices)] -> Option C
  4. Quick Check:

    Index names by argsort(prices) to sort by price [OK]
Hint: Index names by argsort of prices to sort related arrays [OK]
Common Mistakes:
  • Trying to sort names directly without using indices
  • Using np.sort(names) which sorts names alphabetically
  • Indexing with sorted prices instead of indices