测试数组是否可以广播到给定形状的最佳方法是什么?
trying 的“pythonic”方法不适用于我的情况,因为其目的是对操作进行惰性评估。
我在问如何在下面实现 is_broadcastable:
>>> x = np.ones([2,2,2])
>>> y = np.ones([2,2])
>>> is_broadcastable(x,y)
True
>>> y = np.ones([2,3])
>>> is_broadcastable(x,y)
False
或者更好:
>>> is_broadcastable(x.shape, y.shape)
请您参考如下方法:
我真的认为你们想多了,为什么不保持简单呢?
def is_broadcastable(shp1, shp2):
for a, b in zip(shp1[::-1], shp2[::-1]):
if a == 1 or b == 1 or a == b:
pass
else:
return False
return True
