向量化和广播

向量化和广播这两个概念是 numpy 内部实现的基础。有了向量化,编写代码时无需使用显式循环。这些循环实际上不能省略,只不过是在内部实现,被代码中的其他结构代替。向量化的应用使得代码更简洁,可读性更强,也可以说使用了向量化方法的代码看上去更“Pythonic”。
广播(Broadcasting)机制描述了 numpy 如何在算术运算期间处理具有不同形状的数组,让较小的数组在较大的数组上“广播”,以便它们具有兼容的形状。并不是所有的维度都要彼此兼容才符合广播机制的要求,但它们必须满足一定的条件。
若两个数组的各维度兼容,也就是两个数组的每一维等长,或其中一个数组为 一维,那么广播机制就适用。如果这两个条件不满足,numpy就会抛出异常,说两个数组不兼容。
总结来说,广播的规则有三个:

  • 如果两个数组的维度数dim不相同,那么小维度数组的形状将会在左边补1。
  • 如果shape维度不匹配,但是有维度是1,那么可以扩展维度是1的维度匹配另一个数组;
  • 如果shape维度不匹配,但是没有任何一个维度是1,则匹配引发错误;

【例】二维数组加一维数组

  1. import numpy as np
  2. x = np.arange(4)
  3. y = np.ones((3, 4))
  4. print(x.shape) # (4,)
  5. print(y.shape) # (3, 4)
  6. print((x + y).shape) # (3, 4)
  7. print(x + y)
  8. # [[1. 2. 3. 4.]
  9. # [1. 2. 3. 4.]
  10. # [1. 2. 3. 4.]]

【例】两个数组均需要广播

  1. import numpy as np
  2. x = np.arange(4).reshape(4, 1)
  3. y = np.ones(5)
  4. print(x.shape) # (4, 1)
  5. print(y.shape) # (5,)
  6. print((x + y).shape) # (4, 5)
  7. print(x + y)
  8. # [[1. 1. 1. 1. 1.]
  9. # [2. 2. 2. 2. 2.]
  10. # [3. 3. 3. 3. 3.]
  11. # [4. 4. 4. 4. 4.]]
  12. x = np.array([0.0, 10.0, 20.0, 30.0])
  13. y = np.array([1.0, 2.0, 3.0])
  14. z = x[:, np.newaxis] + y
  15. print(z)
  16. # [[ 1. 2. 3.]
  17. # [11. 12. 13.]
  18. # [21. 22. 23.]
  19. # [31. 32. 33.]]

【例】不匹配报错的例子

  1. import numpy as np
  2. x = np.arange(4)
  3. y = np.ones(5)
  4. print(x.shape) # (4,)
  5. print(y.shape) # (5,)
  6. print(x + y)
  7. # ValueError: operands could not be broadcast together with shapes (4,) (5,)

数学函数

算数运算

numpy.add

  • numpy.add(x1, x2, *args, **kwargs) Add arguments element-wise.

    numpy.subtract

    numpy.subtract(x1, x2, *args, **kwargs) Subtract arguments element-wise.

    numpy.multiply

  • numpy.multiply(x1, x2, *args, **kwargs) Multiply arguments element-wise.

    numpy.divide

  • numpy.divide(x1, x2, *args, **kwargs) Returns a true division of the inputs, element-wise.

    numpy.floor_divide

  • numpy.floor_divide(x1, x2, *args, **kwargs) Return the largest integer smaller or equal to the division of the inputs.

    numpy.power

  • numpy.power(x1, x2, *args, **kwargs) First array elements raised to powers from second array, element-wise.

在 numpy 中对以上函数进行了运算符的重载,且运算符为 元素级。也就是说,它们只用于位置相同的元素之间,所得到的运算结果组成一个新的数组。

【例】注意 numpy 的广播规则。

  1. import numpy as np
  2. x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
  3. y = x + 1
  4. print(y)
  5. print(np.add(x, 1))
  6. # [2 3 4 5 6 7 8 9]
  7. y = x - 1
  8. print(y)
  9. print(np.subtract(x, 1))
  10. # [0 1 2 3 4 5 6 7]
  11. y = x * 2
  12. print(y)
  13. print(np.multiply(x, 2))
  14. # [ 2 4 6 8 10 12 14 16]
  15. y = x / 2
  16. print(y)
  17. print(np.divide(x, 2))
  18. # [0.5 1. 1.5 2. 2.5 3. 3.5 4. ]
  19. y = x // 2
  20. print(y)
  21. print(np.floor_divide(x, 2))
  22. # [0 1 1 2 2 3 3 4]
  23. y = x ** 2
  24. print(y)
  25. print(np.power(x, 2))
  26. # [ 1 4 9 16 25 36 49 64]

【例】注意 numpy 的广播规则。

  1. import numpy as np
  2. x = np.array([[11, 12, 13, 14, 15],
  3. [16, 17, 18, 19, 20],
  4. [21, 22, 23, 24, 25],
  5. [26, 27, 28, 29, 30],
  6. [31, 32, 33, 34, 35]])
  7. y = x + 1
  8. print(y)
  9. print(np.add(x, 1))
  10. # [[12 13 14 15 16]
  11. # [17 18 19 20 21]
  12. # [22 23 24 25 26]
  13. # [27 28 29 30 31]
  14. # [32 33 34 35 36]]
  15. y = x - 1
  16. print(y)
  17. print(np.subtract(x, 1))
  18. # [[10 11 12 13 14]
  19. # [15 16 17 18 19]
  20. # [20 21 22 23 24]
  21. # [25 26 27 28 29]
  22. # [30 31 32 33 34]]
  23. y = x * 2
  24. print(y)
  25. print(np.multiply(x, 2))
  26. # [[22 24 26 28 30]
  27. # [32 34 36 38 40]
  28. # [42 44 46 48 50]
  29. # [52 54 56 58 60]
  30. # [62 64 66 68 70]]
  31. y = x / 2
  32. print(y)
  33. print(np.divide(x, 2))
  34. # [[ 5.5 6. 6.5 7. 7.5]
  35. # [ 8. 8.5 9. 9.5 10. ]
  36. # [10.5 11. 11.5 12. 12.5]
  37. # [13. 13.5 14. 14.5 15. ]
  38. # [15.5 16. 16.5 17. 17.5]]
  39. y = x // 2
  40. print(y)
  41. print(np.floor_divide(x, 2))
  42. # [[ 5 6 6 7 7]
  43. # [ 8 8 9 9 10]
  44. # [10 11 11 12 12]
  45. # [13 13 14 14 15]
  46. # [15 16 16 17 17]]
  47. y = x ** 2
  48. print(y)
  49. print(np.power(x, 2))
  50. # [[ 121 144 169 196 225]
  51. # [ 256 289 324 361 400]
  52. # [ 441 484 529 576 625]
  53. # [ 676 729 784 841 900]
  54. # [ 961 1024 1089 1156 1225]]

【例】注意 numpy 的广播规则。

  1. import numpy as np
  2. x = np.array([[11, 12, 13, 14, 15],
  3. [16, 17, 18, 19, 20],
  4. [21, 22, 23, 24, 25],
  5. [26, 27, 28, 29, 30],
  6. [31, 32, 33, 34, 35]])
  7. y = np.arange(1, 6)
  8. print(y)
  9. # [1 2 3 4 5]
  10. z = x + y
  11. print(z)
  12. print(np.add(x, y))
  13. # [[12 14 16 18 20]
  14. # [17 19 21 23 25]
  15. # [22 24 26 28 30]
  16. # [27 29 31 33 35]
  17. # [32 34 36 38 40]]
  18. z = x - y
  19. print(z)
  20. print(np.subtract(x, y))
  21. # [[10 10 10 10 10]
  22. # [15 15 15 15 15]
  23. # [20 20 20 20 20]
  24. # [25 25 25 25 25]
  25. # [30 30 30 30 30]]
  26. z = x * y
  27. print(z)
  28. print(np.multiply(x, y))
  29. # [[ 11 24 39 56 75]
  30. # [ 16 34 54 76 100]
  31. # [ 21 44 69 96 125]
  32. # [ 26 54 84 116 150]
  33. # [ 31 64 99 136 175]]
  34. z = x / y
  35. print(z)
  36. print(np.divide(x, y))
  37. # [[11. 6. 4.33333333 3.5 3. ]
  38. # [16. 8.5 6. 4.75 4. ]
  39. # [21. 11. 7.66666667 6. 5. ]
  40. # [26. 13.5 9.33333333 7.25 6. ]
  41. # [31. 16. 11. 8.5 7. ]]
  42. z = x // y
  43. print(z)
  44. print(np.floor_divide(x, y))
  45. # [[11 6 4 3 3]
  46. # [16 8 6 4 4]
  47. # [21 11 7 6 5]
  48. # [26 13 9 7 6]
  49. # [31 16 11 8 7]]
  50. z = x ** np.full([1, 5], 2)
  51. print(z)
  52. print(np.power(x, np.full([5, 5], 2)))
  53. # [[ 121 144 169 196 225]
  54. # [ 256 289 324 361 400]
  55. # [ 441 484 529 576 625]
  56. # [ 676 729 784 841 900]
  57. # [ 961 1024 1089 1156 1225]]

【例】

  1. import numpy as np
  2. x = np.array([[11, 12, 13, 14, 15],
  3. [16, 17, 18, 19, 20],
  4. [21, 22, 23, 24, 25],
  5. [26, 27, 28, 29, 30],
  6. [31, 32, 33, 34, 35]])
  7. y = np.arange(1, 26).reshape([5, 5])
  8. print(y)
  9. # [[ 1 2 3 4 5]
  10. # [ 6 7 8 9 10]
  11. # [11 12 13 14 15]
  12. # [16 17 18 19 20]
  13. # [21 22 23 24 25]]
  14. z = x + y
  15. print(z)
  16. print(np.add(x, y))
  17. # [[12 14 16 18 20]
  18. # [22 24 26 28 30]
  19. # [32 34 36 38 40]
  20. # [42 44 46 48 50]
  21. # [52 54 56 58 60]]
  22. z = x - y
  23. print(z)
  24. print(np.subtract(x, y))
  25. # [[10 10 10 10 10]
  26. # [10 10 10 10 10]
  27. # [10 10 10 10 10]
  28. # [10 10 10 10 10]
  29. # [10 10 10 10 10]]
  30. z = x * y
  31. print(z)
  32. print(np.multiply(x, y))
  33. # [[ 11 24 39 56 75]
  34. # [ 96 119 144 171 200]
  35. # [231 264 299 336 375]
  36. # [416 459 504 551 600]
  37. # [651 704 759 816 875]]
  38. z = x / y
  39. print(z)
  40. print(np.divide(x, y))
  41. # [[11. 6. 4.33333333 3.5 3. ]
  42. # [ 2.66666667 2.42857143 2.25 2.11111111 2. ]
  43. # [ 1.90909091 1.83333333 1.76923077 1.71428571 1.66666667]
  44. # [ 1.625 1.58823529 1.55555556 1.52631579 1.5 ]
  45. # [ 1.47619048 1.45454545 1.43478261 1.41666667 1.4 ]]
  46. z = x // y
  47. print(z)
  48. print(np.floor_divide(x, y))
  49. # [[11 6 4 3 3]
  50. # [ 2 2 2 2 2]
  51. # [ 1 1 1 1 1]
  52. # [ 1 1 1 1 1]
  53. # [ 1 1 1 1 1]]
  54. z = x ** np.full([5, 5], 2)
  55. print(z)
  56. print(np.power(x, np.full([5, 5], 2)))
  57. # [[ 121 144 169 196 225]
  58. # [ 256 289 324 361 400]
  59. # [ 441 484 529 576 625]
  60. # [ 676 729 784 841 900]
  61. # [ 961 1024 1089 1156 1225]]

numpy.sqrt

  • numpy.sqrt(x, *args, **kwargs) Return the non-negative square-root of an array, element-wise.

    numpy.square

  • numpy.square(x, *args, **kwargs) Return the element-wise square of the input.

【例】

  1. import numpy as np
  2. x = np.arange(1, 5)
  3. print(x) # [1 2 3 4]
  4. y = np.sqrt(x)
  5. print(y)
  6. # [1. 1.41421356 1.73205081 2. ]
  7. print(np.power(x, 0.5))
  8. # [1. 1.41421356 1.73205081 2. ]
  9. y = np.square(x)
  10. print(y)
  11. # [ 1 4 9 16]
  12. print(np.power(x, 2))
  13. # [ 1 4 9 16]

三角函数

numpy.sin

  • numpy.sin(x, *args, **kwargs) Trigonometric sine, element-wise.

    numpy.cos

  • numpy.cos(x, *args, **kwargs) Cosine element-wise.

    numpy.tan

  • numpy.tan(x, *args, **kwargs) Compute tangent element-wise.

    numpy.arcsin

  • numpy.arcsin(x, *args, **kwargs) Inverse sine, element-wise.

    numpy.arccos

  • numpy.arccos(x, *args, **kwargs) Trigonometric inverse cosine, element-wise.

    numpy.arctan

  • numpy.arctan(x, *args, **kwargs) Trigonometric inverse tangent, element-wise.

通用函数(universal function)通常叫作ufunc,它对数组中的各个元素逐一进行操作。这表明,通用函数分别处理输入数组的每个元素,生成的结果组成一个新的输出数组。输出数组的大小跟输入数组相同。
三角函数等很多数学运算符合通用函数的定义,例如,计算平方根的sqrt()函数、用来取对数的log()函数和求正弦值的sin()函数。
【例】

  1. import numpy as np
  2. x = np.linspace(start=0, stop=np.pi / 2, num=10)
  3. print(x)
  4. # [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
  5. # 1.04719755 1.22173048 1.3962634 1.57079633]
  6. y = np.sin(x)
  7. print(y)
  8. # [0. 0.17364818 0.34202014 0.5 0.64278761 0.76604444
  9. # 0.8660254 0.93969262 0.98480775 1. ]
  10. z = np.arcsin(y)
  11. print(z)
  12. # [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
  13. # 1.04719755 1.22173048 1.3962634 1.57079633]
  14. y = np.cos(x)
  15. print(y)
  16. # [1.00000000e+00 9.84807753e-01 9.39692621e-01 8.66025404e-01
  17. # 7.66044443e-01 6.42787610e-01 5.00000000e-01 3.42020143e-01
  18. # 1.73648178e-01 6.12323400e-17]
  19. z = np.arccos(y)
  20. print(z)
  21. # [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
  22. # 1.04719755 1.22173048 1.3962634 1.57079633]
  23. y = np.tan(x)
  24. print(y)
  25. # [0.00000000e+00 1.76326981e-01 3.63970234e-01 5.77350269e-01
  26. # 8.39099631e-01 1.19175359e+00 1.73205081e+00 2.74747742e+00
  27. # 5.67128182e+00 1.63312394e+16]
  28. z = np.arctan(y)
  29. print(z)
  30. # [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
  31. # 1.04719755 1.22173048 1.3962634 1.57079633]

指数和对数

numpy.exp

  • numpy.exp(x, *args, **kwargs) Calculate the exponential of all elements in the input array.

    numpy.log

  • numpy.log(x, *args, **kwargs) Natural logarithm, element-wise.

    numpy.exp2

  • numpy.exp2(x, *args, **kwargs) Calculate 2**p for all p in the input array.

    numpy.log2

  • numpy.log2(x, *args, **kwargs) Base-2 logarithm of x.

    numpy.log10

  • numpy.log10(x, *args, **kwargs) Return the base 10 logarithm of the input array, element-wise.

【例】The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x. The natural logarithm is logarithm in base e.

  1. import numpy as np
  2. x = np.arange(1, 5)
  3. print(x)
  4. # [1 2 3 4]
  5. y = np.exp(x)
  6. print(y)
  7. # [ 2.71828183 7.3890561 20.08553692 54.59815003]
  8. z = np.log(y)
  9. print(z)
  10. # [1. 2. 3. 4.]

加法函数、乘法函数

numpy.sum

  • 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

  • numpy.cumsum(a, axis=None, dtype=None, out=None) Return the cumulative sum of the elements along a given axis.

聚合函数 是指对一组值(比如一个数组)进行操作,返回一个单一值作为结果的函数。因而,求数组所有元素之和的函数就是聚合函数。ndarray类实现了多个这样的函数。
【例】返回给定轴上的数组元素的总和。

  1. import numpy as np
  2. x = np.array([[11, 12, 13, 14, 15],
  3. [16, 17, 18, 19, 20],
  4. [21, 22, 23, 24, 25],
  5. [26, 27, 28, 29, 30],
  6. [31, 32, 33, 34, 35]])
  7. y = np.sum(x)
  8. print(y) # 575
  9. y = np.sum(x, axis=0)
  10. print(y) # [105 110 115 120 125]
  11. y = np.sum(x, axis=1)
  12. print(y) # [ 65 90 115 140 165]

【例】返回给定轴上的数组元素的累加和。

  1. import numpy as np
  2. x = np.array([[11, 12, 13, 14, 15],
  3. [16, 17, 18, 19, 20],
  4. [21, 22, 23, 24, 25],
  5. [26, 27, 28, 29, 30],
  6. [31, 32, 33, 34, 35]])
  7. y = np.cumsum(x)
  8. print(y)
  9. # [ 11 23 36 50 65 81 98 116 135 155 176 198 221 245 270 296 323 351
  10. # 380 410 441 473 506 540 575]
  11. y = np.cumsum(x, axis=0)
  12. print(y)
  13. # [[ 11 12 13 14 15]
  14. # [ 27 29 31 33 35]
  15. # [ 48 51 54 57 60]
  16. # [ 74 78 82 86 90]
  17. # [105 110 115 120 125]]
  18. y = np.cumsum(x, axis=1)
  19. print(y)
  20. # [[ 11 23 36 50 65]
  21. # [ 16 33 51 70 90]
  22. # [ 21 43 66 90 115]
  23. # [ 26 53 81 110 140]
  24. # [ 31 63 96 130 165]]

numpy.prod 乘积

  • numpy.prod(a[, axis=None, dtype=None, out=None, …]) Return the product of array elements over a given axis.

    numpy.cumprod 累乘

  • numpy.cumprod(a, axis=None, dtype=None, out=None) Return the cumulative product of elements along a given axis.

【例】返回给定轴上数组元素的乘积。

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

【例】返回给定轴上数组元素的累乘。

  1. import numpy as np
  2. x = np.array([[11, 12, 13, 14, 15],
  3. [16, 17, 18, 19, 20],
  4. [21, 22, 23, 24, 25],
  5. [26, 27, 28, 29, 30],
  6. [31, 32, 33, 34, 35]])
  7. y = np.cumprod(x)
  8. print(y)
  9. # [ 11 132 1716 24024 360360 5765760
  10. # 98017920 1764322560 -837609728 427674624 391232512 17180672
  11. # 395155456 893796352 870072320 1147043840 905412608 -418250752
  12. # 755630080 1194065920 -1638662144 -897581056 444596224 -2063597568
  13. # 788529152]
  14. y = np.cumprod(x, axis=0)
  15. print(y)
  16. # [[ 11 12 13 14 15]
  17. # [ 176 204 234 266 300]
  18. # [ 3696 4488 5382 6384 7500]
  19. # [ 96096 121176 150696 185136 225000]
  20. # [2978976 3877632 4972968 6294624 7875000]]
  21. y = np.cumprod(x, axis=1)
  22. print(y)
  23. # [[ 11 132 1716 24024 360360]
  24. # [ 16 272 4896 93024 1860480]
  25. # [ 21 462 10626 255024 6375600]
  26. # [ 26 702 19656 570024 17100720]
  27. # [ 31 992 32736 1113024 38955840]]

numpy.diff 差值

  • numpy.diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue)Calculate the n-th discrete difference along the given axis.
    • a:输入矩阵
    • n:可选,代表要执行几次差值
    • axis:默认是最后一个

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.

【例】沿着指定轴计算第N维的离散差值。

  1. import numpy as np
  2. A = np.arange(2, 14).reshape((3, 4))
  3. A[1, 1] = 8
  4. print(A)
  5. # [[ 2 3 4 5]
  6. # [ 6 8 8 9]
  7. # [10 11 12 13]]
  8. print(np.diff(A))
  9. # [[1 1 1]
  10. # [2 0 1]
  11. # [1 1 1]]
  12. print(np.diff(A, axis=0))
  13. # [[4 5 4 4]
  14. # [4 3 4 4]]

四舍五入

numpy.around 舍入

  • numpy.around(a, decimals=0, out=None) Evenly round to the given number of decimals.

【例】将数组舍入到给定的小数位数。

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

numpy.ceil 上限

  • numpy.ceil(x, *args, **kwargs) Return the ceiling of the input, element-wise.

    numpy.floor 下限

  • numpy.floor(x, *args, **kwargs) Return the floor of the input, element-wise.

【例】

  1. import numpy as np
  2. x = np.random.rand(3, 3) * 10
  3. print(x)
  4. # [[0.67847795 1.33073923 4.53920122]
  5. # [7.55724676 5.88854047 2.65502046]
  6. # [8.67640444 8.80110812 5.97528726]]
  7. y = np.ceil(x)
  8. print(y)
  9. # [[1. 2. 5.]
  10. # [8. 6. 3.]
  11. # [9. 9. 6.]]
  12. y = np.floor(x)
  13. print(y)
  14. # [[0. 1. 4.]
  15. # [7. 5. 2.]
  16. # [8. 8. 5.]]

杂项

numpy.clip 裁剪

  • numpy.clip(a, a_min, a_max, out=None, **kwargs): Clip (limit) the values in an array.

将数组中的元素限制在a_min,a_max之间,大于a_max的就使得它等于 a_max,小于a_min的就使得它等于a_min。
【例】裁剪(限制)数组中的值。

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

numpy.absolute 绝对值

  • numpy.absolute(x, *args, **kwargs) Calculate the absolute value element-wise.

    numpy.abs

  • numpy.abs(x, *args, **kwargs) 简写

【例】

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

numpy.sign 返回数字符号的逐元素指示

  • numpy.sign(x, *args, **kwargs) Returns an element-wise indication of the sign of a number.

【例】

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

**