一般函数
In [1]:
- import numpy as np
三角函数
sin(x)
cos(x)
tan(x)
sinh(x)
conh(x)
tanh(x)
arccos(x)
arctan(x)
arcsin(x)
arccosh(x)
arctanh(x)
arcsinh(x)
arctan2(x,y)
arctan2(x,y)
返回 arctan(x/y)
。
向量操作
dot(x,y)
inner(x,y)
cross(x,y)
vdot(x,y)
outer(x,y)
kron(x,y)
tensordot(x,y[,axis])
其他操作
exp(x)
log(x)
log10(x)
sqrt(x)
absolute(x)
conjugate(x)
negative(x)
ceil(x)
floor(x)
fabs(x)
hypot(x)
fmod(x)
maximum(x,y)
minimum(x,y)
hypot
返回对应点 (x,y)
到原点的距离。
In [2]:
- x = np.array([1,2,3])
- y = np.array([4,5,6])
- np.hypot(x,y)
Out[2]:
- array([ 4.12310563, 5.38516481, 6.70820393])
类型处理
iscomplexobj
iscomplex
isrealobj
isreal
imag
real
real_if_close
isscalar
isneginf
isposinf
isinf
isfinite
isnan
nan_to_num
common_type
typename
正无穷:
In [3]:
- np.inf
Out[3]:
- inf
负无穷:
In [4]:
- -np.inf
Out[4]:
- -inf
非法值(Not a number):
In [5]:
- np.nan
Out[5]:
- nan
检查是否为无穷:
In [6]:
- np.isinf(1.0)
Out[6]:
- False
In [7]:
- np.isinf(np.inf)
Out[7]:
- True
In [8]:
- np.isinf(-np.inf)
Out[8]:
- True
非法值:
In [9]:
- np.array([0]) / 0.0
- c:\Miniconda\lib\site-packages\IPython\kernel\__main__.py:1: RuntimeWarning: invalid value encountered in divide
- if __name__ == '__main__':
Out[9]:
- array([ nan])
这并不会报错,而是返回一个非法值。
只有 0/0
会得到 nan
,非0值除以0会得到无穷:
In [10]:
- a = np.arange(5.0)
- b = a / 0.0
- b
- c:\Miniconda\lib\site-packages\IPython\kernel\__main__.py:2: RuntimeWarning: divide by zero encountered in divide
- from IPython.kernel.zmq import kernelapp as app
- c:\Miniconda\lib\site-packages\IPython\kernel\__main__.py:2: RuntimeWarning: invalid value encountered in divide
- from IPython.kernel.zmq import kernelapp as app
Out[10]:
- array([ nan, inf, inf, inf, inf])
nan
与任何数进行比较都是 False
:
In [11]:
- b == np.nan
Out[11]:
- array([False, False, False, False, False], dtype=bool)
想要找出 nan
值需要使用 isnan
:
In [12]:
- np.isnan(b)
Out[12]:
- array([ True, False, False, False, False], dtype=bool)
修改形状
atleast_1d
atleast_2d
atleast_3d
expand_dims
apply_over_axes
apply_along_axis
hstack
vstack
dstack
column_stack
hsplit
vsplit
dsplit
split
squeeze
其他有用函数
fix
mod
amax
amin
ptp
sum
cumsum
prod
cumprod
diff
angle
unwrap
sort_complex
trim_zeros
fliplr
flipud
rot90
diag
eye
select
extract
insert
roots
poly
any
all
disp
unique
nansum
nanmax
nanargmax
nanargmin
nanmin
nan
开头的函数会进行相应的操作,但是忽略 nan
值。