格式
numpy.where(condition[,x,y])
参数
condition : array_like, bool
if condition == True
取当前位置的 x 值
else :
  取当前位置的 y 值
x, y : array_like, optional, x 和 y 与 condition 尺寸相同
返回值
返回一个数组,或者由数组组成的元组。
根据定义条件返回元素,这些元素或者从 x 中获得,或者从 y 中获得。
如果只给出条件,没有给出 [,x, y] , 返回条件中非零 (True) 元素的坐标。
实例理解
np.where([ [True, False], [True, True] ], ...
[ [1, 2], [3, 4]], [ [9, 8], [7, 6]])
# 结果为 [ [1, 8], [3, 4]], True 时从 x 中取值, False
# 时从 y 中取值
np.where([[0, 1], [1, 0]])
# 只有 condition, 返回非零值的坐标
# 其结果为 (array([0,1]), array([1, 0]))
# 第一个 array 中的元素代表行,第二个 array 中的元素代表列
# 可以方便进行值替换
x = np.arange(9.).reshape(3,3)
np.where( x<5, x, -1)
# 输出结果为
# array([ [0., 1., 2.],
# [3., 4., -1.],
# [-1., -1., -1.]])