python及numpy笔记(一)

enumerate()函数

  • enumerate是python内置的函数,用于循环迭代
  • 返回值为一个元组
  • 示例:
    1
    2
    3
    list = ['a', 'b', 'c']
    for index, text in enumerate(list,start=0):
    print index, text

运行结果为

1
2
3
0 a
1 b
2 c

  • enumerate函数等价于如下函数:
    1
    2
    3
    4
    5
    def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
    yield n, elem
    n += 1

yield关键字

  • yield类似于return,只是yield返回的是一个生成器
  • 第一次迭代中函数会执行到yield 关键字,然后返回 yield 后的值作为第一次迭代的返回值. 此后,每次执行这个函数都会从上一次中断的yield处继续执行,然后进入循环,遇到yield返回此次迭代值
  • 即函数只有在每次被调用迭代器时会执行循环
  • 只言片语很难讲清楚yield的准确作用,更详细的解释可以点击这里
  • 给出示例如下,执行程序:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    ls = ['a', 'b', 'c']

    def enem(ls, start=0):
    n = start
    for i in ls:
    print "this is ",i,"in yield"
    yield n,i
    n += 1
    print "n=",n
    index=0
    for n,elem in enem(ls):
    print "this is ",index,"times in for loop"
    print n,elem
    index+=1
  • 输出值为:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    this is  a in yield
    this is 0 times in for loop
    0 a
    n= 1
    this is b in yield
    this is 1 times in for loop
    1 b
    n= 2
    this is c in yield
    this is 2 times in for loop
    2 c
    n= 3

numpy.flatnonzero()函数

  • 输入一个数组,将其展开成一维向量后输出对应的非0值的下标

numpy.random.choice(a, size=None, replace=True, p=None)

  • 输入参数:
    • a:1维向量或者整数。如果a为整数,随机样本与np.arange(a)相同
    • size:输出的元素个数
    • replace:bool值,replace=False时表示样本不可重复被选取
    • p:对应a中元素被选取的概率,可选,默认为平均分布

numpy的broadcast

numpy中的矩阵运算会具有broadcast性质,决定了在运算时对于不同shape的矩阵会采取不同的操作

The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. It does this without making needless copies of data and usually leads to efficient algorithm implementations. There are, however, cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows computation.

例如:

1
2
3
4
>>> a = np.array([1.0, 2.0, 3.0])
>>> b = 2.0
>>> a * b
array([ 2., 4., 6.])

numpy会通过broadcast方式对于不符合shape的矩阵自动将其扩展后再进行运算,这一性质在许多情况下可以使得代码更加简洁,但是也常常容易使人感到困惑。