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)
>>> 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类似,但返回值为最大元素而非索引。

##  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.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]]
 

 ##E

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'}

 
原文地址:https://www.cnblogs.com/lyc-seu/p/7944196.html