numpy常用函数

numpy常用函数

Fre_soe 868 2023-04-23

Numpy函数

数学函数

算术运算

算符为 元素级。也就是说,它们只用于位置相同的元素之间,所得到的运算结果组成一个新的数组。

加:numpy.add(x1, x2, *args, **kwargs)

Add arguments element-wise.

减:numpy.subtract(x1, x2, *args, **kwargs)

Subtract arguments element-wise

乘:numpy.multiply(x1, x2, *args, **kwargs)

Multiply arguments element-wise.

除:numpy.divide(x1, x2, *args, **kwargs)

Returns a true division of the inputs, element-wise.

整除:numpy.floor_divide(x1, x2, *args, **kwargs)

Return the largest integer smaller or equal to the division of the inputs.

幂:numpy.power(x1, x2, *args, **kwargs)

First array elements raised to powers from second array, element-wise.

开方:numpy.sqrt(x, *args, **kwargs)

Return the non-negative square-root of an array, element-wise.

平方:numpy.square(x, *args, **kwargs)

Return the element-wise square of the input.

三角函数

numpy.sin(x, *args, **kwargs)

Trigonometric sine, element-wise.

numpy.cos(x, *args, **kwargs)

Cosine element-wise.

numpy.tan(x, *args, **kwargs)

Compute tangent element-wise.

numpy.arcsin(x, *args, **kwargs)

Inverse sine, element-wise.

numpy.arccos(x, *args, **kwargs)

Trigonometric inverse cosine, element-wise.

numpy.arctan(x, *args, **kwargs)

Trigonometric inverse tangent, element-wise.

指数、对数函数

numpy.exp(x, *args, **kwargs)

Calculate the exponential of all elements in the input array.

numpy.log(x, *args, **kwargs)

Natural logarithm, element-wise.

numpy.exp2(x, *args, **kwargs)

Calculate 2**p for all p in the input array.

numpy.log2(x, *args, **kwargs)

Base-2 logarithm of x.

numpy.log10(x, *args, **kwargs)

Return the base 10 logarithm of the input array, element-wise.

维度加、累加、累乘

维度加:numpy.sum(a[, axis=None, dtype=None, out=None, …])—— 返回给定轴上的数组元素的总和

Sum of array elements over a given axis.
返回给定轴上的数组元素的总和。
通过不同的 axis,numpy 会沿着不同的方向进行操作:如果不设置,那么对所有的元素操作;如果axis=0,则沿着纵轴进行操作;axis=1,则沿着横轴进行操作;axis=i,则 numpy 沿着第i个下标变化的方向进行操作。累加:numpy.cumsum(a, axis=None, dtype=None, out=None) ——返回给定轴上的数组元素的累加和。

Return the cumulative sum of the elements along a given axis.
聚合函数 是指对一组值(比如一个数组)进行操作,返回一个单一值作为结果的函数。因而,求数组所有元素之和的函数就是聚合函数。ndarray类实现了多个这样的函数。维度乘:numpy.prod(a[, axis=None, dtype=None, out=None, …])—— 返回给定轴上数组元素的乘积

Return the product of array elements over a given axis.
参数用法与numpy.sum()相同
示例:

x = np.array([[11, 12, 13, 14, 15],               [16, 17, 18, 19, 20],               [21, 22, 23, 24, 25],               [26, 27, 28, 29, 30],               [31, 32, 33, 34, 35]]) y = np.prod(x) print(y)  # 788529152  y = np.prod(x, axis=0) print(y) # [2978976 3877632 4972968 6294624 7875000]  y = np.prod(x, axis=1) print(y) # [  360360  1860480  6375600 17100720 38955840] 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

累乘:numpy.cumprod(a, axis=None, dtype=None, out=None) ——返回给定轴上数组元素的累乘

Return the cumulative product of elements along a given axis.临差:numpy.diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue) — 沿着指定轴计算第N维的离散差值

The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively.
Calculate the n-th discrete difference along the given axis

  • a:输入矩阵
  • n:可选,代表要执行几次差值
  • axis:默认是最后一个四舍五入

    numpy.around(a, decimals=0, out=None) —— 将数组舍入到给定的小数位数

    Evenly round to the given number of decimals

    x = np.random.rand(3, 3) * 10 print(x) # [[6.59144457 3.78566113 8.15321227] #  [1.68241475 3.78753332 7.68886328] #  [2.84255822 9.58106727 7.86678037]]  y = np.around(x) print(y) # [[ 7.  4.  8.] #  [ 2.  4.  8.] #  [ 3. 10.  8.]]  y = np.around(x, decimals=2) print(y) # [[6.59 3.79 8.15] #  [1.68 3.79 7.69] #  [2.84 9.58 7.87]] 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    向上\下取整

    numpy.ceil(x, *args, **kwargs)——向上取整

    Return the ceiling of the input, element-wise.

    x = np.random.rand(3, 3) * 10 print(x) # [[0.67847795 1.33073923 4.53920122] #  [7.55724676 5.88854047 2.65502046] #  [8.67640444 8.80110812 5.97528726]]  y = np.ceil(x) print(y) # [[1. 2. 5.] #  [8. 6. 3.] #  [9. 9. 6.]]  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    numpy.floor(x, *args, **kwargs) ——向下取整

    Return the floor of the input, element-wise

    y = np.floor(x) print(y) # [[0. 1. 4.] #  [7. 5. 2.] #  [8. 8. 5.]]  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    其它

    numpy.clip(a, a_min, a_max, out=None, **kwargs) —— 限制值范围

    Clip (limit) the values in an array

    x = np.array([[11, 12, 13, 14, 15],               [16, 17, 18, 19, 20],               [21, 22, 23, 24, 25],               [26, 27, 28, 29, 30],               [31, 32, 33, 34, 35]]) y = np.clip(x, a_min=20, a_max=30) print(y) # [[20 20 20 20 20] #  [20 20 20 20 20] #  [21 22 23 24 25] #  [26 27 28 29 30] #  [30 30 30 30 30]] 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    numpy.absolute(x, *args, **kwargs) /numpy.abs(x, *args, **kwargs) —— 绝对值

    abs是对absolute的简写形势

    x = np.arange(-5, 5) print(x) # [-5 -4 -3 -2 -1  0  1  2  3  4]  y = np.abs(x) print(y) # [5 4 3 2 1 0 1 2 3 4]  y = np.absolute(x) print(y) # [5 4 3 2 1 0 1 2 3 4] 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    numpy.sign(x, *args, **kwargs) ——正负性返回

    Returns an element-wise indication of the sign of a number.

    x = np.arange(-5, 5) print(x) #[-5 -4 -3 -2 -1  0  1  2  3  4] print(np.sign(x)) #[-1 -1 -1 -1 -1  0  1  1  1  1] 
    • 1
    • 2
    • 3
    • 4
    • 5

    逻辑函数

    真值判断numpy.all(任意真则真)、numpy.any(存在真则真)

    • numpy.all(a, axis=None, out=None, keepdims=np._NoValue) Test whether all array elements along a given axis evaluate to True.
    • numpy.any(a, axis=None, out=None, keepdims=np._NoValue) Test whether any array element along a given axis evaluates to True.
    a = np.array([0, 4, 5]) b = np.copy(a) print(np.all(a == b))  # True print(np.any(a == b))  # True  b[0] = 1 print(np.all(a == b))  # False print(np.any(a == b))  # True  print(np.all([1.0, np.nan]))  # True print(np.any([1.0, np.nan]))  # True  a = np.eye(3) print(np.all(a, axis=0))  # [False False False] print(np.any(a, axis=0))  # [ True  True  True] 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    逻辑运算

    与、或、非、异或

    numpy.logical_and(x1, x2, *args, **kwargs)

    Compute the truth value of x1 AND x2 element-wise.

    numpy.logical_or(x1, x2, *args, **kwargs)

    Compute the truth value of x1 OR x2 element-wise.

    numpy.logical_not(x, *args, **kwargs)

    Compute the truth value of NOT x element-wise.

    numpy.logical_xor(x1, x2, *args, **kwargs)

    Compute the truth value of x1 XOR x2, element-wise.

    print(np.logical_not(3))   # False print(np.logical_not([True, False, 0, 1])) # [False  True  True False]  x = np.arange(5) print(np.logical_not(x < 3)) # [False False False  True  True]  【例】计算x1 AND x2元素的真值。  print(np.logical_and(True, False))   # False print(np.logical_and([True, False], [True, False])) # [ True False] print(np.logical_and(x > 1, x < 4)) # [False False  True  True False]  【例】逐元素计算x1 OR x2的真值。   print(np.logical_or(True, False)) # True print(np.logical_or([True, False], [False, False])) # [ True False] print(np.logical_or(x < 1, x > 3)) # [ True False False False  True]  【例】计算x1 XOR x2的真值,按元素计算。  print(np.logical_xor(True, False)) # True print(np.logical_xor([True, True, False, False], [True, False, True, False])) # [False  True  True False] print(np.logical_xor(x < 1, x > 3)) # [ True False False False  True] print(np.logical_xor(0, np.eye(2))) # [[ True False] #  [False  True]] 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    比较(大于、小于、等于、不大于、不小于)

    numpy.greater(x1, x2, *args, **kwargs)

    Return the truth value of (x1 > x2) element-wise.

    numpy.greater_equal(x1, x2, *args, **kwargs)

    Return the truth value of (x1 >= x2) element-wise.

    numpy.equal(x1, x2, *args, **kwargs)

    Return (x1 == x2) element-wise.

    numpy.not_equal(x1, x2, *args, **kwargs)

    Return (x1 != x2) element-wise.

    numpy.less(x1, x2, *args, **kwargs)

    Return the truth value of (x1 < x2) element-wise.

    numpy.less_equal(x1, x2, *args, **kwargs)

    Return the truth value of (x1 =< x2) element-wise.

    x = np.array([1, 2, 3, 4, 5, 6, 7, 8])  y = x > 2 print(y) print(np.greater(x, 2)) # [False False  True  True  True  True  True  True]  y = x >= 2 print(y) print(np.greater_equal(x, 2)) # [False  True  True  True  True  True  True  True]  y = x == 2 print(y) print(np.equal(x, 2)) # [False  True False False False False False False]  y = x != 2 print(y) print(np.not_equal(x, 2)) # [ True False  True  True  True  True  True  True]  y = x < 2 print(y) print(np.less(x, 2)) # [ True False False False False False False False]  y = x <= 2 print(y) print(np.less_equal(x, 2)) # [ True  True False False False False False False] 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    比较时可以存在广播

    x = np.array([[11, 12, 13, 14, 15],               [16, 17, 18, 19, 20],               [21, 22, 23, 24, 25],               [26, 27, 28, 29, 30],               [31, 32, 33, 34, 35]])  np.random.seed(20200611) y = np.random.randint(10, 50, 5)  print(y) # [32 37 30 24 10]  z = x > y print(z) print(np.greater(x, y)) # [[False False False False  True] #  [False False False False  True] #  [False False False False  True] #  [False False False  True  True] #  [False False  True  True  True]]  z = x >= y print(z) print(np.greater_equal(x, y)) # [[False False False False  True] #  [False False False False  True] #  [False False False  True  True] #  [False False False  True  True] #  [False False  True  True  True]]  z = x == y print(z) print(np.equal(x, y)) # [[False False False False False] #  [False False False False False] #  [False False False  True False] #  [False False False False False] #  [False False False False False]]  z = x != y print(z) print(np.not_equal(x, y)) # [[ True  True  True  True  True] #  [ True  True  True  True  True] #  [ True  True  True False  True] #  [ True  True  True  True  True] #  [ True  True  True  True  True]]  z = x < y print(z) print(np.less(x, y)) # [[ True  True  True  True False] #  [ True  True  True  True False] #  [ True  True  True False False] #  [ True  True  True False False] #  [ True  True False False False]]  z = x <= y print(z) print(np.less_equal(x, y)) # [[ True  True  True  True False] #  [ True  True  True  True False] #  [ True  True  True  True False] #  [ True  True  True False False] #  [ True  True False False False]] 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65