Numpy Day 2 -逻辑函数,广播

1. 逻辑函数

1.1真值测试

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.

【例】

import numpy as np

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]

axis:
使用0值表示跨行(down)
使用1值表示跨列(across)

np.eye()
函数的原型:numpy.eye(N,M=None,k=0,dtype=<class ‘float’>,order='C)

返回的是一个二维2的数组(N,M),对角线的地方为1,其余的地方为0.

参数介绍:

(1)N:int型,表示的是输出的行数

(2)M:int型,可选项,输出的列数,如果没有就默认为N

(3)k:int型,可选项,对角线的下标,默认为0表示的是主对角线,负数表示的是低对角,正数表示的是高对角。

(4)dtype:数据的类型,可选项,返回的数据的数据类型

(5)order:{‘C’,‘F’},可选项,也就是输出的数组的形式是按照C语言的行优先’C’,还是按照Fortran形式的列优先‘F’存储在内存中

1.2.数组内容

numpy.isnan

  • numpy.isnan(x, *args, **kwargs)

Test element-wise for NaN and return result as a boolean array.

【例】

a=np.array([1,2,np.nan])
print(np.isnan(a))
#[False False  True]

1.3逻辑运算

numpy.logical_not
numpy.logical_and
numpy.logical_or
numpy.logical_xor

  • numpy.logical_not(x, *args, **kwargs)
    Compute the truth value of NOT x element-wise.
  • 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_xor(x1, x2, *args, **kwargs)
    Compute the truth value of x1 XOR x2, element-wise.

【例】计算非x元素的真值。

import numpy as np

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]
x = np.arange(5)
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]
x = np.arange(5)
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]
x = np.arange(5)
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.4对照

numpy.greater
numpy.greater_equal
numpy.equal
numpy.not_equal
numpy.less
numpy.less_equal

  • 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.

【例】numpy对以上对照函数进行了运算符的重载。

import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])

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

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

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

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

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

y = x <= 2
print(y) # [ True  True False False False False False False]
print(np.less_equal(x, 2))
# [ True  True False False False False False False]

【例】

import numpy as np

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 = x > 20
print(y)
print(np.greater(x, 20))
# [[False False False False False]
#  [False False False False False]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]]

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

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

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


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

y = x <= 20
print(y)
print(np.less_equal(x, 20))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [False False False False False]
#  [False False False False False]
#  [False False False False False]]

【例】

import numpy as np

np.random.seed(20200611)
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.random.randint(10, 40, [5, 5])
print(y)
# [[32 28 31 33 37]
#  [23 37 37 30 29]
#  [32 24 10 33 15]
#  [27 17 10 36 16]
#  [25 32 23 39 34]]

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

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

z = x == y
print(z)
print(np.equal(x, y))
# [[False False False False False]
#  [False False False False False]
#  [False False False False False]
#  [False False False False False]
#  [False  True 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  True  True]
#  [ True  True  True  True  True]
#  [ True False  True  True  True]]

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

z = x <= y
print(z)
print(np.less_equal(x, y))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True False  True False]
#  [ True False False  True False]
#  [False  True False  True False]]

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

import numpy as np

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]]

Numpy的广播规则

numpy两个数组的相加、相减以及相乘都是对应元素之间的操作。

import numpy as np

x = np.array([[2,2,3],[1,2,3]])
y = np.array([[1,1,3],[2,2,4]])
print(x*y)  #numpy当中的数组相乘是对应元素的乘积,与线性代数当中的矩阵相乘不一样

# [[ 2  2  9]
#  [ 2  4 12]]

当两个数组的形状并不相同的时候,我们可以通过扩展数组的方法来实现相加、相减、相乘等操作,这种机制叫做广播(broadcasting)。

比如,一个二维数组减去列平均值,来对数组的每一列进行距平化处理:

import numpy as np

arr = np.random.randn(4,3)  #shape(4,3)
arr_mean = arr.mean(0)      #shape(3,)
demeaned = arr -arr_mean
print(demeaned)

# [[-0.38447918 -0.44188175  0.57710696]
#  [ 0.39243862  0.11242401 -0.61372017]
#  [ 0.55069907  0.46957538  0.32882964]
#  [-0.5586585  -0.14011764 -0.29221643]]

很明显上式arr和arr_mean维度并不形同,但是它们可以进行相减操作,这就是通过广播机制来实现的。

广播的原则: 如果两个数组的后缘维度(trailing dimension,即从末尾开始算起的维度)的轴长度相符,或其中的一方的长度为1,则认为它们是广播兼容的。广播会在缺失和(或)长度为1的维度上进行。

这句话乃是理解广播的核心。广播主要发生在两种情况,一种是两个数组的维数不相等,但是它们的后缘维度的轴长相符,另外一种是有一方的长度为1

数组维度不同,后缘维度的轴长相符
例子

import numpy as np

arr1 = np.array([[0, 0, 0],[1, 1, 1],[2, 2, 2], [3, 3, 3]])  #arr1.shape = (4,3)
arr2 = np.array([1, 2, 3])    #arr2.shape = (3,)
arr_sum = arr1 + arr2
print(arr_sum)

上例中arr1的shape为(4,3),arr2的shape为(3,)。可以说前者是二维的,而后者是一维的。但是它们的后缘维度相等,arr1的第二维长度为3,和arr2的维度相同。arr1和arr2的shape并不一样,但是它们可以执行相加操作,这就是通过广播完成的,在这个例子当中是将arr2沿着0轴进行扩展。

上面程序当中的广播如下图所示:
在这里插入图片描述
同样的例子还有:
在这里插入图片描述
从上面的图可以看到,(3,4,2)和(4,2)的维度是不相同的,前者为3维,后者为2维。但是它们后缘维度的轴长相同,都为(4,2),所以可以沿着0轴进行广播。

同样,还有一些例子:(4,2,3)和(2,3)是兼容的,(4,2,3)还和(3)是兼容的,后者需要在两个轴上面进行扩展。

数组维度相同,其中有个轴为1

import numpy as np

arr1 = np.array([[0, 0, 0],[1, 1, 1],[2, 2, 2], [3, 3, 3]])  #arr1.shape = (4,3)
arr2 = np.array([[1],[2],[3],[4]])    #arr2.shape = (4, 1)

arr_sum = arr1 + arr2
print(arr_sum)

# [[1 1 1]
#  [3 3 3]
#  [5 5 5]
#  [7 7 7]]

arr1的shape为(4,3),arr2的shape为(4,1),它们都是二维的,但是第二个数组在1轴上的长度为1,所以,可以在1轴上面进行广播,如下图所示:
在这里插入图片描述
在这种情况下,两个数组的维度要保证相等,其中有一个轴的长度为1,这样就会沿着长度为1的轴进行扩展。这样的例子还有:(4,6)和(1,6) 。(3,5,6)和(1,5,6)、(3,1,6)、(3,5,1),后面三个分别会沿着0轴,1轴,2轴进行广播。

后话:还有上面两种结合的情况,如(3,5,6)和(1,6)是可以相加的。在TensorFlow当中计算张量的时候也是用广播机制,并且和numpy的广播机制是一样的。

以上广播内容出自:
https://www.cnblogs.com/jiaxin359/p/9021726.html

numpy.isclose
numpy.allclose

用来判断两个浮点数的值是否接近或相等

  • 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.

参数:

  • a,b:两个需要比较的浮点数;
  • rtol: 相对公差参数: 相对于输入值的大小,被认为是“接近”的最大差异;
  • atol: 绝对公差参数: 无论输入值的大小,被认为“接近”的最大差异
  • equal_nan: 是否将Nan的值比较为相等。如果是真的,a中的Nan等于b中的Nan

numpy.allclose() 等价于 numpy.all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))。

The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.

判断是否为True的计算依据:

np.absolute(a - b) <= (atol + rtol * absolute(b))

- atol:float,绝对公差。
- rtol:float,相对公差。

a和b之差的绝对值<=( 绝对公差+相对公差* b的绝对值) ????
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.

在这里插入图片描述

【例】比较两个数组是否可以认为相等。

import numpy as np

x = np.isclose([1e10, 1e-7], [1.00001e10, 1e-8])
print(x)  # [ True False]

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

x = np.isclose([1e10, 1e-8], [1.00001e10, 1e-9])
print(x)  # [ True  True]

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

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

x = np.isclose([1.0, np.nan], [1.0, np.nan])
print(x)  # [ True False]

x = np.allclose([1.0, np.nan], [1.0, np.nan])
print(x)  # False

x = np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
print(x)  # [ True  True]

x = np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
print(x)  # True

猜你喜欢

转载自blog.csdn.net/weixin_48760912/article/details/114880986