Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个 5 维矩阵v,现在我想要一个新的矩阵来D实现
v
D
D[a, b, n, m, d] = v[a, b, n, n, d]-v[a, b, m, m, d].
我如何在 numpy 中优雅地做到这一点?
你想如何改变维度?你可以像这样重塑它
import numpy as np a, b, n, d = 2, 3, 4, 5 v = np.zeros((a, b, n, n, d)) D = v.reshape((a, b, n*n, d))
我发现einsum可以这样做:
einsum
D = np.einsum('abiic->abic', v)[..., None, :] - np.einsum('abiic->abic', v)[:, :, None, ...]