[英]Why is numpy.where giving me this output?
I am following the documentation page of numpy.where and found the following code: 我正在关注numpy.where的文档页面,并找到以下代码:
>>>x = np.arange(9.).reshape(3, 3)
>>>x
array([[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.]])
>>>np.where( x > 5 )
(array([2, 2, 2]), array([0, 1, 2]))
I don't understand why np.where( x > 5 ) gives the output mentioned. 我不明白为什么np.where(x> 5)给出输出。 I am sorry if this has been asked before but I didn't find any relevant question.
很抱歉,是否曾经有人问过这个问题,但是我没有找到任何相关的问题。 Please help.
请帮忙。
numpy.where
returns the indices where your condition was True
. numpy.where
返回条件为True
的索引。 So in your example x > 5
is True
at the following indices 因此,在您的示例中,
x > 5
在以下索引处为True
[(2,0), (2,1), (2,2)]
# ^6. ^7. ^8.
This can be useful if you want to extract those elements from the original array, eg 如果您想从原始数组中提取那些元素,这可能会很有用,例如
>>> x[np.where( x > 5 )]
array([6., 7., 8.])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.