What is the most pythonic way to multiply each row(axis=2) of a np array with a matrix. For example, I am working with images read as np array of shape (480, 512, 3), I want to multiply each img[i,j] with a 3x3 matrix. I don't want to use for loops for this. This is what I tried but it gives an error
将np数组的每一行(轴= 2)与矩阵相乘的最pythonic方法是什么。例如,我正在使用读取为np数组形状(480,512,3)的图像,我想将每个img [i,j]与3x3矩阵相乘。我不想为此使用for循环。这是我尝试过但它给出了一个错误
A = np.array([
[.412453, .35758, .180423],
[.212671, .71516, .072169],
[.019334, .119193, .950227]
])
lin_XYZ = lambda x: np.dot(A, x[::-1])
#lin_XYZ = np.vectorize(lin_XYZ)
tmp_img = lin_XYZ(tmp_img[:,:])
File ".\proj1a.py", line 24, in color2luv
tmp_img = lin_XYZ(tmp_img[:,:])
File ".\proj1a.py", line 22, in <lambda>
lin_XYZ = lambda x: np.dot(A, x)
ValueError: shapes (3,3) and (480,512,3) not aligned: 3 (dim 1) != 512 (dim 1)
ValueError:形状(3,3)和(480,512,3)未对齐:3(暗淡1)!= 512(暗淡1)
1 个解决方案
#1
1
So A
is (3,3) and x
is (480, 512, 3), and you what is a dot
on the size 3 dimension. The key thing to remember with dot(A,B)
is, last dim of A with 2nd to the last of B. (That's what the error is complaining about 3 (dim 1) != 512 (dim 1)
)
所以A是(3,3)而x是(480,512,3),你就是3号尺寸上的点。用点(A,B)记住的关键是,A的最后一个D是第二个到B的最后一个。(这就是错误抱怨3(暗淡1)!= 512(暗淡1))
x.dot(A)
x.dot(A.T)
would meet that requirement.
会满足这个要求。
A.dot(x.transpose(0,2,1)) # (3,3) with (480,3,512)
would also work, though the resulting array may need further transposing - assuming you want the 3 to be last.
也可以工作,虽然结果数组可能需要进一步转置 - 假设您希望3是最后一个。
You can also pair dimensions with einsum
or tensordot
:
您还可以将尺寸与einsum或tensordot配对:
np.einsum('ij,kli->klj', A, x)
x[::-1]
flips x
on its first dimenion, the 480 one. Shape remains the same. Did you want the transpose?
x [:: - 1]在第一个维度上翻转x,即480个。形状保持不变。你想要转置吗?
#1
1
So A
is (3,3) and x
is (480, 512, 3), and you what is a dot
on the size 3 dimension. The key thing to remember with dot(A,B)
is, last dim of A with 2nd to the last of B. (That's what the error is complaining about 3 (dim 1) != 512 (dim 1)
)
所以A是(3,3)而x是(480,512,3),你就是3号尺寸上的点。用点(A,B)记住的关键是,A的最后一个D是第二个到B的最后一个。(这就是错误抱怨3(暗淡1)!= 512(暗淡1))
x.dot(A)
x.dot(A.T)
would meet that requirement.
会满足这个要求。
A.dot(x.transpose(0,2,1)) # (3,3) with (480,3,512)
would also work, though the resulting array may need further transposing - assuming you want the 3 to be last.
也可以工作,虽然结果数组可能需要进一步转置 - 假设您希望3是最后一个。
You can also pair dimensions with einsum
or tensordot
:
您还可以将尺寸与einsum或tensordot配对:
np.einsum('ij,kli->klj', A, x)
x[::-1]
flips x
on its first dimenion, the 480 one. Shape remains the same. Did you want the transpose?
x [:: - 1]在第一个维度上翻转x,即480个。形状保持不变。你想要转置吗?