ufunc 对象¶

Numpy 有两种基本对象:ndarray (N-dimensional array object)ufunc (universal function object)ndarray 是存储单一数据类型的多维数组,而 ufunc 则是能够对数组进行处理的函数。

例如,我们之前所接触到的二元操作符对应的 Numpy 函数,如 add,就是一种 ufunc 对象,它可以作用于数组的每个元素。

In [1]:

  1. import numpy as np

In [2]:

  1. a = np.array([0,1,2])
  2. b = np.array([2,3,4])
  3.  
  4. np.add(a, b)

Out[2]:

  1. array([2, 4, 6])

查看支持的方法:

In [3]:

  1. dir(np.add)

Out[3]:

  1. ['__call__',
  2. '__class__',
  3. '__delattr__',
  4. '__doc__',
  5. '__format__',
  6. '__getattribute__',
  7. '__hash__',
  8. '__init__',
  9. '__name__',
  10. '__new__',
  11. '__reduce__',
  12. '__reduce_ex__',
  13. '__repr__',
  14. '__setattr__',
  15. '__sizeof__',
  16. '__str__',
  17. '__subclasshook__',
  18. 'accumulate',
  19. 'at',
  20. 'identity',
  21. 'nargs',
  22. 'nin',
  23. 'nout',
  24. 'ntypes',
  25. 'outer',
  26. 'reduce',
  27. 'reduceat',
  28. 'signature',
  29. 'types']

除此之外,大部分能够作用于数组的数学函数如三角函数等,都是 ufunc 对象。

特别地,对于二元操作符所对应的 ufunc 对象,支持以下方法:

reduce 方法¶

  1. op.reduce(a)

op沿着某个轴应用,使得数组 a 的维数降低一维。

add 作用到一维数组上相当于求和:

\begin{align}y & = add.recuce(a) \& = a[0] + a[1] + … + a[N-1] \& = \sum_{n=0}^{N-1} a[n]\end{align}

In [4]:

  1. a = np.array([1,2,3,4])
  2.  
  3. np.add.reduce(a)

Out[4]:

  1. 10

多维数组默认只按照第一维进行运算:

In [5]:

  1. a = np.array([[1,2,3],[4,5,6]])
  2.  
  3. np.add.reduce(a)

Out[5]:

  1. array([5, 7, 9])

指定维度:

In [6]:

  1. np.add.reduce(a, 1)

Out[6]:

  1. array([ 6, 15])

作用于字符串:

In [7]:

  1. a = np.array(['ab', 'cd', 'ef'], np.object)
  2.  
  3. np.add.reduce(a)

Out[7]:

  1. 'abcdef'

逻辑运算:

In [8]:

  1. a = np.array([1,1,0,1])
  2.  
  3. np.logical_and.reduce(a)

Out[8]:

  1. False

In [9]:

  1. np.logical_or.reduce(a)

Out[9]:

  1. True

accumulate 方法¶

  1. op.accumulate(a)

accumulate 可以看成保存 reduce 每一步的结果所形成的数组。

\begin{align}y & = add.accumulate(a) \& = \left[\sum{n=0}^{0} a[n], \sum{n=0}^{1} a[n], …, \sum_{n=0}^{N-1} a[n]\right]\end{align}

与之前类似:

In [10]:

  1. a = np.array([1,2,3,4])
  2.  
  3. np.add.accumulate(a)

Out[10]:

  1. array([ 1, 3, 6, 10])

In [11]:

  1. a = np.array(['ab', 'cd', 'ef'], np.object)
  2.  
  3. np.add.accumulate(a)

Out[11]:

  1. array(['ab', 'abcd', 'abcdef'], dtype=object)

In [12]:

  1. a = np.array([1,1,0,1])
  2.  
  3. np.logical_and.accumulate(a)

Out[12]:

  1. array([ True, True, False, False], dtype=bool)

In [13]:

  1. np.logical_or.accumulate(a)

Out[13]:

  1. array([ True, True, True, True], dtype=bool)

reduceat 方法¶

  1. op.reduceat(a, indices)

reduceat 方法将操作符运用到指定的下标上,返回一个与 indices 大小相同的数组:

\begin{align}y & = add.reduceat(a, indices) \& = \left[\sum{n=indice[0]}^{indice[1]-1} a[n], \sum{n=indice[1]}^{indice[2]-1} a[n], …, \sum_{n=indice[-1]}^{N-1} a[n]\right]\end{align}

In [14]:

  1. a = np.array([0, 10, 20, 30, 40, 50])
  2. indices = np.array([1,4])
  3.  
  4. np.add.reduceat(a, indices)

Out[14]:

  1. array([60, 90])

这里,indices[1, 4],所以 60 表示从下标1(包括)加到下标4(不包括)的结果,90 表示从下标4(包括)加到结尾的结果。

outer 方法¶

  1. op.outer(a, b)

对于 a 中每个元素,将 op 运用到它和 b 的每一个元素上所得到的结果:

In [15]:

  1. a = np.array([0,1])
  2. b = np.array([1,2,3])
  3.  
  4. np.add.outer(a, b)

Out[15]:

  1. array([[1, 2, 3],
  2. [2, 3, 4]])

注意有顺序的区别:

In [16]:

  1. np.add.outer(b, a)

Out[16]:

  1. array([[1, 2],
  2. [2, 3],
  3. [3, 4]])

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/03-numpy/03.16-universal-functions.ipynb