博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
numpy矩阵乘法_NumPy矩阵乘法
阅读量:2534 次
发布时间:2019-05-11

本文共 2765 字,大约阅读时间需要 9 分钟。

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 nparr1 = 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 nparr1 = 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 nparr1 = 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)

翻译自:

numpy矩阵乘法

转载地址:http://humzd.baihongyu.com/

你可能感兴趣的文章
从微信小程序开发者工具源码看实现原理(一)- - 小程序架构设计
查看>>
ASP.NET 上的 Async/Await 简介
查看>>
Effective C++学习笔记(1)
查看>>
element-ui 组件源码分析整理笔记目录
查看>>
GridEh排序
查看>>
[oc学习笔记]多态
查看>>
Tomcat connectionTimeout问题定位处理
查看>>
【PP系列】SAP 取消报工后修改日期
查看>>
ZooKeeper学习第四期---构建ZooKeeper应用(转)
查看>>
JNday4-am
查看>>
UI控件(复习一下)
查看>>
window下自己主动备份数据库成dmp格式的bat写法
查看>>
Memcache存储大数据的问题
查看>>
HDU 5050 Divided Land(进制转换)
查看>>
python进阶学习笔记(三)
查看>>
javascript语法之Date对象与小案例
查看>>
Day45 jquery表格操作、轮播图
查看>>
POJ 2079 Triangle 旋转卡壳求最大三角形
查看>>
【模板】树链剖分
查看>>
计算机博弈研究——六子棋
查看>>