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…length1], 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 :
 Import numpy library and create numpy array.
 pass n, indices of the array i.e [0,1,2…length1], arr.getitem to nlargest() method.
 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 :
 Import numpy library and create numpy array.
 find the index of first maximum element by using where() and max() methods.
 Iterate the array and find index of 2nd maximum element.
 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(n1): 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.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.