Instagram
youtube
Facebook
Twitter

Index of Minimum Value in NumPy Array

A Index of Minimum Value in NumPy Array?
Code Explanation:
Library Import:
numpy is imported as np to work with arrays and functions.
Array Creation: A 2D NumPy array arr is created with integer values.
Find Min Index (Flat): np.argmin(arr) returns the index of the minimum value in the flattened array.
Convert to 2D Index: np.unravel_index(..., arr.shape) converts the flat index into row and column indices based on the original shape of the array.
Store Result: The resulting index is stored in the variable index.
Display Output: print(index) prints the position of the minimum value in the array.

 

Program:

import numpy as np

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

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

print(index)