Python 按行比较N x 2 2D数组与1 x 2数组
考虑一个数组:Python 按行比较N x 2 2D数组与1 x 2数组,python,numpy,Python,Numpy,考虑一个数组: array([[ 0, 0], [ 0, 0], [ 0, 0], [ 0, 255], [255, 255], [255, 255], [255, 0], [ 0, 0], [ 0, 0], [ 0, 0], [ 0, 0], [ 0, 255],
array([[ 0, 0],
[ 0, 0],
[ 0, 0],
[ 0, 255],
[255, 255],
[255, 255],
[255, 0],
[ 0, 0],
[ 0, 0],
[ 0, 0],
[ 0, 0],
[ 0, 255],
[255, 255],
[255, 0],
[ 0, 0],
[ 0, 0]], dtype=uint8))
我想将其与[255,0]
进行比较,得到如下输出:
array([False,
False,
False,
False,
False,
False,
True,
False,
False,
False,
False,
False,
False,
True,
False,
False])
如何使用numpy实现这一点,因为当我执行arr==[255,0]
时,得到的形状是(N,2),这意味着它是一个元素级比较:
array([[False, True],
[False, True],
[False, True],
[False, False],
[ True, False],
[ True, False],
[ True, True],
[False, True],
[False, True],
[False, True],
[False, True],
[False, False],
[ True, False],
[ True, True],
[False, True],
[False, True]])
我尝试的是使用np.apply\u沿着np.logical\u和
函数的np.apply\u轴,但由于and函数有两个参数,我的行被视为一个参数,因此它给出了一个无效的参数错误:
np.apply_along_axis(lambda x: np.logical_and(x), 1, arr==[255,0])
Value Error: Invalid number of arguments
输出
array([False, False, False, False, False, False, True, False, False,
False, False, False, False, True, False, False])
array([False, False, False, False, False, False, True, False, False,
False, False, False, False, True, False, False])