一个更好的方式来表达大量的点产品?

2024-10-02 00:26:29 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有更好更快的方式来表达以下的点产品在纽比? 我有以下形状:

>>> h.shape
(600L, 400L, 3L)
>>> c.shape
(400L, 3L)

如果没有循环,我想计算以下内容:

^{pr2}$

我想用simeple整形是可能的,但我不知道atm是怎么回事。在


Tags: 产品方式形状shapeatmpr2simeple
2条回答

您可以使用numpy.einsum

ans = einsum('ijk,jk->ij', h, c)

作为沃伦解决方案的一个替代方案,我认为这是最好的,这里有一个未记录的inner1d

>>> from numpy.core.umath_tests import inner1d
>>> a = inner1d(h, c)
>>> np.allclose(a, ans)
True

从其docstring:

inner1d(x1, x2[, out])

inner on the last dimension and broadcast on the rest: (i),(i)->()

对于这个特殊情况,在我的系统中,inner1dnp.einsum稍快:

^{pr2}$

相关问题 更多 >

    热门问题