python 筆記,python
## numpy.argmax(a, axis=None, out=None)
返回沿軸axis最大值的索引。
Parameters:
a : array_like
數組
axis : int, 可選
預設情況下,索引的是平鋪的數組,否則沿指定的軸。
out : array, 可選
如果提供,結果以合適的形狀和類型被插入到此數組中。
Returns:
index_array : ndarray of ints
索引數組。它具有與a.shape相同的形狀,其中axis被移除。
例子:
>>> a = np.arange(6).reshape(2,3)>>> aarray([[0, 1, 2], [3, 4, 5]])>>> np.argmax(a)5>>> np.argmax(a, axis=0)#0代表列array([1, 1, 1])>>> np.argmax(a, axis=1)#1代表行array([2, 2])>>>>>> b = np.arange(6)>>> b[1] = 5>>> barray([0, 5, 2, 3, 4, 5])>>> np.argmax(b) # 只返回第一次出現的最大值的索引1
## numpy.max(a, axis=None, out=None)
與argmax類似,但傳回值為最大元素而非索引。
## tf.boolean_mask(a,b)
tensorflow 裡的一個函數,在做目標檢測(YOLO)時常常用到。
其中b一般是bool型的n維向量,若a.shape=[3,3,3] b.shape=[3,3]
則 tf.boolean_mask(a,b) 將使a (m維)矩陣僅保留與b中“True”元素同下標的部分,並將結果展開到m-1維。
例:應用在YOLO演算法中返回所有檢測到的各類目標(車輛、行人、交通標誌等)的位置資訊(bx,by,bh,bw)
a = np.random.randn(3, 3,3)b = np.max(a,-1)c= b >0.5print("a="+str(a))print("b="+str(b))print("c="+str(c))with tf.Session() as sess: d=tf.boolean_mask(a,c)print("d="+str(d.eval(session=sess)))
>>
a=[[[-1.25508127 1.76972539 0.21302597] [-0.2757053 -0.28133549 -0.50394556] [-0.70784415 0.52658374 -3.04217963]] [[ 0.63942957 -0.76669861 -0.2002611 ] [-0.38026374 0.42007134 -1.08306957] [ 0.30786828 1.80906798 -0.44145949]] [[ 0.22965498 -0.23677034 0.24160667] [ 0.3967085 1.70004822 -0.19343556] [ 0.18405488 -0.95646895 -0.5863234 ]]]b=[[ 1.76972539 -0.2757053 0.52658374] [ 0.63942957 0.42007134 1.80906798] [ 0.24160667 1.70004822 0.18405488]]c=[[ True False True] [ True False True] [False True False]]d=[[-1.25508127 1.76972539 0.21302597] [-0.70784415 0.52658374 -3.04217963] [ 0.63942957 -0.76669861 -0.2002611 ] [ 0.30786828 1.80906798 -0.44145949] [ 0.3967085 1.70004822 -0.19343556]]