一元函数:对数组各个元素进行操作运算
函数名 | 描述 |
---|---|
np.abs(x) / np.fabs(x) | 求个元素的绝对值 |
np.sqrt(x) | 求平方根 |
np.square(x) | 求平方 |
np.log(x) | 求自然对数 |
np.ceil(x) / np.floor(x) | 求其ceil值与floor值 |
np.rint(x) | 四舍五入 |
np.modf(x) | 将小数与整数分开以俩个数组进行保存 |
np.cos(x)等系列 | 求cos值等 |
np.exp(x) | 求指数值 |
np.sign(x) | 返回符号值 -1 ,0,1. |
部分方法的使用:
>>> import numpy as np
>>> a=np.arange(24).reshape((2,3,4))
>>> a
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]]])
>>> np.square(a)
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)
>>> np.sqrt(a)
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]]])
>>> np.modf(a)
(array([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]]), 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.]]]))
二元函数:俩个数组各元素进行对应运算
函数名 | 描述 |
---|---|
np.maximun(a,b) | 元素级最大值计算 |
np.minimun(a,b) | 元素级最小值计算 |
np.mod(a,b) | 元素级的模运算 |
np.copysign(a,b) | 将数组b中个元素值的符号赋值给a中对应元素 |
还有最简单的== 、 != 、>=等
算术比较,返回值只有布尔值的ndarray数组
部分方法的使用:
>>> b=np.sqrt(a)
>>> b
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]]])
>>> np.maximum(a,b)
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.]]])
>>> a>b
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]]])