find the positions of the minima using numpy in python

Here's a code snippet that finds the positions of the minima in a numpy array using argwhere and argmin functions:

main.py
import numpy as np

# create a numpy array
arr = np.array([1, 2, 1, 4, 5, 0, 1, 2])

# find the minimum value in the array
min_val = np.min(arr)

# find the indices of the minimum values
min_idxs = np.argwhere(arr == min_val)

print("Minimum value:", min_val)
print("Indices of minima:", min_idxs)
298 chars
14 lines

The output of this code will be:

main.py
Minimum value: 0
Indices of minima: [[5]]
42 chars
3 lines

Here, argwhere returns a list of indices where the condition arr == min_val holds true, and argmin returns the first occurrence of the minimum value in the array.

gistlibby LogSnag