格式
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.]])