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矩阵乘法_NumPy矩阵乘法_第1张图片

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矩阵乘法_NumPy矩阵乘法_第2张图片

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
  • numpy.square()

    numpy.square()
  • NumPy sqrt() – Square Root of Matrix Elements

    NumPy sqrt()–矩阵元素的平方根
  • Python NumPy Tutorial

    Python NumPy教程

参考资料 (References)

  • numpy matmul()

    numpy matmul()
  • numpy multiply()

    numpy的multiple()

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

numpy矩阵乘法

你可能感兴趣的:(python,numpy,机器学习,matlab,php)