Where and argmin

We sometimes want to know where a value is in an array.

import numpy as np

By “where” we mean, which element contains a particular value.

Here is an array.

arr = np.array([2, 99, -1, 4, 99])
arr
array([ 2, 99, -1,  4, 99])

As you know, we can get element using their index in the array. In Python, array indices start at zero.

Here’s the value at index (position) 0:

arr[0]
2

We might also be interested to find which positions hold particular values.

In our array above, by reading, and counting positions, we can see that the values of 99 are in positions 1 and 4. We can ask for these elements by passing a list or an array between the square brackets, to index the array:

positions_with_99 = np.array([1, 4])
arr[positions_with_99]
array([99, 99])

Of course, we are already used to finding and then selecting elements according to various conditions, using Boolean vectors.

Here we identify the elements that contain 99. There is a True at the position where the array contains 99, and False otherwise.

contains_99 = arr == 99
contains_99
array([False,  True, False, False,  True])

We can then get the 99 values with:

arr[contains_99]
array([99, 99])

Enter “where”

Sometimes we really do need to know the index of the values that meet a certain condition.

In that case, you can use the Numpy where function. where finds the index positions of the True values in Boolean vectors.

indices = np.where(arr == 99)
indices
(array([1, 4]),)

We can use the returned indices to index into the array, using square brackets.

arr[indices]
array([99, 99])

Where summary

Numpy where returns the indices of True values in a Boolean array.

You can use these indices to index into an array, and get the matching elements.

Argmin, argmax

Numpy has various arg- functions that are a shortcut for using where, for particular cases.

A typical case is where you want to know the index (position) of the minimum value in an array.

Here is our array:

arr
array([ 2, 99, -1,  4, 99])

We can get the minimum value with Numpy min:

np.min(arr)
-1

Sometimes we want to know the index position of the minimum value. Numpy argmin returns the index of the minimum value:

min_pos = np.argmin(arr)
min_pos
2

Therefore, we can get the minimum value again with:

arr[min_pos]
-1

There is a matching argmax function that returns the position of the maximum value:

np.max(arr)
99
max_pos = np.argmax(arr)
max_pos
1
arr[max_pos]
99

Notice that there are two values of 99 in this array, and therefore, two maximum values. np.argmax returns the index of the first maximum values. np.argmin does the same, if there is more than one minimum value.