Instagram
youtube
Facebook
Twitter

Index of Maximum Value in NumPy Array

A Index of Maximum Value in NumPy Array?
Code Explanation:
Library Import:
numpy is imported as np to use array and indexing functions.
Array Creation: A 2D NumPy array arr is created with integer values.
Find Max Index (Flat): np.argmax(arr) returns the index of the maximum value in the flattened version of the array.
Convert to 2D Index: np.unravel_index(..., arr.shape) converts the flat index into a tuple representing the row and column indices in the 2D array.
Store Result: The resulting 2D index is stored in the variable index.
Display Output: print(index) prints the position of the maximum value in the array.

 

Program:

import numpy as np

arr = np.array([[1, 5], [3, 7]])

index = np.unravel_index(np.argmax(arr), arr.shape)

print(index)