我看过这篇文章Difference between nonzero(a), where(a) and argwhere(a). When to use which?,但是我并不是很理解numpy模块中where函数的用法。
例如,我有以下代码:
import numpy as np
Z =np.array(
[[1,0,1,1,0,0],
[0,0,0,1,0,0],
[0,1,0,1,0,0],
[0,0,1,1,0,0],
[0,1,0,0,0,0],
[0,0,0,0,0,0]])
print Z
print np.where(Z)
这将会得到:
(array([0, 0, 0, 1, 2, 2, 3, 3, 4], dtype=int64),
array([0, 2, 3, 3, 1, 3, 2, 3, 1], dtype=int64))
where函数的定义是:根据条件返回x或y中的元素。但对我来说这并没有意义。
那么输出结果究竟是什么意思呢?
np.where(condition, x, y)
时,它会执行你引用的操作。如果你省略了x
和y
参数,它等同于np.nonzero
。 - Jaime