4

我阅读了关于numpy 的 where的文档,但我不明白如果我检查一个简单的一维数组的条件,为什么where会返回一个嵌套数组的元组。

> import numpy as np
> my_array = np.random.randint(1,10, (20))
> np.where(my_array > 5)

(array([ 0,  1,  4,  6,  7,  8, 10, 11, 13, 15, 16, 17, 18]),)

为什么np.where在这种情况下返回一个元组?为什么嵌套结果?

4

1 回答 1

6

我认为这是为了一致性,考虑一个二维数组:

import numpy as np
my_array = np.random.randint(1,10, (4, 5))
pos = np.where(my_array > 5)
my_array[pos]

您可以使用元组作为索引来选择位置中的所有值。

于 2013-03-15T22:36:54.877 回答