HOW TO RETURN ALL THE MAXIMUM INDICES IN NUMPY

In this topic, we will discuss how to easily and interestingly return all the maximum indices in a NumPy array using Python programming. Let’s explore the methods we will use to find all the indices of maximum elements in a NumPy array.

Return All The Indices Of Maximum Elements in NumPy

 

Table of Contents:

  • Introduction
  • Program to return indices of max elements:
  • Output
  • Conclusion

Introduction:

To find the indices of maximum elements in a NumPy array efficiently, you can utilize appropriate methods and functions.

  1. numpy.argmax()
  2. numpy.where() – By find out the max elements.

Here we use both methods to find the maximum elements of a NumPy array.

“The `numpy.argmax()` method returns the indices of the maximum elements of a NumPy array along a specified axis.”

Syntax:
numpy.argmax(input_arr, axis=None)
Parameters:
intput_arr  :  Input array from the user.

axis        :  Specified axis 0 or 1 ( axis=0 denotes row, axis=1 denotes column)

The `numpy.where()` method is used by passing a condition as an argument, such as `input_arr == max_ele`.

It identifies the indices where this condition holds true within the NumPy array, effectively finding the locations of maximum elements..

Program:

Example using “numpy.argwhere()” method:

 

import numpy as np
input_arr=np.array([[[20,34,21],[4,22,54],[53,24,66]]])
print("Input Numpy array:",input_arr) 
max_ele=np.argmax(input_arr)
print("Maximum elements:",max_ele)
print("Indices of Max element By Row : ", np.argmax(input_arr, axis = 0))
print("Indices of Max element By Col : ", np.argmax(input_arr, axis = 1)) 
Output:
Input Numpy array: [[[20 34 21]
[ 4 22 54]
[53 24 66]]]
Maximum elements: 8
Indices of Max element By Row : [[1 2 2]
Indices of Max element By Col : [[2 0 2]]
Example using “numpy.where()” method:

 

import numpy as np
int_arr=np.array([[22, 34,21],
                [33, 54, 44],
                [52, 55, 11]])
max_val = np.max(int_arr)
max_ind = np.where(arr == max_value)
print("Maximum value in the Numpy array:", max_value)
print("Indices of maximum values:")
for i in max_ind:
    print(i)
Output:
Maximum value in the Numpy array: 55
Indices of maximum values:
[2]
[1]
Conclusion:

In conclusion, Python programming offers flexible methods to retrieve the indices of all maximum elements in a NumPy array.  The methods `numpy.argmax()` and `numpy.where()` were successfully employed to find these indices.

Thank you for visiting our site!!!!!……

Leave a Comment

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

Scroll to Top