参考链接: NumPy官网
参考链接: NumPy: the absolute basics for beginners
参考链接: Quickstart tutorial
参考链接: Broadcasting广播
参考链接: NumPy 中文教程
参考链接: Python数据分析与展示
数组与标量之间的运算作用于数组的每一个元素实验如下:
>>> >>> a = np.arange(24).reshape((2,3,4))>>> aarray([[[ 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.mean()11.5>>> a = a / a.mean()>>> aarray([[[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一元函数
函数 | 说明 |
---|---|
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(‐) |
对ndarray中的数据执行元素级运算的函数,实验如下:
>>> >>> >>> a = np.arange(24).reshape((2,3,4))>>> aarray([[[ 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)>>> aarray([[[ 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 = np.aqrt(a)Traceback (most recent call last): File "<pyshell#65>", line 1, in <module> a = np.aqrt(a) File "D:\Python\Python37\lib\site-packages\numpy\__init__.py", line 220, in __getattr__ "{!r}".format(__name__, attr))AttributeError: module 'numpy' has no attribute 'aqrt'>>> a = np.sqrt(a)>>> aarray([[[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.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.]]]))>>> x,y = np.modf(a)>>> xarray([[[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]]])>>> yarray([[[0., 1., 1., 1.], [2., 2., 2., 2.], [2., 3., 3., 3.]], [[3., 3., 3., 3.], [4., 4., 4., 4.], [4., 4., 4., 4.]]])>>> aarray([[[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.rint(a)array([[[0., 1., 1., 2.], [2., 2., 2., 3.], [3., 3., 3., 3.]], [[3., 4., 4., 4.], [4., 4., 4., 4.], [4., 5., 5., 5.]]])>>> np.cos(a)array([[[ 1. , 0.54030231, 0.15594369, -0.16055654], [-0.41614684, -0.61727288, -0.76990573, -0.87956873], [-0.95136313, -0.9899925 , -0.99978607, -0.98472094]], [[-0.9484432 , -0.89428806, -0.82529906, -0.74424627], [-0.65364362, -0.55576539, -0.45266186, -0.34617416], [-0.23794839, -0.129449 , -0.02197145, 0.08334575]]])>>> np.cosh(a)array([[[ 1. , 1.54308063, 2.17818356, 2.91457744], [ 3.76219569, 4.73167347, 5.83438641, 7.08249107], [ 8.48896721, 10.067662 , 11.83333607, 13.80171176]], [[15.98952331, 18.41456937, 21.09576772, 24.05321231], [27.30823284, 30.88345677, 34.80287403, 39.09190439], [43.77746767, 48.8880569 , 54.45381443, 60.50661126]]])>>> np.exp(a)array([[[ 1. , 2.71828183, 4.11325038, 5.65223367], [ 7.3890561 , 9.35646902, 11.58243519, 14.09403011], [ 16.91882868, 20.08553692, 23.62434292, 27.56714845]], [[ 31.94774551, 36.80196629, 42.16782067, 48.08562838], [ 54.59815003, 61.7507194 , 69.59137847, 78.17101632], [ 87.54351246, 97.76588528, 108.898446 , 121.00495841]]])>>> aarray([[[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.sign(a)array([[[0., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]], [[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]])>>> a[-1,-1,-1] = -33>>> aarray([[[ 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, -33. ]]])>>> np.sign(a)array([[[ 0., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]], [[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., -1.]]])>>>
NumPy二元函数
函数 | 说明 |
---|---|
+ ‐ * / ** | 两个数组各元素进行对应运算 |
np.maximum(x,y)、np.fmax()、np.minimum(x,y)、np.fmin() | 元素级的最大值/最小值计算 |
np.mod(x,y) | 元素级的模运算 |
np.copysign(x,y) | 将数组y中各元素值的符号赋值给数组x对应元素 |
> < >= <= == != | 算术比较,产生布尔型数组 |
实验如下:
>>> >>> a = np.arange(24).reshape((2,3,4))>>> b = np.sqrt(a)>>> aarray([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])>>> barray([[[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 > barray([[[False, False, True, True], [ True, True, True, True], [ True, True, True, True]], [[ True, True, True, True], [ True, True, True, True], [ True, True, True, True]]])>>> >>> >>>