Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError使用OR运算符合并两个数组_Python_Arrays_Numpy - Fatal编程技术网

Python ValueError使用OR运算符合并两个数组

Python ValueError使用OR运算符合并两个数组,python,arrays,numpy,Python,Arrays,Numpy,我正在使用Python和numpy,其中有两个形状相同的numpy数组,我正在尝试创建这些数组的并集。这些数组只包含0和1,基本上我想使用OR操作将它们合并到一个新数组中。因此,我做了以下工作: import numpy as np segs = list() a = np.ones((10, 10)).astype('uint8') b = np.zeros((10, 10)).astype('uint8') segs.append(a) segs.append(b) mask = np.

我正在使用Python和numpy,其中有两个形状相同的numpy数组,我正在尝试创建这些数组的并集。这些数组只包含0和1,基本上我想使用OR操作将它们合并到一个新数组中。因此,我做了以下工作:

import numpy as np

segs = list()
a = np.ones((10, 10)).astype('uint8')
b = np.zeros((10, 10)).astype('uint8')
segs.append(a)
segs.append(b)

mask = np.asarray([any(tup) for tup in zip(*segs)]).astype('uint8')
使用最后一个状态,我得到错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
如果我使用
np.any
,不知何故,我的数组形状现在只是
(10,)
。如何在不显式循环数组的情况下创建此合并

编辑

mask = np.asarray([any(tup) for tup in zip(segs)]).astype('uint8')

也会导致相同的错误。

您的
segs
是两个数组的列表:

In [25]: segs = [np.ones((3,6),'uint8'), np.zeros((3,6),'uint8')]
In [26]: [tup for tup in zip(*segs)]
Out[26]: 
[(array([1, 1, 1, 1, 1, 1], dtype=uint8),
  array([0, 0, 0, 0, 0, 0], dtype=uint8)),
 (array([1, 1, 1, 1, 1, 1], dtype=uint8),
  array([0, 0, 0, 0, 0, 0], dtype=uint8)),
 (array([1, 1, 1, 1, 1, 1], dtype=uint8),
  array([0, 0, 0, 0, 0, 0], dtype=uint8))]
zip
生成1d数组的元组(两个数组的行配对)。Python
应用于数组的任何
都会产生歧义错误-这对于其他逻辑Python操作(如
if
)都是如此,这些操作需要标量
真/假

您尝试了
np.any
-将数组元组转换为二维数组。但是如果没有
参数,它将在展平版本上工作,则返回标量
真/假
。但通过axis参数,我们可以跨行应用此
任意

In [27]: [np.any(tup, axis=0) for tup in zip(*segs)]
Out[27]: 
[array([ True,  True,  True,  True,  True,  True]),
 array([ True,  True,  True,  True,  True,  True]),
 array([ True,  True,  True,  True,  True,  True])]
使用注释中建议的
logical_或
ufunc:

In [31]: np.logical_or(segs[0],segs[1])
Out[31]: 
array([[ True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True]])
In [32]: np.logical_or.reduce(segs)
Out[32]: 
array([[ True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True]])
使用“|”运算符并不完全相同:

In [33]: segs[0] | segs[1]
Out[33]: 
array([[1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1]], dtype=uint8)

它使用
segs[0]。\uuuu或(segs[1])
方法。我得检查一下文件,看看是怎么回事。应用于
uint8
(或其他数值)与应用于
bool
不同。几乎看起来像a
max

那么
a | b
呢?是的,但理论上我可以有很多数组。我这样做是为了创建一个简单的可复制程序。大小也只能在运行时知道,那么
np.logical\u或.reduce((a,b))
呢?您当前的代码在每次迭代中调用两个NumPy数组的元组上的
any
,这显然是行不通的。啊……行得通!是否要将其作为答案编写?Python逻辑运算符不能很好地处理numpy数组(
if
any
等)。像
|
np这样的小游戏。任何
都更好。