简体   繁体   English

了解numpy.where

[英]Understanding about the numpy.where

I am reading the numpy.where(condition[, x, y]) documentation , but I can not understand the small example: 我正在阅读numpy.where(condition[, x, y]) 文档 ,但我无法理解这个小例子:

>>> x = np.arange(9.).reshape(3, 3)
>>> np.where( x > 5 )
Out: (array([2, 2, 2]), array([0, 1, 2]))

Can some one explain how the result comes? 有人可以解释一下结果如何?

The first array ( array([2, 2, 2]) ) is the index of rows and the second ( array([0, 1, 2]) ) is the columns of those values that are more than 5. 第一个数组( array([2, 2, 2]) )是行的索引,第二个array([0, 1, 2]) )是那些大于5的值的列。

You can use zip to get the exact index of values : 您可以使用zip来获取值的确切索引:

>>> zip(*np.where( x > 5 ))
[(2, 0), (2, 1), (2, 2)]

Or use np.dstack : 或者使用np.dstack

>>> np.dstack(np.where( x > 5 ))
array([[[2, 0],
        [2, 1],
        [2, 2]]])

It's printing out the coordinates to your condition 它会根据您的情况打印出坐标

import numpy as np

x = np.arange(9.).reshape(3, 3)
print x
print np.where( x > 5 )

where print x prints: 打印x打印的位置:

[[ 0.  1.  2.]
 [ 3.  4.  5.]
 [ 6.  7.  8.]]

and np.where( x > 5 ) prints the index location of all elements greater than 5 np.where( x > 5 )打印大于5的所有元素的索引位置

(array([2, 2, 2]), array([0, 1, 2]))

where 2,0 == 6 and 2,1 == 7 and 2,2 == 8 其中2,0 == 6和2,1 == 7和2,2 == 8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM