How to get indices of N maximum values in a NumPy array?

By ayush goel in 22 Sep 2023 | 04:16 pm
ayush goel

ayush goel

Student
Posts: 346
Member since: 21 Sep 2023

How to get indices of N maximum values in a NumPy array?

22 Sep 2023 | 04:16 pm
0 Likes
divas goyal

divas goyal

Student
Posts: 453
Member since: 22 Sep 2023

To get the indices of the N maximum values in a NumPy array, you can use the `numpy.argsort()` function. Here's a step-by-step guide on how to do it:


1. First, import NumPy:

   

python

   import numpy as np



2. Create your NumPy array. Let's assume you have an array named `data`.


3. Use `numpy.argsort()` to get the indices that would sort the array in ascending order:


python

   sorted_indices = np.argsort(data)



4. To get the indices of the N maximum values, you can slice the `sorted_indices` array from the end, taking the last N elements:


python

   N = 5  # Change this to the desired number of maximum values

   max_indices = sorted_indices[-N:]




22 Sep 2023 | 05:48 pm
0 Likes

Report

Please describe about the report short and clearly.