8.2 Where and argmin

Download notebook Interact

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])

This also works in two or more dimensions. Here is a two-dimensional array, with some values of 99.

arr2d = np.array([[4, 99, 3], [8, 8, 99]])
arr2d
array([[ 4, 99,  3],
       [ 8,  8, 99]])

where now returns two index arrays, one for the rows, and one for the columns.

indices2d = np.where(arr2d == 99)
indices2d
(array([0, 1]), array([1, 2]))

Just as for the one-dimensional case, we can use the returned indices to index into the array, and get the elements.

arr2d[indices2d]
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

Numpy has various argmin 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

We could also have found the position of the minimum value above, using np.min and where:

min_value = np.min(arr)
min_indices = np.where(arr == min_value)
arr[min_indices]
array([-1])

The argmin and argmax functions are not quite the same, in that they only return the first position of the minimum or maximum, if there are multiple values with the same value.

Compare:

np.argmax(arr)
1

to

max_value = np.max(arr)
np.where(arr == max_value)
(array([1, 4]),)