数组与标量之间的运算
数组与标量之间的运算作用于数组的每一个元素
In [62]: a=np.arange(24).reshape((2,3,4))
In [63]: a
Out[63]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
In [64]: a.mean()
Out[64]: 11.5
In [65]: a=a/a.mean() #计算a与元素平均值的商
In [66]: a
Out[66]:
array([[[ 0. , 0.08695652, 0.17391304, 0.26086957],
[ 0.34782609, 0.43478261, 0.52173913, 0.60869565],
[ 0.69565217, 0.7826087 , 0.86956522, 0.95652174]],
[[ 1.04347826, 1.13043478, 1.2173913 , 1.30434783],
[ 1.39130435, 1.47826087, 1.56521739, 1.65217391],
[ 1.73913043, 1.82608696, 1.91304348, 2. ]]])
NumPy一元函数
对ndarray中的数据执行元素级运算的函数
函数 | 说明 |
---|---|
np.abs(x) np.fabs(x) | 计算数组各元素的绝对值 |
np.sqrt(x) | 计算数组各元素的平方根 |
np.square(x) | 计算数组各元素的平方 |
np.log(x) np.log10(x) np.log2(x) | 计算数组各元素的自然对数、10底对数和2底对数 |
np.ceil(x) np.floor(x) | 计算数组各元素的ceiling值或floor值 |
np.rint(x) | 计算数组各元素的四舍五入值 |
np.modf(x) | 将数组各元素的小数和整数部分以两个独立数组形式返回 |
np.cos(x) np.cosh(x) np.sin(x) np.sinh(x) np.tan(x) np.tanh(x) | 计算数组各元素的普通型和双曲型三角函数 |
np.exp(x) | 计算数组各元素的指数值 |
np.sign(x) | 计算数组各元素的符号值,1(+), 0, ‐1(‐) |
In [70]: a=np.arange(24).reshape((2,3,4))
In [71]: np.square(a)
Out[71]:
array([[[ 0, 1, 4, 9],
[ 16, 25, 36, 49],
[ 64, 81, 100, 121]],
[[144, 169, 196, 225],
[256, 289, 324, 361],
[400, 441, 484, 529]]], dtype=int32)
In [72]: a=np.sqrt(a)
In [73]: a
Out[73]:
array([[[ 0. , 1. , 1.41421356, 1.73205081],
[ 2. , 2.23606798, 2.44948974, 2.64575131],
[ 2.82842712, 3. , 3.16227766, 3.31662479]],
[[ 3.46410162, 3.60555128, 3.74165739, 3.87298335],
[ 4. , 4.12310563, 4.24264069, 4.35889894],
[ 4.47213595, 4.58257569, 4.69041576, 4.79583152]]])
In [74]: np.modf(a)
Out[74]:
(array([[[ 0. , 0. , 0.41421356, 0.73205081],
[ 0. , 0.23606798, 0.44948974, 0.64575131],
[ 0.82842712, 0. , 0.16227766, 0.31662479]],
[[ 0.46410162, 0.60555128, 0.74165739, 0.87298335],
[ 0. , 0.12310563, 0.24264069, 0.35889894],
[ 0.47213595, 0.58257569, 0.69041576, 0.79583152]]]),
array([[[ 0., 1., 1., 1.],
[ 2., 2., 2., 2.],
[ 2., 3., 3., 3.]],
[[ 3., 3., 3., 3.],
[ 4., 4., 4., 4.],
[ 4., 4., 4., 4.]]]))
NumPy二元函数
函数 | 说明 |
---|---|
+ ‐ * / ** | 两个数组各元素进行对应运算 |
np.maximum(x,y) np.fmax() np.minimum(x,y) np.fmin() | 元素级的最大值/最小值计算 |
np.mod(x,y) | 元素级的模运算 |
np.copysign(x,y) | 将数组y中各元素值的符号赋值给数组x对应元素 |
> < >= <= == != | 算术比较,产生布尔型数组 |
In [75]: a=np.arange(24).reshape((2,3,4))
In [76]: b=np.sqrt(a)
In [77]: a
Out[77]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
In [78]: b
Out[78]:
array([[[ 0. , 1. , 1.41421356, 1.73205081],
[ 2. , 2.23606798, 2.44948974, 2.64575131],
[ 2.82842712, 3. , 3.16227766, 3.31662479]],
[[ 3.46410162, 3.60555128, 3.74165739, 3.87298335],
[ 4. , 4.12310563, 4.24264069, 4.35889894],
[ 4.47213595, 4.58257569, 4.69041576, 4.79583152]]])
In [79]: np.maximum(a,b)
Out[79]:
array([[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]],
[[ 12., 13., 14., 15.],
[ 16., 17., 18., 19.],
[ 20., 21., 22., 23.]]])
In [80]: a>b
Out[80]:
array([[[False, False, True, True],
[ True, True, True, True],
[ True, True, True, True]],
[[ True, True, True, True],
[ True, True, True, True],
[ True, True, True, True]]], dtype=bool)