How can I obtain the indices where a certain value is True using NumPy?

How can I obtain the indices where a certain value is True using NumPy?

The process of obtaining the indices where a certain value is True using NumPy involves using the “np.where()” function. This function takes in an array and a condition as parameters, and returns the indices where the condition is met. In the case of finding True values, the condition would be “array == True”. This function is useful for locating specific values within an array and can be used in various data analysis and manipulation tasks.

NumPy: Get Indices Where Value is True


You can use the following methods to get the indices where some condition is true in NumPy:

Method 1: Get Indices Where Condition is True in NumPy Array

#get indices of values greater than 10
np.asarray(my_array>10).nonzero()

Method 2: Get Indices Where Condition is True in NumPy Matrix

#get indices of values greater than 10
np.transpose((my_matrix>10).nonzero())

Method 3: Get Indices Where Condition is True in Any Row of NumPy Matrix

#get indices of rows where any value is greater than 10np.asarray(np.any(my_matrix>10, axis=1)).nonzero()

The following examples show how to use each method in practice.

Example 1: Get Indices Where Condition is True in NumPy Array

The following code shows how to get all indices in a NumPy array where the value is greater than 10:

import numpy as np

#create NumPy array
my_array = np.array([2, 2, 4, 5, 7, 9, 11, 12, 3, 19])

#get index of values greater than 10
np.asarray(my_array>10).nonzero()

(array([6, 7, 9], dtype=int32),)

From the output we can see that the values in index positions 6, 7, and 9 of the original NumPy array have values greater than 10.

Example 2: Get Indices Where Condition is True in NumPy Matrix

The following code shows how to get all indices in a NumPy matrix where the value is greater than 10:

import numpy as np

#create NumPy matrix
my_matrix = np.array([[2, 5, 9, 12],
                     [6, 7, 8, 8],
                     [2, 5, 7, 8],
                     [4, 1, 15, 11]])

#get index of values greater than 10
np.transpose((my_matrix>10).nonzero())

array([[0, 3],
       [3, 2],
       [3, 3]], dtype=int32)

From the output we can see that the values in the following index positions of the matrix have values greater than 10:

  • [0, 3]
  • [3, 2]
  • [3, 3]

Example 3: Get Indices Where Condition is True in Any Row of NumPy Matrix

The following code shows how to get all row indices in a NumPy matrix where the value is greater than 10 in any element of the row:

import numpy as np

#create NumPy matrix
my_matrix = np.array([[2, 5, 9, 12],
                     [6, 7, 8, 8],
                     [2, 5, 7, 8],
                     [4, 1, 15, 11]])

#get index of rows where any value is greater than 10
np.asarray(np.any(my_matrix>10, axis=1)).nonzero()

(array([0, 3], dtype=int32),)

From the output we can see that rows 0 and 3 have at least one value greater than 10.

Note: To get indices where a condition is true in a column, use axis=0 instead.

The following tutorials explain how to perform other common tasks in NumPy:

Cite this article

stats writer (2024). How can I obtain the indices where a certain value is True using NumPy?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-obtain-the-indices-where-a-certain-value-is-true-using-numpy/

stats writer. "How can I obtain the indices where a certain value is True using NumPy?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-obtain-the-indices-where-a-certain-value-is-true-using-numpy/.

stats writer. "How can I obtain the indices where a certain value is True using NumPy?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-obtain-the-indices-where-a-certain-value-is-true-using-numpy/.

stats writer (2024) 'How can I obtain the indices where a certain value is True using NumPy?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-obtain-the-indices-where-a-certain-value-is-true-using-numpy/.

[1] stats writer, "How can I obtain the indices where a certain value is True using NumPy?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I obtain the indices where a certain value is True using NumPy?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top