python 筆記

##  numpy.argmax(a, axis=None, out=None) 
    返回沿軸axis最大值的索引。python

    Parameters: 
    a : array_like 
    數組 
    axis : int, 可選 
    默認狀況下,索引的是平鋪的數組,不然沿指定的軸。 
    out : array, 可選 
    若是提供,結果以合適的形狀和類型被插入到此數組中。 
    Returns: 
    index_array : ndarray of ints 
    索引數組。它具備與a.shape相同的形狀,其中axis被移除。 
例子:算法

>>> a = np.arange(6).reshape(2,3)
>>> a array([[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 >>> b array([0, 5, 2, 3, 4, 5]) >>> np.argmax(b) # 只返回第一次出現的最大值的索引 1

##  numpy.max(a, axis=None, out=None) 數組

        與argmax相似,但返回值爲最大元素而非索引。session

 

##  tf.boolean_mask(a,b)dom

    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維。spa

      例:應用在YOLO算法中返回全部檢測到的各種目標(車輛、行人、交通標誌等)的位置信息(bx,by,bh,bw).net

a = np.random.randn(3, 3,3)
b = np.max(a,-1) c= b >0.5 print("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]]
 

 

 ##Erest

 

enumerate:
  • enumerate()是python的內置函數
  • enumerate在字典上是枚舉、列舉的意思
  • 對於一個可迭代的(iterable)/可遍歷的對象(如列表、字符串),enumerate將其組成一個索引序列,利用它能夠同時得到索引和值
  • enumerate多用於在for循環中獲得計數
  • 若是對一個列表,既要遍歷索引又要遍歷元素時,首先能夠這樣寫:
  • list1 = ["這", "是", "一個", "測試"]
  • for i in range (len(list1)): print i ,list1[i]
    • 1
    • 2
    • 3
    • 上述方法有些累贅,利用enumerate()會更加直接和優美:
    list1 = ["這", "是", "一個", "測試"] for index, item in enumerate(list1): print index, item >>> 0 這 1 是 2 一個 3 測試
  • 更詳細的案例:http://blog.csdn.net/churximi/article/details/51648388

 

  ## L

   lower: 返回小寫字母

def lower(self): # real signature unknown; restored from __doc__
"""
B.lower() -> copy of B

Return a copy of B with all ASCII characters converted to lowercase.
"""
pass



## O

       os.path.join()函數

      語法:  os.path.join(path1[,path2[,......]])

      返回值:將多個路徑組合後返回

  注:第一個絕對路徑以前的參數將被忽略

  >>>  import os
  >>> os.path.join('/hello/','good/boy/','doiido')
  '/hello/good/boy/doiido'

 

## S

   set() 函數:返回對象的一個集合

   例如: set('boy')

   --> {'b','o','y'}

   set(['class1', 'class2', 'class1'])

   ->> {'class1', 'class2'}

相關文章
相關標籤/搜索