一般函数

In [1]:

  1. import numpy as np

三角函数

  1. sin(x)
  2. cos(x)
  3. tan(x)
  4. sinh(x)
  5. conh(x)
  6. tanh(x)
  7. arccos(x)
  8. arctan(x)
  9. arcsin(x)
  10. arccosh(x)
  11. arctanh(x)
  12. arcsinh(x)
  13. arctan2(x,y)

arctan2(x,y) 返回 arctan(x/y)

向量操作

  1. dot(x,y)
  2. inner(x,y)
  3. cross(x,y)
  4. vdot(x,y)
  5. outer(x,y)
  6. kron(x,y)
  7. tensordot(x,y[,axis])

其他操作

  1. exp(x)
  2. log(x)
  3. log10(x)
  4. sqrt(x)
  5. absolute(x)
  6. conjugate(x)
  7. negative(x)
  8. ceil(x)
  9. floor(x)
  10. fabs(x)
  11. hypot(x)
  12. fmod(x)
  13. maximum(x,y)
  14. minimum(x,y)

hypot 返回对应点 (x,y) 到原点的距离。

In [2]:

  1. x = np.array([1,2,3])
  2. y = np.array([4,5,6])
  3. np.hypot(x,y)

Out[2]:

  1. array([ 4.12310563, 5.38516481, 6.70820393])

类型处理

  1. iscomplexobj
  2. iscomplex
  3. isrealobj
  4. isreal
  5. imag
  6. real
  7. real_if_close
  8. isscalar
  9. isneginf
  10. isposinf
  11. isinf
  12. isfinite
  13. isnan
  14. nan_to_num
  15. common_type
  16. typename

正无穷:

In [3]:

  1. np.inf

Out[3]:

  1. inf

负无穷:

In [4]:

  1. -np.inf

Out[4]:

  1. -inf

非法值(Not a number):

In [5]:

  1. np.nan

Out[5]:

  1. nan

检查是否为无穷:

In [6]:

  1. np.isinf(1.0)

Out[6]:

  1. False

In [7]:

  1. np.isinf(np.inf)

Out[7]:

  1. True

In [8]:

  1. np.isinf(-np.inf)

Out[8]:

  1. True

非法值:

In [9]:

  1. np.array([0]) / 0.0
  1. c:\Miniconda\lib\site-packages\IPython\kernel\__main__.py:1: RuntimeWarning: invalid value encountered in divide
  2. if __name__ == '__main__':

Out[9]:

  1. array([ nan])

这并不会报错,而是返回一个非法值。

只有 0/0 会得到 nan,非0值除以0会得到无穷:

In [10]:

  1. a = np.arange(5.0)
  2. b = a / 0.0
  3. b
  1. c:\Miniconda\lib\site-packages\IPython\kernel\__main__.py:2: RuntimeWarning: divide by zero encountered in divide
  2. from IPython.kernel.zmq import kernelapp as app
  3. c:\Miniconda\lib\site-packages\IPython\kernel\__main__.py:2: RuntimeWarning: invalid value encountered in divide
  4. from IPython.kernel.zmq import kernelapp as app

Out[10]:

  1. array([ nan, inf, inf, inf, inf])

nan 与任何数进行比较都是 False

In [11]:

  1. b == np.nan

Out[11]:

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

想要找出 nan 值需要使用 isnan

In [12]:

  1. np.isnan(b)

Out[12]:

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

修改形状

  1. atleast_1d
  2. atleast_2d
  3. atleast_3d
  4. expand_dims
  5. apply_over_axes
  6. apply_along_axis
  7. hstack
  8. vstack
  9. dstack
  10. column_stack
  11. hsplit
  12. vsplit
  13. dsplit
  14. split
  15. squeeze

其他有用函数

  1. fix
  2. mod
  3. amax
  4. amin
  5. ptp
  6. sum
  7. cumsum
  8. prod
  9. cumprod
  10. diff
  11. angle
  12. unwrap
  13. sort_complex
  14. trim_zeros
  15. fliplr
  16. flipud
  17. rot90
  18. diag
  19. eye
  20. select
  21. extract
  22. insert
  23. roots
  24. poly
  25. any
  26. all
  27. disp
  28. unique
  29. nansum
  30. nanmax
  31. nanargmax
  32. nanargmin
  33. nanmin

nan 开头的函数会进行相应的操作,但是忽略 nan 值。

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