Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of numpy.where returning a tuple?

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

like image 526
Basj Avatar asked Jun 01 '18 14:06

Basj


People also ask

What does NumPy where return?

where() in Python. The numpy. where() function returns the indices of elements in an input array where the given condition is satisfied.

What is NumPy tuple?

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.

What is the purpose of NumPy?

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.

How do you convert a tuple to an array in Python?

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.


1 Answers

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 
like image 52
jpp Avatar answered Sep 18 '22 22:09

jpp