numpy矩阵乘法_NumPy矩阵乘法

numpy矩阵乘法

NumPy matrix multiplication can be done by the following three methods.

NumPy矩阵乘法可以通过以下三种方法完成。

  1. multiply(): element-wise matrix multiplication.

    multiple():逐元素矩阵乘法。
  2. matmul(): matrix product of two arrays.

    matmul():两个数组的矩阵乘积。
  3. dot(): dot product of two arrays.

    dot():两个数组的点积。

1. NumPy矩阵乘法元素明智 (1. NumPy Matrix Multiplication Element Wise)

If you want element-wise matrix multiplication, you can use multiply() function.

如果要逐元素矩阵相乘,可以使用multiple()函数。

import numpy as np

arr1 = np.array([[1, 2],
                 [3, 4]])
arr2 = np.array([[5, 6],
                 [7, 8]])

arr_result = np.multiply(arr1, arr2)

print(arr_result)

Output:

输出:

[[ 5 12]
 [21 32]]

The below image shows the multiplication operation performed to get the result matrix.

下图显示了为获得结果矩阵而执行的乘法运算。

Numpy Matrix Multiply

Numpy Matrix multiply()

numpy矩阵乘法()

2.两个NumPy阵列的矩阵乘积 (2. Matrix Product of Two NumPy Arrays)

If you want the matrix product of two arrays, use matmul() function.

如果要两个数组的矩阵乘积,请使用matmul()函数。

import numpy as np

arr1 = np.array([[1, 2],
                 [3, 4]])
arr2 = np.array([[5, 6],
                 [7, 8]])

arr_result = np.matmul(arr1, arr2)

print(f'Matrix Product of arr1 and arr2 is:\n{arr_result}')

arr_result = np.matmul(arr2, arr1)

print(f'Matrix Product of arr2 and arr1 is:\n{arr_result}')

Output:

输出:

Matrix Product of arr1 and arr2 is:
[[19 22]
 [43 50]]
Matrix Product of arr2 and arr1 is:
[[23 34]
 [31 46]]

The below diagram explains the matrix product operations for every index in the result array. For simplicity, take the row from the first array and the column from the second array for each index. Then multiply the corresponding elements and then add them to reach the matrix product value.

下图说明了结果数组中每个索引的矩阵乘积运算。 为简单起见,为每个索引取第一个数组的行和第二个数组的列。 然后乘以相应的元素,然后将它们相加以达到矩阵乘积值。

Numpy Matrix Product

Numpy Matrix Product

块状矩阵产品

The matrix product of two arrays depends on the argument position. So matmul(A, B) might be different from matmul(B, A).

两个数组的矩阵乘积取决于参数位置。 因此,matmul(A,B)可能与matmul(B,A)不同。

3.两个NumPy阵列的点积 (3. Dot Product of Two NumPy Arrays)

The numpy dot() function returns the dot product of two arrays. The result is the same as the matmul() function for one-dimensional and two-dimensional arrays.

numpy dot()函数返回两个数组的点积。 结果与一维和二维数组的matmul()函数相同。

import numpy as np

arr1 = np.array([[1, 2],
                 [3, 4]])
arr2 = np.array([[5, 6],
                 [7, 8]])

arr_result = np.dot(arr1, arr2)

print(f'Dot Product of arr1 and arr2 is:\n{arr_result}')

arr_result = np.dot(arr2, arr1)

print(f'Dot Product of arr2 and arr1 is:\n{arr_result}')

arr_result = np.dot([1, 2], [5, 6])
print(f'Dot Product of two 1-D arrays is:\n{arr_result}')

Output:

输出:

Dot Product of arr1 and arr2 is:
[[19 22]
 [43 50]]
Dot Product of arr2 and arr1 is:
[[23 34]
 [31 46]]
Dot Product of two 1-D arrays is:
17

参考资料 (References)

翻译自: https://www.journaldev.com/32966/numpy-matrix-multiplication

numpy矩阵乘法

  • 27
    点赞
  • 123
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值