Python 如何使用numpy将一个矩阵与另一个矩阵中的每一行相乘
A的尺寸:N*N(3*3) B的尺寸:K*N(5*3) 预期结果是: C=[A*B[0],A*B[1],A*B[2],A*B[3],A*B[4]](C的维数也是5*3) 我是numpy新手,不知道如何在不使用for循环的情况下执行此操作Python 如何使用numpy将一个矩阵与另一个矩阵中的每一行相乘,python,numpy,Python,Numpy,A的尺寸:N*N(3*3) B的尺寸:K*N(5*3) 预期结果是: C=[A*B[0],A*B[1],A*B[2],A*B[3],A*B[4]](C的维数也是5*3) 我是numpy新手,不知道如何在不使用for循环的情况下执行此操作 谢谢 根据你提供的数学,我认为你在计算A乘以B的转置。如果希望结果矩阵的大小为5*3,可以对其进行转置(相当于numpy.matmul(B.transpose(),A)) 你可以像这样前进: import numpy A = numpy.array([ [0
谢谢 根据你提供的数学,我认为你在计算A乘以B的转置。如果希望结果矩阵的大小为5*3,可以对其进行转置(相当于
numpy.matmul(B.transpose(),A))
你可以像这样前进:
import numpy
A = numpy.array([
[0,1,1],
[2,2,0],
[3,0,3]
])
B = numpy.array([
[1,1,1],
[2,2,2],
[3,2,9],
[4,4,4],
[5,9,5]
])
print(numpy.matmul(A,B.transpose()))
output :array([[ 2, 4, 11, 8, 14],
[ 4, 8, 10, 16, 28],
[ 6, 12, 36, 24, 30]])
for i in range(5):
print (numpy.matmul(A,B[i]))
Output:
[2 4 6]
[ 4 8 12]
[11 10 36]
[ 8 16 24]
[14 28 30]
记住:
对于矩阵
乘法,矩阵-A的第一列顺序
==矩阵-B的第一行顺序-例如:B->(3,3)==(3,5),要获取矩阵的列和行顺序,可以使用:
import numpy as np
matrix_a = np.array([
[0, 1, 1],
[2, 2, 0],
[3, 0, 3]
])
matrix_b = np.array([
[1, 1, 1],
[2, 2, 2],
[3, 2, 9],
[4, 4, 4],
[5, 9, 5]
])
在这里,您可以检查矩阵-A的第一列的顺序
==矩阵-B的第一行的顺序。如果顺序不一样,则进行矩阵B的转置,否则只需相乘即可
rows_of_second_matrix = matrix_b.shape[0]
columns_of_first_matrix = matrix_a.shape[1]
输出:
if columns_of_first_matrix != rows_of_second_matrix:
transpose_matrix_b = np.transpose(matrix_b)
output_1 = np.dot(matrix_a, transpose_matrix_b)
print('Shape of dot product:', output_1.shape)
print('Dot product:\n {}\n'.format(output_1))
output_2 = np.matmul(matrix_a, transpose_matrix_b)
print('Shape of matmul product:', output_2.shape)
print('Matmul product:\n {}\n'.format(output_2))
# In order to obtain -> Output_Matrix of shape (5, 3), Again take transpose
output_matrix = np.transpose(output_1)
print("Shape of required matrix: ", output_matrix.shape)
else:
output_1 = np.dot(matrix_a, matrix_b)
print('Shape of dot product:', output_1.shape)
print('Dot product:\n {}\n'.format(output_1))
output_2 = np.matmul(matrix_a, matrix_b)
print('Shape of matmul product:', output_2.shape)
print('Matmul product:\n {}\n'.format(output_2))
output_matrix = output_2
print("Shape of required matrix: ", output_matrix.shape)
在您的示例中,
A*B[0]
的计算结果是什么?如果输出形状也是(5,3,3)
,那么它可能有助于澄清它是如何工作的。基本上,它只是一个*BT。因此np.matmul(A,B.transpose())将给出您想要的结果?我要求你手工计算,并提供你的预期输出。把答案传回来,得到OP想要的亮度,不是吗<是的,我想是的。我编辑了答案。谢谢你的评论
if columns_of_first_matrix != rows_of_second_matrix:
transpose_matrix_b = np.transpose(matrix_b)
output_1 = np.dot(matrix_a, transpose_matrix_b)
print('Shape of dot product:', output_1.shape)
print('Dot product:\n {}\n'.format(output_1))
output_2 = np.matmul(matrix_a, transpose_matrix_b)
print('Shape of matmul product:', output_2.shape)
print('Matmul product:\n {}\n'.format(output_2))
# In order to obtain -> Output_Matrix of shape (5, 3), Again take transpose
output_matrix = np.transpose(output_1)
print("Shape of required matrix: ", output_matrix.shape)
else:
output_1 = np.dot(matrix_a, matrix_b)
print('Shape of dot product:', output_1.shape)
print('Dot product:\n {}\n'.format(output_1))
output_2 = np.matmul(matrix_a, matrix_b)
print('Shape of matmul product:', output_2.shape)
print('Matmul product:\n {}\n'.format(output_2))
output_matrix = output_2
print("Shape of required matrix: ", output_matrix.shape)
- Shape of dot product: (3, 5)
Dot product:
[[ 2 4 11 8 14]
[ 4 8 10 16 28]
[ 6 12 36 24 30]]
- Shape of matmul product: (3, 5)
Matmul product:
[[ 2 4 11 8 14]
[ 4 8 10 16 28]
[ 6 12 36 24 30]]
- Shape of required matrix: (5, 3)