是否有比此列表理解更简单的Numpy阵列广播表达式?
在此代码段中:
triangles = np.float32([[[0, -2], [-2, 3], [1, 1]], [[0, -1], [-1, 3], [1, 1]]])
centers = np.average(triangles, axis=1)
samples = np.float32([t-centers[i] for i, t in enumerate(triangles)])
我想将样本
作为阵列广播扣除,即类似于triangles-Centers
的东西,由于以下原因是不起作用的:
ValueError: operands could not be broadcast together with shapes (2,3,2) (2,2)
是否有:定义样本
的简单方法比列表理解?
In this code snippet:
triangles = np.float32([[[0, -2], [-2, 3], [1, 1]], [[0, -1], [-1, 3], [1, 1]]])
centers = np.average(triangles, axis=1)
samples = np.float32([t-centers[i] for i, t in enumerate(triangles)])
I would like to express samples
as an array broadcast subtraction, i.e. something similar to triangles-centers
, which doesn't work due to:
ValueError: operands could not be broadcast together with shapes (2,3,2) (2,2)
Is there a simpler way to define samples
than a list comprehension?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
numpy.mean.mean
与keepdims = true
在轴上保持一个长度-1维度,您要遵守:Use
numpy.mean
withkeepdims=True
to keep a length-1 dimension in the axis you're taking the mean over:我相信这做了您想要的。只需重塑中心以匹配行大小即可。
I believe this does what you want. Just reshape the centers to match the row size.
受@Tim Roberts上面答案的启发:
Inspired by an answer above from @Tim Roberts: