【问题标题】:how to multiply a matrix with every row in another matrix using numpy如何使用numpy将矩阵与另一个矩阵中的每一行相乘
【发布时间】:2020-02-24 08:47:47
【问题描述】:
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]
])

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[0] 的评估结果是什么?
  • 输出形状应该是(5, 3, 3)
  • 也阅读here,也许它会有助于阐明它是如何工作的。
  • 基本上就是A*BT。所以 np.matmul(A,B.transpose()) 会给你想要的
  • 哪个是?我要求你手工做数学并提供你预期的输出

标签: python numpy


【解决方案1】:

根据您提供的数学,我认为您正在评估 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]

【讨论】:

【解决方案2】:

你可以这样前进:

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]
])

记住: 对于matrix乘法,matrix-A的第一列的顺序==matrix-B的第一行的顺序 - 如:B -> (3, 3) == (3, 5),得到列的顺序和行矩阵,你可以使用:

rows_of_second_matrix = matrix_b.shape[0]
columns_of_first_matrix = matrix_a.shape[1]

在这里,您可以检查matrix-A的第一列的顺序是否==matrix-B的第一行的顺序。如果顺序不同,则转置matrix-B,否则只需相乘。

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)

【讨论】:

    猜你喜欢
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2012-09-22
    相关资源
    最近更新 更多