Faster-R-CNN(Python).3: numpy.where()函式
阿新 • • 發佈:2019-02-15
讀程式碼時候遇到numpy.where(),費了半天勁,終於理解了,分享一下。
格式
numpy.where(condition[, x, y])
引數
condition : array_like, bool
if conditon == 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]])
array([[1, 8],
[3, 4]]) # True時從x取值,False時從y取值
>>> np.where([[0, 1], [1, 0]]) # 只有condition,返回非零值的座標
(array([0, 1]), array([1, 0]))
>>> np.where([[0,1],[1,2]] )
(array([0,1,1]), array([1,0,1]))
>>> x = np.arange(9.).reshape(3, 3)
>>> np.where( x > 5 )
(array([2, 2, 2]), array([0, 1, 2]))
>>> x[np.where( x > 3.0 )] # Note: result is 1D.
array([ 4., 5., 6., 7., 8.])
>>> np.where(x < 5, x, -1 ) # 值替換
array([[ 0., 1., 2.],
[ 3., 4., -1.],
[-1., -1., -1.]])
參考
Bonne courage!