When I run this code:
import numpy as np a = np.array([1, 2, 3, 4, 5, 6]) print(np.where(a > 2))
it would be natural to get an array of indices where a > 2
, i.e. [2, 3, 4, 5]
, but instead we get:
(array([2, 3, 4, 5], dtype=int64),)
i.e. a tuple with empty second member.
Then, to get the the "natural" answer of numpy.where
, we have to do:
np.where(a > 2)[0]
What's the point in this tuple? In which situation is it useful?
Note: I'm speaking here only about the use case numpy.where(cond)
and not numpy.where(cond, x, y)
that also exists (see documentation).
where() in Python. The numpy. where() function returns the indices of elements in an input array where the given condition is satisfied.
A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.
NumPy aims to provide an array object that is up to 50x faster than traditional Python lists. The array object in NumPy is called ndarray , it provides a lot of supporting functions that make working with ndarray very easy. Arrays are very frequently used in data science, where speed and resources are very important.
To convert a tuple of lists into an array, use the np. asarray() function and then flatten the array using the flatten() method to convert it into a one-dimensional array.
numpy.where
returns a tuple because each element of the tuple refers to a dimension.
Consider this example in 2 dimensions:
a = np.array([[1, 2, 3, 4, 5, 6], [-2, 1, 2, 3, 4, 5]]) print(np.where(a > 2)) (array([0, 0, 0, 0, 1, 1, 1], dtype=int64), array([2, 3, 4, 5, 3, 4, 5], dtype=int64))
As you can see, the first element of the tuple refers to the first dimension of relevant elements; the second element refers to the second dimension.
This is a convention numpy
often uses. You will see it also when you ask for the shape of an array, i.e. the shape of a 1-dimensional array will return a tuple with 1 element:
a = np.array([[1, 2, 3, 4, 5, 6], [-2, 1, 2, 3, 4, 5]]) print(a.shape, a.ndim) # (2, 6) 2 b = np.array([1, 2, 3, 4, 5, 6]) print(b.shape, b.ndim) # (6,) 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With