python - Numpy:确定广播后的形状

标签 python numpy

我有 2 个 numpy 数组的形状。我想检查是否可以添加这些数组并确定结果形状。

有什么方法可以快速做到这一点并覆盖所有边缘情况吗?

最佳答案

如果您有实际的 NumPy 对象,您可以使用 NumPy 的逻辑来确定这一点(添加需要广播,请参阅 here ):

def checkShapes(a, b):
  try:
    return (np.broadcast(a, b)).shape
  except:
    return (-1,)

关于python - Numpy:确定广播后的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36949908/

相关文章:

python - 聚合 numpy 屏蔽数组

python - 在多次跳转或列中将 numpy 数组转换为 pandas

python - django 在聚合期间对字段执行逻辑(而不是直接进行聚合(Sum ('somefield' )))

python - 如何将日期时间类型(Jython 的)转换为 java.util.Date?

python 3 : Is the method I'm using to count the results of combination too slow?

python - Pandas 列到 numpy 数组

python - 使用 python 反向模板

python - 如何按相同字段合并字典列表并在此过程中对另一个字段求​​和?

python - 从图的顶部到底部更改 matshow xticklabel 位置

python - Lorentzian scipy.optimize.leastsq 适合数据失败