Numpy学习---Task04---数学函数及逻辑函数

Task04---数学函数及逻辑函数

目录

Task04---数学函数及逻辑函数

 

一、向量化和广播

1)向量化

2)广播

二、数学函数

1)算术运算

2)三角函数

3)指数和对数函数

4)加法函数、乘法函数

5)四舍五入函数

6)统计函数

7)杂项

二、逻辑函数


一、向量化和广播

向量化和广播这两个概念是 numpy 内部实现的基础。

1)向量化

       有了向量化,编写代码时无需使用显式循环。这些循环实际上不能省略,只不过是在内部实现,被代码中的其他结构代替。向量化的应用使得代码更简洁,可读性更强,也可以说使用了向量化方法的代码看上去更“Pythonic”。

#向量化举例
#1、算术运算
a = np.arange(1,5)
b = np.arange(1,5)

print(a)
#[1 2 3 4]
print(b)
#[1 2 3 4]

print(a+b)#[2 4 6 8]

print(a-b)#[0 0 0 0]

print(a*b)#[ 1  4  9 16]

print(a/b)#[1. 1. 1. 1.]

print(a**b)#[  1   4  27 256]

print(a%b)#[0 0 0 0]

#2.与、或、补码

print(a&b)#[1 2 3 4]

print(a|b)#[1 2 3 4]

print(~a)#[-2 -3 -4 -5]

#3、比较

print(a>2.5)#[False False  True  True]

print(a<2.5)#[ True  True False False]

print(a==2.5)#[False False False False]

print(a!=2.5)#[ True  True  True  True]

2)广播

广播(Broadcasting)机制描述了 numpy 如何在算术运算期间处理具有不同形状的数组,让较小的数组在较大的数组上“广播”,以便它们具有兼容的形状。并不是所有的维度都要彼此兼容才符合广播机制的要求,但它们必须满足一定的条件。

总结来说,广播的规则有三个:

  • 如果两个数组的维度数dim不相同,那么小维度数组的形状将会在左边持续补1,补至与大维度数组shape相同。
  • 如果shape维度不匹配,但是有维度是1,那么可以扩展维度是1的维度匹配另一个数组;
  • 如果shape维度不匹配,但是没有任何一个维度是1,则匹配引发错误;
#两数组维度数不等
x = np.arange(4)
y = np.ones((3,4))

print(x.shape)#(4,)

print(y.shape)#(3, 4)

print((x+y).shape)#(3, 4)

print(x+y)
#[[1. 2. 3. 4.]
# [1. 2. 3. 4.]
# [1. 2. 3. 4.]]

#维度数相等但shape维度不匹配
x = np.arange(4).reshape((1,4))
y = np.ones((4,1))

print(x.shape)#(1, 4)

print(y.shape)#(4, 1)

print((x+y).shape)#(4, 4)

print(x+y)
#[[1. 2. 3. 4.]
# [1. 2. 3. 4.]
# [1. 2. 3. 4.]
# [1. 2. 3. 4.]]
#可见输出数组的形状是输入数组形状的各个维度上的最大值。

#不匹配报错
x = np.arange(4)
y = np.ones(5)

print(x.shape)#(4,)

print(y.shape)#(5,)

print(x+y)
#operands could not be broadcast together with shapes (4,) (5,) 

二、数学函数

1)算术运算

  • `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.(平方)
#函数调用遵循广播机制

x = np.arange(11,36).reshape((5,-1))
print(x)
#[[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.arange(1,6)
print(y)#[1 2 3 4 5]

z = np.add(x,y) 
print(z)
#[[12 14 16 18 20]
# [17 19 21 23 25]
# [22 24 26 28 30]
# [27 29 31 33 35]
# [32 34 36 38 40]]

z = np.subtract(x,y)
print(z)
#[[10 10 10 10 10]
# [15 15 15 15 15]
# [20 20 20 20 20]
# [25 25 25 25 25]
# [30 30 30 30 30]]

z = np.multiply(x,y)
print(z)
#[[ 11  24  39  56  75]
# [ 16  34  54  76 100]
# [ 21  44  69  96 125]
# [ 26  54  84 116 150]
# [ 31  64  99 136 175]]

z = np.divide(x,y)
print(z)
#[[11.          6.          4.33333333  3.5         3.        ]
# [16.          8.5         6.          4.75        4.        ]
# [21.         11.          7.66666667  6.          5.        ]
# [26.         13.5         9.33333333  7.25        6.        ]
# [31.         16.         11.          8.5         7.        ]]

z = np.floor_divide(x,y)
print(z)
#[[11  6  4  3  3]
# [16  8  6  4  4]
# [21 11  7  6  5]
# [26 13  9  7  6]
# [31 16 11  8  7]]

z = np.power(x,y)
print(z)
#[[      11      144     2197    38416   759375]
# [      16      289     5832   130321  3200000]
# [      21      484    12167   331776  9765625]
# [      26      729    21952   707281 24300000]
# [      31     1024    35937  1336336 52521875]]
  • `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.(求平方)
x = np.arange(5)
print(x)
#[0 1 2 3 4]

z = np.sqrt(x)
print(z)
#[0.         1.         1.41421356 1.73205081 2.        ]

z = np.square(x)
print(z)
#[ 0  1  4  9 16]

2)三角函数

  • `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.(反正切)

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

x = np.linspace(0, np.pi/2, 10)
print(x)
#[0.         0.17453293 0.34906585 0.52359878 0.6981317  0.87266463
# 1.04719755 1.22173048 1.3962634  1.57079633]

y = np.sin(x)
print(y)
#[0.         0.17364818 0.34202014 0.5        0.64278761 0.76604444
# 0.8660254  0.93969262 0.98480775 1.        ]

z = np.arcsin(y)
print(z)
#[0.         0.17453293 0.34906585 0.52359878 0.6981317  0.87266463
# 1.04719755 1.22173048 1.3962634  1.57079633]

y = np.cos(x)
print(y)
#[1.00000000e+00 9.84807753e-01 9.39692621e-01 8.66025404e-01
# 7.66044443e-01 6.42787610e-01 5.00000000e-01 3.42020143e-01
# 1.73648178e-01 6.12323400e-17]

z = np.arccos(y)
print(z)
#[0.         0.17453293 0.34906585 0.52359878 0.6981317  0.87266463
# 1.04719755 1.22173048 1.3962634  1.57079633]

y = np.tan(x)
print(y)
#[0.00000000e+00 1.76326981e-01 3.63970234e-01 5.77350269e-01
# 8.39099631e-01 1.19175359e+00 1.73205081e+00 2.74747742e+00
# 5.67128182e+00 1.63312394e+16]

z = np.arctan(y)
print(z)
#[0.         0.17453293 0.34906585 0.52359878 0.6981317  0.87266463
# 1.04719755 1.22173048 1.3962634  1.57079633]

3)指数和对数函数

  • `numpy.exp(x, *args, **kwargs)` Calculate the exponential of all elements in the input array.(e的x次幂)
  • `numpy.log(x, *args, **kwargs)` Natural logarithm, element-wise.(以e为底x的对数)
  • `numpy.exp2(x, *args, **kwargs)` Calculate `2**p` for all `p` in the input array.(2的x次幂)
  • `numpy.log2(x, *args, **kwargs)` Base-2 logarithm of `x`.(以2为底x的对数)
  • `numpy.log10(x, *args, **kwargs)` Return the base 10 logarithm of the input array, element-wise.(以10为底x的对数)
x = np.arange(1,5)
print(x)
#[1 2 3 4]

y = np.exp(x)
print(y)
#[ 2.71828183  7.3890561  20.08553692 54.59815003]

z = np.log(y)
print(z)
#[1. 2. 3. 4.]

y = np.exp2(x)
print(y)
#[ 2.  4.  8. 16.]

z = np.log2(y)
print(z)
#[1. 2. 3. 4.]

y = np.power(10,x)
print(y)
#[   10   100  1000 10000]

z = np.log10(y)
print(z)
#[1. 2. 3. 4.]

4)加法函数、乘法函数

  • `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 .sum沿着第`i`个下标变化的方向进行操作。

x = np.random.randint(1,10,(3,3,3))
print(x)
#[[[5 6 8]
#  [7 3 1]
#  [2 2 6]]
#
# [[7 6 4]
#  [4 6 3]
#  [2 1 6]]
#
# [[9 8 1]
#  [9 8 2]
#  [5 4 2]]]

y = np.sum(x)
print(y)
#127

y = np.sum(x,axis=0)
print(y)
#[[21 20 13]
# [20 17  6]
# [ 9  7 14]]

y = np.sum(x,axis=1)
print(y)
#[[14 11 15]
# [13 13 13]
# [23 20  5]]

y = np.sum(x,axis=2)
print(y)
#[[19 11 10]
# [17 13  9]
# [18 19 11]]
  • `numpy.cumsum(a, axis=None, dtype=None, out=None)` Return the cumulative sum of the elements along a given axis.(累积和)

沿着所给axis参数方向逐项累计求和。

x = np.random.randint(1,10,(3,3,3))
print(x)
#[[[5 6 8]
#  [7 3 1]
#  [2 2 6]]
#
# [[7 6 4]
#  [4 6 3]
#  [2 1 6]]
#
# [[9 8 1]
#  [9 8 2]
#  [5 4 2]]]

y = np.cumsum(x)
print(y)
#[  5  11  19  26  29  30  32  34  40  47  53  57  61  67  70  72  73  79
#  88  96  97 106 114 116 121 125 127]

y = np.cumsum(x,axis=0)
print(y)
#[[[ 5  6  8]
#  [ 7  3  1]
#  [ 2  2  6]]
#
# [[12 12 12]
#  [11  9  4]
#  [ 4  3 12]]
#
# [[21 20 13]
#  [20 17  6]
#  [ 9  7 14]]

y = np.cumsum(x,axis=1)
print(y)
#[[[ 5  6  8]
#  [12  9  9]
#  [14 11 15]]
#
# [[ 7  6  4]
#  [11 12  7]
#  [13 13 13]]
#
# [[ 9  8  1]
#  [18 16  3]
#  [23 20  5]]]

y = np.cumsum(x,axis=2)
print(y)
#[[[ 5 11 19]
#  [ 7 10 11]
#  [ 2  4 10]]
#
# [[ 7 13 17]
#  [ 4 10 13]
#  [ 2  3  9]]
#
# [[ 9 17 18]
#  [ 9 17 19]
#  [ 5  9 11]]]
  • `numpy.prod(a[, axis=None, dtype=None, out=None, …])` Return the product of array elements over a given axis.(乘积)
x = np.random.randint(1,10,(3,3,3))
print(x)
#[[[5 6 8]
#  [7 3 1]
#  [2 2 6]]
#
# [[7 6 4]
#  [4 6 3]
#  [2 1 6]]
#
# [[9 8 1]
#  [9 8 2]
#  [5 4 2]]]

y = np.prod(x)
print(y)
#637534208

y = np.prod(x,axis=0)
print(y)
#[[315 288  32]
# [252 144   6]
# [ 20   8  72]]

y = np.prod(x,axis=1)
print(y)
#[[ 70  36  48]
# [ 56  36  72]
# [405 256   4]]

y = np.prod(x,axis=2)
print(y)
#[[240  21  24]
# [168  72  12]
# [ 72 144  40]]
  • `numpy.cumprod(a, axis=None, dtype=None, out=None)` Return the cumulative product of elements along a given axis.(累乘)
x = np.random.randint(1,10,(3,3,3))
print(x)
#[[[5 6 8]
#  [7 3 1]
#  [2 2 6]]
#
# [[7 6 4]
#  [4 6 3]
#  [2 1 6]]
#
# [[9 8 1]
#  [9 8 2]
#  [5 4 2]]]

#将x中数据类型转换为长整型,避免累乘结果溢出
x = x.astype(np.int64)

y = np.cumprod(x)
print(y)
#[               5               30              240             1680
#             5040             5040            10080            20160
#           120960           846720          5080320         20321280
#         81285120        487710720       1463132160       2926264320
#       2926264320      17557585920     158018273280    1264146186240
#    1264146186240   11377315676160   91018525409280  182037050818560
#  910185254092800 3640741016371200 7281482032742400]

y = np.cumprod(x,axis=0)
print(y)
#[[[  5   6   8]
#  [  7   3   1]
#  [  2   2   6]]
#
# [[ 35  36  32]
#  [ 28  18   3]
#  [  4   2  36]]
#
# [[315 288  32]
#  [252 144   6]
#  [ 20   8  72]]]

y = np.cumprod(x,axis=1)
print(y)
#[[[  5   6   8]
#  [ 35  18   8]
#  [ 70  36  48]]
#
# [[  7   6   4]
#  [ 28  36  12]
#  [ 56  36  72]]
#
# [[  9   8   1]
#  [ 81  64   2]
#  [405 256   4]]]

y = np.cumprod(x,axis=2)
print(y)
#[[[  5  30 240]
#  [  7  21  21]
#  [  2   4  24]]
#
# [[  7  42 168]
#  [  4  24  72]
#  [  2   2  12]]
#
# [[  9  72  72]
#  [  9  72 144]
#  [  5  20  40]]]
  • `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:默认是最后一个

#沿着指定轴计算第N维的离散差值。
x = np.random.randint(1,10,(3,3,3))
print(x)
#[[[5 6 8]
#  [7 3 1]
#  [2 2 6]]
#
# [[7 6 4]
#  [4 6 3]
#  [2 1 6]]
#
# [[9 8 1]
#  [9 8 2]
#  [5 4 2]]]

y = np.diff(x)
print(y)
#[[[ 1  2]
#  [-4 -2]
#  [ 0  4]]
#
# [[-1 -2]
#  [ 2 -3]
#  [-1  5]]
#
# [[-1 -7]
#  [-1 -6]
#  [-1 -2]]]

y = np.diff(x,axis=0)
print(y)
#[[[ 2  0 -4]
#  [-3  3  2]
#  [ 0 -1  0]]
#
# [[ 2  2 -3]
#  [ 5  2 -1]
#  [ 3  3 -4]]]

y = np.diff(x,axis=1)
print(y)
#[[[ 2 -3 -7]
#  [-5 -1  5]]
#
# [[-3  0 -1]
#  [-2 -5  3]]
#
# [[ 0  0  1]
#  [-4 -4  0]]]

y = np.diff(x,axis=2)
print(y)
#[[[ 1  2]
#  [-4 -2]
#  [ 0  4]]
#
# [[-1 -2]
#  [ 2 -3]
#  [-1  5]]
#
# [[-1 -7]
#  [-1 -6]
#  [-1 -2]]]

5)四舍五入函数

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

x = np.random.rand(3,3)*10
print(x)
#[[3.15130063 2.72268082 0.49932473]
# [0.79021506 4.92113421 8.20935821]
# [8.42156508 8.10899641 4.50658408]]

y = np.round(x)
print(y)
#[[3. 3. 0.]
# [1. 5. 8.]
# [8. 8. 5.]]

#将数组舍入到给定的小数位数。
y = np.round(x,decimals=2)
print(y)
#[[3.15 2.72 0.5 ]
# [0.79 4.92 8.21]
# [8.42 8.11 4.51]]

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

print(np.ceil(x))
#[[4. 3. 1.]
# [1. 5. 9.]
# [9. 9. 5.]]

print(np.floor(x))
#[[3. 2. 0.]
# [0. 4. 8.]
# [8. 8. 4.]]

6)统计函数


  • numpy.amin(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>) :沿指定轴的最小值
  • numpy.amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>):沿指定轴的最大值
  • numpy.ptp(a, axis=None, out=None, keepdims=<no value>):沿指定轴的最小值与最大值之差
x = np.random.randint(1,10,(3,3))
print(x)
#[[3 7 3]
# [4 5 8]
# [4 4 7]]

print(np.amin(x))
#3

print(np.amin(x,axis=0))
#[3 4 3]

print(np.amax(x))
#8

print(np.amax(x,axis=1))
#[4 7 8]

print(np.ptp(x))
#5

print(np.ptp(x,axis=0))
#[1 3 5]
  • numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False):计算数组的任意百分比分位数
  • numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False):计算数组 a 中元素的中位数
x = np.random.randint(1,10,(3,3))
print(x)
#[[3 7 3]
# [4 5 8]
# [4 4 7]]

print(np.percentile(x,50))
#4.0

print(np.percentile(x,50,axis=0))
#[4. 5. 7.]

print(np.percentile(x,50,axis=1))
#[3. 5. 4.]

print(np.median(x))
#4.0

print(np.median(x,axis=0))
#[4. 5. 7.]

print(np.median(x,axis=1))
#[3. 5. 4.]
  • numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<no value>):返回数组中元素的算术平均值
  • numpy.average(a, axis=None, weights=None, returned=False):根据在另一个数组中给出的各自的权重计算数组中元素的加权平均值 
y =np.array([1,2,3,4])
print(np.mean(y))
#2.5

print(np.average(y))
#2.5

print(np.average(y,weights=[1,2,3,4]))
#3.0

print(np.average(y,weights=[1/10,2/10,3/10,4/10]))
#3.0
  • numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>):标准差
  • numpy.var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>):方差
  • numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None):协方差矩阵
  • numpy.corrcoef(x, y=None, rowvar=True, bias=<no value>, ddof=<no value>):相关性矩阵
x = np.random.randint(1,10,(3,3))
print(x)
#[[3 7 3]
# [4 5 8]
# [4 4 7]]

print(np.std(x))
#1.7638342073763937

print(np.std(x,axis=0))
#[0.47140452 1.24721913 2.1602469 ]

print(np.var(x))
#3.111111111111111

print(np.var(x,axis=0))
#[0.22222222 1.55555556 4.66666667]

print(np.cov(x))
#[[ 5.33333333 -1.33333333 -2.        ]
# [-1.33333333  4.33333333  3.5       ]
# [-2.          3.5         3.        ]]

print(np.corrcoef(x))
#[[ 1.         -0.2773501  -0.5       ]
# [-0.2773501   1.          0.97072534]
# [-0.5         0.97072534  1.        ]]

7)杂项

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

将数组a中小于a_min的值换成a_min,将大于a_max的值换成a_max。

x = np.random.randint(1,10,(3,3))
print(x)
#[[3 7 3]
# [4 5 8]
# [4 4 7]]

print(np.clip(x,5,7))
#[[5 7 5]
# [5 5 7]
# [5 5 7]]
  • `numpy.absolute(x, *args, **kwargs)` Calculate the absolute value element-wise. (求绝对值)

附:`numpy.abs(x, *args, **kwargs)` is a shorthand for this function.

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

print(np.absolute(x))
#[5 4 3 2 1 0 1 2 3 4]

print(np.abs(x))
#[5 4 3 2 1 0 1 2 3 4]
  • `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)真值测试

  • `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.random.randint(0,10,7)
b = np.copy(a)
print(a)
#[2 8 6 0 9 0 9]

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

a = np.identity(3)#生成单位矩阵
print(np.all(a,axis=1))
#[False False False]

print(np.any(a,axis=1))
[ True  True  True]

2)逻辑运算

  • `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.(逻辑异或)
x = np.linspace(0,5,7,dtype='float32')
print(np.around(x,2))
#[0.   0.83 1.67 2.5  3.33 4.17 5.  ]

print(np.logical_and(x>1, x<4))
#[False False  True  True  True False False]

print(np.logical_or(x<1, x>4))
#[ True  True False False False  True  True]

#print(np.logical_not(x<2))
#[False False False  True  True  True  True]

print(np.logical_xor(x<1, x>4))
#[ True  True False False False  True  True]

3)比较

  • `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.random.randint(10,50,(4,4))
print(x)
#[[27 45 41 47]
# [26 34 22 44]
# [14 18 45 26]
# [10 24 28 16]]

y = np.random.randint(10,50,4)
print(y)
#[42 29 41 41]

print(np.greater(x,y))
#[[False  True False  True]
# [False  True False  True]
# [False False  True False]
# [False False False False]]

print(np.greater_equal(x,y))
#[[False  True  True  True]
# [False  True False  True]
# [False False  True False]
# [False False False False]]

print(np.equal(x,y))
#[[False False  True False]
# [False False False False]
# [False False False False]
# [False False False False]]

print(np.not_equal(x,y))
#[[ True  True False  True]
# [ True  True  True  True]
# [ True  True  True  True]
# [ True  True  True  True]]

print(np.less(x,y))
#[[ True False False False]
# [ True False  True False]
# [ True  True False  True]
# [ True  True  True  True]]

print(np.less_equal(x,y))
#[[ True False  True False]
# [ True False  True False]
# [ True  True False  True]
# [ True  True  True  True]]
  • `numpy.isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False)` Returns a boolean array where two arrays are element-wise equal within a tolerance.(是否接近)
  • `numpy.allclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False)` Returns True if two arrays are element-wise equal within a tolerance. (是否全部接近)
  1. atol:float,绝对公差。rtol:float,相对公差。
  2. `numpy.allclose()` 等价于 `numpy.all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))`。
  3. 判断是否为True的计算依据:np.absolute(a - b) <= (atol + rtol * absolute(b))
x = np.isclose([1e10,1e-8],[1.0001e10,1e-9])
print(x)
#[False  True]

x = np.allclose([1e10,1e-8],[1.0001e10,1e-9])
print(x)
#False

#NaNs are treated as equal if they are in the same place and if `equal_nan=True`.  Infs are #treated as equal if they are in the same place and of the same sign in both arrays.
x = np.isclose([1e10,np.nan],[1.0001e10,np.NAN])
print(x)
#[False False]

x = np.isclose([1e10,np.nan],[1.0001e10,np.NAN],equal_nan=True)
print(x)
#[False  True]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值