numpy-具有2d数组的多个3d数组 [英] Numpy - multiple 3d array with a 2d array
问题描述
我正在尝试以下操作:
给定一个矩阵A(x,y,3)和另一个矩阵B(3,3),我想返回一个(x,y,3)矩阵,其中A的第3维乘以值的B(类似于将RGB图像转换为灰色时的情况,只是将那些"RGB"值乘以矩阵而不是标量)...
Given a matrix A (x, y ,3) and another matrix B (3, 3), I would like to return a (x, y, 3) matrix in which the 3rd dimension of A is multiplied by the values of B (similar when an RGB image is transformed into gray, only that those "RGB" values are multiplied by a matrix and not scalars)...
这是我尝试过的:
np.multiply(B, A)
np.einsum('ijk,jl->ilk', B, A)
np.einsum('ijk,jl->ilk', A, B)
所有尺寸均未对齐失败.
All of them failed with dimensions not aligned.
我想念什么?
推荐答案
You can use np.tensordot
-
np.tensordot(A,B,axes=((2),(1)))
einsum
等价于-
np.einsum('ijk,lk->ijl', A, B)
我们也可以使用A.dot(B.T)
,但这将在幕后循环.因此,可能不是最理想的解决方案,但这是一个紧凑的解决方案,
We can also use A.dot(B.T)
, but that would be looping under the hoods. So, might not be the most preferred one, but it's a compact solution,
这篇关于numpy-具有2d数组的多个3d数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!