NumPy array - Basic usage

The NumPy where() returns all the indices of the array elements that fulfill the given conditions.

import numpy as np

a = np.array([5, 6, 7, 7, 4])

b = np.where(a > 5)
c = a[b]

print(b)  # (array([1, 2, 3]),)
print(type(b))  # <class 'tuple'>
print(c)  # [6 7 7]

In the above, a > 5 is a condition. The return is a tuple that contains the indices. c is an subarray of a and its elements satisfy the condition.

Comments

Powered by Markdown

More