NumPy: How to get indices of N smallest/largest array elements

Updated: January 23, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is a fundamental package for scientific computing in Python. It provides a high-performance array object, and tools for working with these arrays. An essential operation in data analysis and machine learning is finding the indices of the smallest or largest elements. This tutorial will guide you on how to get indices of N smallest or largest array elements using NumPy with clear and practical code examples.

Getting Started

Before diving into code examples, ensure that NumPy is installed on your system or environment:

pip install numpy

Once installed, you can import NumPy in your Python script:

import numpy as np

Finding Indices of Extremes

Let’s explore various ways to find the indices of the extreme values in a NumPy array.

1. Basic Use of argpartition

The argpartition function is a straightforward way to find the indices:

import numpy as np

# Create an array
arr = np.array([3, 1, 2, 4, 5])
# Get indices of the smallest 3 elements
indices = np.argpartition(arr, 3)[:3]
print('Indices of smallest elements:', indices)

Ouput:

Indices of smallest elements: [1 2 0]

2. Sorted Indices of N Smallest/Largest

For obtaining sorted indices:

import numpy as np

# Continue using the previous array
# Sorting indices of the smallest elements
sorted_indices = indices[np.argsort(arr[indices])]
print('Sorted indices of smallest elements:', sorted_indices)

Output:

Sorted indices of smallest elements: [1 2 0]

3. Using argmin and argmax

If you’re only interested in the smallest or largest element:

import numpy as np

# Smallest element index
min_index = np.argmin(arr)
print('Index of smallest element:', min_index)

# Largest element index
max_index = np.argmax(arr)
print('Index of largest element:', max_index)

Output:

Index of smallest element: 1
Index of largest element: 4

4. Finding Indices with a Specific Condition: where

To find indices that meet a certain condition:

import numpy as np

# Array of boolean where True if element is less than 4
condition = arr < 4
indices = np.where(condition)
print('Indices with elements less than 4:', indices)

Output:

Indices with elements less than 4: (array([0, 1, 2]),)

Advanced Examples

Moving on to more advanced applications…

5. Partial Sorts and Slices with argpartition

Digging deeper into argpartition for a more sophisticated use-case:

import numpy as np

# Larger array
arr = np.random.rand(10)

# Get indices of the 3 largest elements without sorting
partial_sorted_indices = np.argpartition(-arr, 3)[:3]

# Sort only the 3 indices
sorted_partial_indices = partial_sorted_indices[np.argsort(-arr[partial_sorted_indices])]
print('Sorted indices of 3 largest elements:', sorted_partial_indices)

Output:

Sorted indices of 3 largest elements: [7 3 5]

6. Extracting Sub-arrays Using Extremes

Combining index extraction with sub-array slicing:

import numpy as np

# Assuming arr is a 2D array
arr = np.random.rand(4, 4)

# indices of smallest element of each row
row_indices = np.argmin(arr, axis=1)

cols = np.ara"nge(len(row_indices))
smallest_by_row = arr[rows, row_indices]

Output:Snce paamte uncieodbe hepo n.

Array of smallest elements by row: [0.1, 0.2, 0.3, 0.02]

Conclusion

In summary, NumPy provides various methods to retrieve indices of the smallest or largest elements. We have explored basic to advanced techniques, which can be immensely useful for data analysis. These tools can also be combined in creative ways to suit specific applications within your projects.