Get indices of N maximum values in a NumPy array in Python

In this article, we will learn how to get indices of N maximum values in a NumPy array.

Table Of Contents

Introduction

Suppose we have a NumPy array,

[1, 5, 8, 3, 9, 7]

The indices of 3 maximum values in the given array are,

[2, 4, 5]

There are multiple ways to get indices of N maximum values in a NumPy array. Lets discuss all the methods one by one with proper approach and a working code examples.

Method 1: Using argpartition() function

The numpy module has argpartition() function. It takes an array and an integer N as input arguments, and returns an array of indices. These indices are the positions of elements of partitioned array.
It considers the element at the Nth position as pivot, and moves all elements smaller than that, to the left. Whereas, the right side of the tha element contains the items which are larger than that.
The order of all elements in the partitions is undefined. Then it returns an array of indices of elements in partioned manner. Actual order of elements in array is untouched, it just returns an array of indices of partioned array.

Now inorder to get the indices of n maximum values we need to print the last n elements in the resultant array. Let’s see an example,

import numpy as np

# creating a numpy array
arr = np.array([1, 5, 8, 3, 9, 7])

n = 3

# getting indices of n maximum values in the array
indices = np.argpartition(arr,n)[-n:]

print(indices)

Output:

[5 4 2]

Method 2: Using argsort() function.

The numpy module has a function argsort(), and it returns the indices that would sort an array. Now, in order to get the indices of n maximum values, we need to print the last n elements of the array returned by the argsort() function. Let’s see an example,

import numpy as np

# creating a numpy array
arr = np.array([1, 5, 8, 3, 9, 7])

n = 3

# getting indices of n maximum values in the array
indices = arr.argsort()[-n:]

print(indices)

Output:

[5 4 2]

Method 3: Using nlargest() of heapq module.

The heapq module have the nlargest() function, and it returns the n large elements from an array. Now, inorder to get indices of n maximum values we need to pass n, indices of the array i.e [0,1,2…length-1], arr.getitem to nlargest() method.

Syntax of heapq.nlargest() function

heapq.nlargest(n, iterable)
  • Parameters:
    • n : int.
    • iterable : array
  • Returns:
    • result_array : array, int : An array of n largest elements.

Approach :

  1. Import numpy library and create numpy array.
  2. pass n, indices of the array i.e [0,1,2…length-1], arr.getitem to nlargest() method.
  3. The resultant array contains the indices of n maximum values in the given array.

Source Code :

import heapq
import numpy as np

# creating a numpy array
arr = np.array([1, 5, 8, 3, 9, 7])

n = 3

# getting indices of n maximum values in the array
indices = heapq.nlargest(n, range(len(arr)), arr.__getitem__)

print(indices)

Output:

[5 2 4]

Method 4. Iterating and finding indices of n maximum values

Find the maximum element present in the array by using the max() function, and get the index of maximum element using where(). Now, to find the index of N maximum element, repeat the same for n maximum values.

Approach :

  1. Import numpy library and create numpy array.
  2. find the index of first maximum element by using where() and max() methods.
  3. Iterate the array and find index of 2nd maximum element.
  4. repeat step 3, till we get indices of n maximum values.

Source Code :

import numpy as np

# creating a numpy array
arr = np.array([1, 5, 8, 3, 9, 7])

n = 3

# getting index of maximum element.
max_element = arr.max()
max_element_index = np.where(arr == max_element)[0][0]
indices = []
indices.append(max_element_index)

# getting indices of remaing maximum values in the array
for j in range(n-1):
    next_max_element = float('-inf')
    for i in range(arr.size):
        if(arr[i]<max_element and arr[i]>next_max_element):
            next_max_element = arr[i]
            next_max_element_index = i
    max_element = next_max_element
    indices.append(next_max_element_index)

print(indices)

Output:

[4, 2, 5]

Summary

Great! you made it. We have discussed All possible methods to get the indices of N maximum values in a NumPy Array. Hppy learning.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top